Annotation based Configuration in spring

This is 15 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

There are two ways via which you can inject dependency in spring

  1. By configuring XML.
  2. By using annotation.

In all our previous posts,we have injected dependency by configuring XML file but instead of doing this,we can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

You might think what if you have done both i.e.used annotations and XML both.In that case,XML configuration will override annotations because XML configuration will be injected after annotations.

Now annotations based configuration is turned off by default so you have to turn it on by entering into spring XML file.

ApplicationContext.xml


Now you are ready to use annotations in your code.Let us discuss few important annotations in spring

@Required:

Β The @Required annotation applies to bean property setter methods.

@Autowired:

The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.

Β @Qualifier:

The @Qualifier annotation along with @Autowired can be used to remove the confusion by specifiying which exact bean will be wired.

Β JSR 250 Annotations:Β 

Spring supports JSR-250 based annotations which include @Resource, @PostConstruct and @PreDestroy annotations.

12 Comments

  1. raven July 10, 2013
  2. Dharmendra July 13, 2013
  3. Divakar November 13, 2013
  4. dangle April 24, 2014
  5. yangbo October 10, 2014
  6. dfg December 3, 2014
  7. Iccy June 25, 2015
  8. Kanishka September 4, 2015
  9. Kanishka September 4, 2015
  10. John Michael Pineda January 18, 2016

Add Comment