Spring boot Spring启动-@Autowired在@Configuration类上不起作用
我在Spring boot Spring启动-@Autowired在@Configuration类上不起作用,spring-boot,Spring Boot,我在应用程序类上使用了@ComponentScan,在我的配置类上使用了@Configuration,在我的配置类中,我想通过使用@Autowired注释注入在其他配置类中定义的bean,但是当我运行应用程序时,这些字段的值为null @Configuration public class AConfiguration { @Bean public A getA(){ return ..; } } @Configuration public c
应用程序
类上使用了@ComponentScan
,在我的配置类上使用了@Configuration
,在我的配置类中,我想通过使用@Autowired
注释注入在其他配置类中定义的bean,但是当我运行应用程序时,这些字段的值为null
@Configuration
public class AConfiguration {
@Bean
public A getA(){
return ..;
}
}
@Configuration
public class BConfiguration {
@Autowired
private A a;
@Bean
public B getB() {
**something need a, but a is null**
}
}
你可以试试这个
@Configuration
public class AConfiguration {
@Bean
public A getA(){
return ..;
}
}
@Configuration
public class BConfiguration {
@Autowired
private A a;
public B getB() {
**something need a, but a is null**
}
}
我很粗心。我有@Bean注释,我在编辑问题时忘记了。
a配置
类是否使用了b配置
中的任何Bean?@KenBekov,否,配置不使用任何bean,从bConfiguration AutowireA
到getB
方法,而不是使用成员变量Spring,这样可以更好地检测类之间的依赖关系。所以写getB(A)
。
@Configuration
public class AConfiguration {
@Bean
public A getA(){
return ..;
}
}
@Configuration
public class BConfiguration {
@Autowired
private A a;
public B getB() {
**something need a, but a is null**
}
}