我有两个弹簧配置类A和B.使用@import注释在A中导入B中的Bean,就像这样
@Configuration
@Import({B.class})
public class A {
private BBean bbean;
@Bean
public ABean aBean() {
// need to reference B's bean over here
return aBean()// after referencing B's bean
}
}
@Configuration
public class B {
@Bean
public BBean bBean(){
return new BBean();
}
}
在创建bean aBean时如何引用bean bBean?有人会认为@Required或@Autowired会在这里工作,但它没有:(
更新 - 我在这里尝试做的是使用TestNG和maven运行单元测试。当我尝试引用'Autowired'bean时,maven挂起,可能在无限循环中或等待加载bean。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
13:15:42,427 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [META-INF/spring/app-context.xml]
13:15:42,589 INFO nnotation.ClassPathBeanDefinitionScanner: 210 - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
13:15:42,671 INFO ontext.support.GenericApplicationContext: 495 - Refreshing org.springframework.context.support.GenericApplicationContext@45d6a56e: startup date [Fri Feb 15 13:15:42 PST 2013]; root of context hierarchy
13:15:42,769 INFO ctory.support.DefaultListableBeanFactory: 623 - Overriding bean definition for bean 'dataSource': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=repositoryConfig; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/abc/pagg/ddee/repository/config/RepositoryConfig.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=serviceConfig; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/abc/pagg/ddee/service/config/ServiceConfig.class]]
13:15:42,983 INFO ion.AutowiredAnnotationBeanPostProcessor: 139 - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
13:15:43,027 INFO ctory.support.DefaultListableBeanFactory: 557 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@66b51404: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,serviceConfig,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,commonConfig,propertiesConfig,repositoryConfig,iusRestManager,localCacheClientAdapter,memcachedClientAdapter,s3Adapter,dataSource,userMapper]; root of factory hierarchy <<--- hangs right here
更新 - 此处为真实代码
@Configuration
@Import({CommonConfig.class})
public class ServiceConfig {
private final Logger log = LoggerFactory.getLogger(ServiceConfig.class);
private org.apache.commons.configuration.Configuration propertiesConfig;
@Autowired
public void setPropertiesConfig(org.apache.commons.configuration.Configuration propertiesConfig){
this.propertiesConfig = propertiesConfig;
}
public ServiceConfig() {
}
@Bean
public DataSource dataSource() {
final BasicDataSource dataSource = new BasicDataSource();
---> dataSource.setDriverClassName(propertiesConfig.getString("jdbc.driver")); <----
//dataSource.setUrl(propertiesConfig.getString("jdbc.url"));
//dataSource.setUsername(propertiesConfig.getString("jdbc.username"));
//dataSource.setPassword(propertiesConfig.getString("jdbc.password"));
return dataSource;
}
bean propertiesConfig在CommonConfig中定义。我在突出显示的行上得到一个invocationTargetException,因为propertiesConfig为null。 dataSource bean继续在循环中实例化。
答案 0 :(得分:2)
@Import
的文档提到您应该在您描述的情况下使用@Autowired
:
应在@Autowired注入中访问在导入的@Configuration类中声明的@Bean定义。 bean本身可以自动装配,或者声明bean的配置类实例可以自动装配。后一种方法允许在@Configuration类方法之间进行显式的,IDE友好的导航。
答案 1 :(得分:0)
导致问题的不是@Autowired
。从那里你体验到了悬挂,我希望你有一个bean在启动时做一些没有及时完成的东西。由于bean的初始化永远不会完成,Spring无法继续启动。某种类型的网络通信是最有可能的,因为除非另外特别配置,否则它们有无限期挂起而没有输出的坏习惯。挂起JVM时拉出堆栈转储,看看你的线程在做什么。否则,请附加调试器,并手动暂停JVM以获得相同的效果。