I know springs AnnotationConfigApplicationContext
is capable of accepting not only @Configuration
classes as input but also plain @Component
classes and classes annotated with JSR-330 metadata.
I have created AppConfig.java below without @Configuration annotation.
public class AppConfig {
@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}
Passed this class as my java config class to AnnotationConfigApplicationContext
, it accepted and registered my service beans.
I did some modification on above same AppConfig like below.
@Component
public class AppConfig {
@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}
passed AppConfig to AnnotationConfigApplicationContext, it accepted and registered my service beans.
Question:
AnnotationConfigApplicationContext
class is accepting the java config class with@Configuration
, without@Configuration
and with @Component annotations, what is the difference between@Component
and@Configuration
?Why is it Accepting even without
@Configuration
annotation?When to use
@Configuration
, and when to use @Component as java config class?