我知道彈簧AnnotationConfigApplicationContext
不僅能夠接受@Configuration
類作爲輸入,而且還能夠接受使用JSR-330元數據註釋的類和類的普通@Component
類。Java @Component類和具有AnnotationConfigApplicationContext的@Configuration類
我在下面創建了AppConfig.java而沒有@Configuration註釋。
public class AppConfig {
@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}
通過了這個課程作爲我的java配置類AnnotationConfigApplicationContext
,它接受和註冊我的服務豆。
我在如下的相同AppConfig上做了一些修改。
@Component
public class AppConfig {
@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}
將AppConfig傳遞給AnnotationConfigApplicationContext,它接受並註冊我的服務bean。
問:
AnnotationConfigApplicationContext
類是接受與@Configuration
java的配置類,沒有@Configuration
與@Component註解,就是@Component
和@Configuration
之間的區別?爲什麼即使沒有
@Configuration
註解也接受?何時使用
@Configuration
以及何時使用@Component作爲java配置類?
謝謝德利曼拿利斯,現在我很清楚。 – Lovababu 2014-10-04 13:17:53