我知道弹簧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