我读到了有关组件扫描的内容,据我所知,配置类是自动扫描的。我的问题是否有以下内容:
@Configuration
public class AppConfig {
@Bean(name="authenticationService")
public AuthenticationService getAuthenticationService(){
return new AuthenticationService();
}
}
如果已经扫描了@Configuration(因此应用程序配置可用),那么它内部的bean不会被创建吗?我有点困惑,因为他们说@Bean没有自动扫描
答案 0 :(得分:-1)
没有。 Spring不会扫描@Bean
方法。
在这里,您正在创建AuthenticationService
的bean,就像使用new
关键字的任何其他Java程序一样。
与AuthenticationService authenticationService = new AuthenticationService();
如果您希望spring在AuthenticationService
类中创建AppConfig
的bean,请使用@Autowired
注释
@Autowired
private AuthenticationService authenticationService;
希望这有帮助!
编辑:
@ M.Deinum纠正我,春天不会根据@Autowired
注释创建bean。如果它们的类使用@ Component / @Configuration / @Service注释注释,那么它们将由spring自动创建。
@ M.Deinum,谢谢你。