Deepak  D
Deepak D

Reputation: 31

Spring order of @component creation

In what order the @Component classes will be created in spring. is it @configuration annotated class that will be created first ?? Can we specify the order of creation ??

Upvotes: 2

Views: 3312

Answers (1)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29286

@Component and @Configuration are different types of annotations.

@Component and similar annotations (@Service, @Repository, etc. ) and its JSR-330 counterpart and allow you to declare beans that are to be picked up by autoscanning with <context:component-scan/> or @ComponentScan they register the bean definition for the classes, so they are roughly equivalent to declaring the specified beans with the <bean ... /> tag in XML. This bean types will adhere to the standard proxy creation policies.

@Configuration annotation was designed as the replacement of the XML configuration file. To create @Configuration annotated beans, Spring will always use CGLIB to subclass the @Configuration annotated class, overriding its @Bean annotated method to replace it with the bean lookup method to make singleton beans to be created only once. Despite that, @Configuration annotated classes are still able to use annotated(@Autowired, @Inject etc.) fields and properties to request beans (and even other @Configuration annotated beans too) from the container.

Now answer to your question, you have to annotate the class with @Configuration and then with @ComponentScan(basePackages = { "com.test.*" }) and you can't specify the order of creation.

Upvotes: 1

Related Questions