Pages

Friday, February 24, 2017

Annotation Based Autowiring in Spring IoC

Annotation Based Autowiring in Spring IoC


Auto-wiring in spring can also be achieved through Annotation. Annotation is the simplest way to achieve wiring in Spring as it doesnt involve any extra code in XML file. It can be parsed by the Spring container at the time of reading the bean class. An the strange fact is that annotation auto-wiring doesnt use any of the constructor or setter injection for injection the bean property else it uses its own way to inject the bean property. But bean will be detected using the byType process. Annotation can be used with the bean properties, setter method and constructor.

@Autowired is the annotation can be used with the bean properties, constructor and Setter method.

1. Annotation on properties
2. Annotation on constructor
3. Annotation on Setter
4. Annotation on properties where ( required = false )

Note : By default annotation in Spring is disabled, we have to enable it manually by modifying the XML file.

For Enabling Annotation in Spring 

Why Annotation is by default disable in Spring ?
It is because in Spring all the functionality is not supported by annotation rather it is possible with using XML file.


Available link for download