2

私は2つのスプリング構成クラス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 を参照しようとすると、おそらく無限ループで、または Bean がロードされるのを待って、maven がハングします。

-------------------------------------------------------
 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 で定義されています。propertiesConfig が null であるため、強調表示された行で invocationTargetException が発生します。dataSource Bean は、ループ内でインスタンス化され続けます。

4

2 に答える 2

2

@Importあなたが説明したような場合に使用する必要がある言及のドキュメント@Autowired:

インポートされた @Configuration クラスで宣言された @Bean 定義には、@Autowired インジェクションを使用してアクセスする必要があります。Bean 自体を自動配線するか、Bean を宣言する構成クラス インスタンスを自動配線することができます。後者のアプローチでは、 @Configuration クラス メソッド間の明示的で IDE に適したナビゲーションが可能になります。

于 2013-02-15T21:20:01.270 に答える
0

それは@Autowiredあなたの問題の原因ではありません。ハングを経験したところから、起動時にタイムリーに終了していない何かを実行する Bean があると思います。Bean の初期化が終了しないため、Spring は起動を続行できません。特に設定しない限り、出力なしで無期限にハングアップするという悪い癖があるため、ある種のネットワーク通信が最も可能性が高いです。JVM がハングしたときにスタック ダンプを取得し、スレッドの動作を確認します。それ以外の場合は、デバッガーをアタッチし、JVM を手動で一時停止して同じ効果を得てください。

于 2013-02-16T02:48:19.747 に答える