来自外部 api 的 @Autowiredl bean 总是 nul
@Autowiredl bean from external api always nul
我有一个外部罐子。里面有个AnnotationConfigApplicationContext。
@Configuration
@ComponentScan{~~~~~}
public A_config() {
@Bean
~~~~~~
}
某处
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext ();
ctx.register(A_config());
我有另一个引用外部 jar 的应用程序。
此应用还有
@Configuration
@ComponentScan{~~~~~}
public B_config() {
@Bean
~~~~~~
}
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext ();
ctx.register(A_config());
ctx.register(B_config());
当我在外部 jar 中使用 ctx.getBean(some.class), some.class 时,没问题。
但是我在外部 jar 中自动装配了字段 bean,它总是空的。
您应该将在外部 jar 中定义的 @Configuration
导入到应用程序的 @Configuration
中,如下所示:
@Import(A_config.class)
@Configuration
@ComponentScan{~~~~~}
public B_config() {
@Bean
~~~~~~
}
我有一个外部罐子。里面有个AnnotationConfigApplicationContext。
@Configuration
@ComponentScan{~~~~~}
public A_config() {
@Bean
~~~~~~
}
某处
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext ();
ctx.register(A_config());
我有另一个引用外部 jar 的应用程序。 此应用还有
@Configuration
@ComponentScan{~~~~~}
public B_config() {
@Bean
~~~~~~
}
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext ();
ctx.register(A_config());
ctx.register(B_config());
当我在外部 jar 中使用 ctx.getBean(some.class), some.class 时,没问题。 但是我在外部 jar 中自动装配了字段 bean,它总是空的。
您应该将在外部 jar 中定义的 @Configuration
导入到应用程序的 @Configuration
中,如下所示:
@Import(A_config.class)
@Configuration
@ComponentScan{~~~~~}
public B_config() {
@Bean
~~~~~~
}