春:基于注解配置

春:基于注解配置

问题描述:

我有两个春天配置类A和B. B中豆类正在使用的@import注解在一个进口的,这样春:基于注解配置

@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();   
    } 
} 

期间@import文件参考豆我会怎样在创建bean aBean时引用bean bBean?有人会认为@Required或@Autowired会在这里工作,但它不会:(

更新 - 我在这里要做的是使用TestNG和maven运行单元测试当我试图引用'自动装配Autowired”豆,行家挂起,可能在一个无限循环或等待要加载的豆

------------------------------------------------------- 
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 [email protected]d6a56e: 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.s[email protected]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 

UPDATE - 真正的代码在这里

@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; 
} 

豆子propertiesCon无花果在CommonConfig中定义。由于propertiesConfig为null,因此突出显示的行上出现invocationTargetException。 dataSource bean继续在循环中实例化。

+0

你确定'@ Autowired'不起作用吗?除了在组件扫描期间引用自动发现的bean之外,我在这种情况下成功使用它。 – 2013-02-15 21:16:06

+0

@Autowired应该可以工作,我在几个应用程序中使用它和相同的方案。 – 2013-02-15 21:17:22

+0

re:你的更新,如果你发现Spring在创建bean工厂的时候挂起了,我会尝试设置一些断点并附上一个调试器来找出它确切地挂在哪里。什么版本的Spring?如果您在IDE中运行测试,就像在命令行中使用'mvn'运行测试一样,您会看到相同的行为吗? – 2013-02-15 21:35:30

@Import说明文档中提到你应该像你描述一个情况下都使用@Autowired

进口@Configuration类中声明@Bean定义应该通过@Autowired注入进行访问。可以自动装配bean本身,或者声明该bean的配置类实例可以是自动装配的。后一种方法允许在@Configuration类方法之间进行明确的,IDE友好的导航。

这不是@Autowired导致您的问题。从你遇到的问题开始,我希望你有一个bean,它在启动时做了一些不及时完成的​​事情。由于bean的初始化永远不会结束,因此Spring无法继续启动。某种网络通信很有可能,因为除非另有特别配置,否则这些通信都有无限期悬挂而没有输出的坏习惯。当JVM挂起时拉出一个堆栈转储,并查看你的线程正在做什么。否则,请附加一个调试器,并手动暂停JVM以获得相同的效果。

+0

这确实让我走上了正轨,请参阅上面更新的问题。 – nsdiv 2013-02-19 18:05:33