Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring framework @Configurable vs @Configuration

I seems have problem understanding these 2 annotation. I have try to read the javadocs but still cannot figure out. Can anyone help to explain with simple code about these 2 ? Thank so much in advance.

like image 355
Hieu Lam Avatar asked Sep 22 '17 10:09

Hieu Lam


People also ask

What is the difference between @configuration and @component in Spring?

The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.

What is the difference between @configuration and autoconfiguration?

You can use @EnableAutoConfiguration annotation along with @Configuration annotation. It has two optional elements, exclude : if you want to exclude the auto-configuration of a class. excludeName : if you want to exclude the auto-configuration of a class using fully qualified name of class.

What is the use of @configuration in Spring?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

What is difference between @configuration and @component?

@Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.


1 Answers

You use @Configuration as a replacement to the XML based configuration for configuring spring beans. So instead of an xml file we write a class and annotate that with @Configuration and define the beans in it using @Bean annotation on the methods.

And finally you use AnnotationConfigApplicationContext to register this @Configuration class and thus spring manages the beans defined. Small example you can find at Spring Configuration Documentaion.

Quoting from the above link

It is just another way of configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

And @Configurable is an annotation that injects dependencies into objects that are not managed by Spring using aspectj libraries. i.e., you still use old way of instantiation with plain new operator to create objects but the spring will take care of injecting the dependencies into that object automatically for you.

like image 97
Madhusudana Reddy Sunnapu Avatar answered Jan 05 '23 05:01

Madhusudana Reddy Sunnapu