【发布时间】:2013-02-15 21:10:12
【问题描述】:
我有两个spring配置类A和B。B中的bean使用@import注解导入A中,像这样
@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 为空。 dataSource bean 不断在循环中实例化。
【问题讨论】:
-
您确定
@Autowired不起作用吗?我在这个场景中成功使用了它,除了我在组件扫描期间引用了自动发现的 bean。 -
@Autowired 应该可以工作,我在几个应用程序中使用它和相同的方案。
-
re:您的更新,如果您发现 Spring 在创建 bean 工厂时挂起,我会尝试设置一些断点并附加一个调试器以找出它挂起的确切位置。什么版本的Spring?如果您在 IDE 中运行测试,是否会看到与从命令行使用
mvn运行测试相同的行为? -
真正挂的是mavent。附加调试器会有所帮助。但是,是的,当我使用 Debug As -> TestNG Test 从 Eclipse 运行测试时,我看到了相同的行为
-
这个问题解决了吗?我现在也有类似的问题。没有挂起,但通过调试我可以看到初始化顺序很时髦。我有用@Autowired注释的DataSource,但它似乎只在我在主配置中实际需要它之后才在导入的配置中初始化。这特别是jUnit测试的问题,但我希望在生产中具有相同的行为。有人有什么技巧吗?
标签: java spring configuration annotations