Hieu Lam
Hieu Lam

Reputation: 453

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.

Upvotes: 17

Views: 14234

Answers (2)

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.

Upvotes: 18

Andreea Craciun
Andreea Craciun

Reputation: 310

@Configuration is the heart of the Java-based configuration mechanism and provides an alternative to XML-based configuration. @Configuration classes are just like regular @Components classes, except that methods annotated with @Bean are used to factory beans.

Upvotes: 0

Related Questions