Java Spring:在基于注释的配置期间,在@import文件中引用bean

Java Spring:在基于注释的配置期间,在@import文件中引用bean,java,spring,configuration,annotations,Java,Spring,Configuration,Annotations,我有两个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

我有两个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运行单元测试。当我尝试引用“自动连线”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
-------------------------------------------------------
T T S T S
-------------------------------------------------------
运行测试套件
13:15:42427 INFO-eans.factory.xml.XmlBeanDefinitionReader:315-从类路径资源[META-INF/spring/app-context.xml]加载xml bean定义
13:15:42589 INFO notation.ClassPathBeanDefinitionScanner:210-JSR-330“javax.inject.Named”找到并支持组件扫描的注释
13:15:42671 INFO ontext.support.GenericApplicationContext:495-刷新org.springframework.context.support。GenericApplicationContext@45d6a56e:启动日期[Fri Feb 15 13:15:42 PST 2013];上下文层次结构的根
13:15:42769 INFO-ctory.support.DefaultListableBeanFactory:623-重写bean“dataSource”的bean定义:替换[Root bean:class[null];scope=;abstract=false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=repositoryConfig;factoryMethodName=dataSource;initMethodName=null;destroyMethodName=(推断);在类路径资源[com/abc/pagg/ddee/repository/config/repositoryConfig.class]]中定义,带有[Root bean:class[null];scope=;abstract=false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=serviceConfig;factoryMethodName=dataSource;initMethodName=null;destroyMethodName=(推断);在类路径资源[com/abc/pagg/ddee/service/config/serviceConfig.class]中定义
13:15:42983信息ion.AutowiredNotationBeanPostProcessor:139-JSR-330“javax.inject.inject”注释已找到并支持自动连线

13:15:43027 INFO-ctory.support.DefaultListableBeanFactory:557-在org.springframework.beans.factory.support中预实例化单例。DefaultListableBeanFactory@66b51404:定义bean[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,serviceConfig,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor#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、MemcachedClient适配器、s3Adapter、数据源、userMapper];工厂层次结构的根导入
@Import
的文档提到,在您描述的情况下,应使用
@Autowired

@导入的@Configuration类中声明的Bean定义应使用@Autowired注入进行访问。Bean本身可以是自动连接的,或者声明Bean的配置类实例可以是自动连接的。后一种方法允许在@Configuration类方法之间进行显式、IDE友好的导航


问题不是由
@Autowired
引起的。从您遇到的挂起情况来看,我希望您有一个bean在启动时做了一些事情,但没有及时完成。由于bean的初始化从未完成,Spring无法继续启动。很有可能是某种网络通信,因为这些bean的性能不好习惯无限期挂起,没有输出,除非另有特别配置。挂起JVM时拉一个堆栈转储,看看线程在做什么。否则,附加一个调试器,并手动暂停JVM以获得相同的效果。

你确定
@Autowired
不起作用吗?我在这个场景中成功地使用了它,除了在组件扫描期间引用自动发现的bean。@Autowired应该可以工作,我在几个应用程序中使用它和相同的方案。re:您的更新,如果您在创建bean工厂时发现Spring挂起,我会尝试设置一些断点并附加一个调试器,以查明它挂起的确切位置。Spring的哪个版本?您看到了吗e如果在IDE中运行测试,就像在命令行中使用
mvn
运行测试一样,会出现相同的行为吗?实际上是mavent挂起了。附加调试器会有帮助。但是,是的,当我使用Debug as->TestNG TestNG从Eclipse运行测试时,我看到了相同的行为。这有没有解决过?我现在有一个类似的问题。没有挂起,但是通过调试我可以看到init顺序很奇怪。我用@Autowired注释了数据源,但它似乎只有在我在主配置中实际需要它之后才能在导入的配置中初始化。这是jUnit测试的一个问题,但我希望在生产中也会出现同样的行为。有人有什么窍门吗?这确实让我对e正确的轨道,请参阅上面更新的问题。
@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;
}