在@Component class 中注册bean 是否尊重@Scope?
Does registering bean inside @Component class respect @Scope?
这 website 表示在组件 classes 中注册的 bean 没有被 cglib 代理,并且不通过 spring 容器。那么这是否意味着如果我在组件 class 中注册一个 bean(下面的片段),添加 @Scope("request") 不会有任何区别,并且 AnotherBean
的新实例将始终是每当从某些外部 class?
调用 testBean.anotherBean()
时创建
@Component
public class TestBean {
@Bean
@Scope("request")
public AnotherBean anotherBean() {
return new AnotherBean();
}
}
未被 cglib 代理的 bean 是 @Component
本身,而不是使用 @Bean
注释注册的 bean。如果您没有显式调用 anotherBean 方法,则不会有什么不同,因为在调用带有 @Bean
注释的方法时代理用于 return bean。看例子
bean testBeanComponent
不是 cglib 代理:
@Component
public class TestBeanComponent {
@Bean
@Scope("request")
public AnotherBeanComponent anotherBeanComponent() {
return new AnotherBeanComponent();
}
}
bean testBeanConfiguration
是 cglib 代理的:
@Configuration
public class TestBeanConfiguration {
@Bean
@Scope("request")
public AnotherBeanConfiguration anotherBeanConfiguration() {
return new AnotherBeanConfiguration();
}
}
什么意思:
@Service
public class TestService {
@Autowired //Inject a normal bean
private TestBeanComponent testBeanComponent;
@Autowired //Inject a proxy
private TestBeanConfiguration testBeanConfiguration;
public void test() {
//Calling anotherBeanComponent always return a new instance of AnotherBeanComponent
testBeanComponent.anotherBeanComponent()
.equals(testBeanComponent.anotherBeanComponent()); // is false
//Calling anotherBeanConfiguration return the bean managed by the container
testBeanConfiguration.anotherBeanConfiguration()
.equals(testBeanConfiguration.anotherBeanConfiguration()); // is true
}
}
但是如果您是注入 bean 而不是使用方法,一切都会按您预期的那样工作:
@Service
public class TestService2 {
@Autowired //Inject a proxy with scope request
private AnotherBeanComponent anotherBeanComponent;
@Autowired //Inject a proxy with scope request
private AnotherBeanConfiguration anotherBeanConfiguration;
}
这 website 表示在组件 classes 中注册的 bean 没有被 cglib 代理,并且不通过 spring 容器。那么这是否意味着如果我在组件 class 中注册一个 bean(下面的片段),添加 @Scope("request") 不会有任何区别,并且 AnotherBean
的新实例将始终是每当从某些外部 class?
testBean.anotherBean()
时创建
@Component
public class TestBean {
@Bean
@Scope("request")
public AnotherBean anotherBean() {
return new AnotherBean();
}
}
未被 cglib 代理的 bean 是 @Component
本身,而不是使用 @Bean
注释注册的 bean。如果您没有显式调用 anotherBean 方法,则不会有什么不同,因为在调用带有 @Bean
注释的方法时代理用于 return bean。看例子
bean testBeanComponent
不是 cglib 代理:
@Component
public class TestBeanComponent {
@Bean
@Scope("request")
public AnotherBeanComponent anotherBeanComponent() {
return new AnotherBeanComponent();
}
}
bean testBeanConfiguration
是 cglib 代理的:
@Configuration
public class TestBeanConfiguration {
@Bean
@Scope("request")
public AnotherBeanConfiguration anotherBeanConfiguration() {
return new AnotherBeanConfiguration();
}
}
什么意思:
@Service
public class TestService {
@Autowired //Inject a normal bean
private TestBeanComponent testBeanComponent;
@Autowired //Inject a proxy
private TestBeanConfiguration testBeanConfiguration;
public void test() {
//Calling anotherBeanComponent always return a new instance of AnotherBeanComponent
testBeanComponent.anotherBeanComponent()
.equals(testBeanComponent.anotherBeanComponent()); // is false
//Calling anotherBeanConfiguration return the bean managed by the container
testBeanConfiguration.anotherBeanConfiguration()
.equals(testBeanConfiguration.anotherBeanConfiguration()); // is true
}
}
但是如果您是注入 bean 而不是使用方法,一切都会按您预期的那样工作:
@Service
public class TestService2 {
@Autowired //Inject a proxy with scope request
private AnotherBeanComponent anotherBeanComponent;
@Autowired //Inject a proxy with scope request
private AnotherBeanConfiguration anotherBeanConfiguration;
}