0
我曾經使用JSR 330 @Inject註釋來自動裝入我的Spring bean。我開始嘗試刪除@Inject註釋 - 但我的應用程序上下文仍然正確加載。不知道這是否是預期的,無法找到任何春季文檔來驗證此用例。JavaConfig「自動」注入beans
// This context is loaded correctly - and beans exist for B, C and Db
final ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
@Import({ B.class })
@Configuration
public class ApplicationConfig {
@Bean
public Db db() {
return new Database();
}
@Bean
// I thought this method would need an @Autowire or @Inject annotation to resolve b!?
public C c(final B b){
return new C(b);
}
}
@Configuration
public class BConfig {
@Bean
public B b() {
return new B();
}
}
是有意義的 - 並與我所看到的有聯繫。真的很想看到一些文檔,雖然這樣做.. Configuration或Bean根本沒有提到這種行爲。 – Damo
我不認爲這真的被認爲是最佳做法(使代碼有點脆弱)。但我已經看到了足夠多的例子知道這很常見。您可以在Spring中始終提出文檔請求。 –