我正在学习 spring :)
我有以下疑问,
- spring 是否为每个 bean 创建代理对象,即使我没有使用注释,例如我认为需要代理的 @Transactional。
- 我在 spring documentation 中阅读了以下内容
Note that calls to static @Bean methods will never get intercepted by the container, not even within @Configuration classes (see above). This is due to technical limitations: CGLIB subclassing can only override non-static methods. As a consequence, a direct call to another @Bean method will have standard Java semantics, resulting in an independent instance being returned straight from the factory method itself.
一个。由于无法覆盖的静态方法,Spring 在以下示例中不返回代理对象。所以它会在这里返回原始对象。我说得对吗?
@Configuration
public class Config {
@Bean
public static Student student() {
return new Student();
}
}
“请注意,对静态 @Bean 方法的调用永远不会被容器拦截,即使在 @Configuration 类中也是如此”是什么意思
最佳答案
一个。是的,你是对的。您将获得原始对象。
student() 方法位于 Config 类中,该类使用 @Configuration 注释进行注释。所有在@Configuration 类中用@Bean 注解的方法都会被spring 通过创建代理拦截。因此,由于您的方法是静态的,并且虽然它在 Config 类中,但它不会被 Spring 拦截,也不会创建代理。
关于java - spring 使用的代理模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36637702/