Tuesday, September 15, 2015

Autowiring using Annotations





In one of my previous post I talked about XML based autowiring. Was it a hassel to configure it? If yes, then we'll look an easier way to our autowire the beans - Autowiring using Annotation.

Autowiring using Annotation can be done using several annotation methods, they are :
  • Autowired
  • Resource
  • Inject

Here we are going to discuss Autowired annotation and some other annotations. 

What's this annotation?

Annotation is a like a specifier and there are many annotations specifiers provided by Java. A basic annotation can be "@Override" specifier. 


Setting up annotation 

 

View the classes, interfaces and the XML configuration files used in this example

 

To use the annotation we'll need to add the following code to the existing configuration file. 

This code should be added to the beans tag as follows:

xmlns:context="http://www.springframework.org/schema/context"

This adds the context namespace to the XML file.

The schema location parameter too needs to be changed as follows: 

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"

The section which is highlighted is the new addition to the schema location parameter.

Finally, this last piece of code is also required to set up annotations successfully.

<context:annotation-config></context:annotation-config>

It should be added in between the beans tag.

 

Autowired Annotation

 

Here comes the long awaited section, using annotation we can reduce the XML configuration time and it's some times boring for developers to configure things up. Now let's go to the code.

package com.kasibsblog.spring.test;

import org.springframework.beans.factory.annotation.Autowired;

public class AbCompany {

    private FullTimeEmployee fulltime;
    private PartTimeEmployee parttime;

    @Autowired
    public void setFulltime(FullTimeEmployee fulltime) {
        this.fulltime = fulltime;
    }

    @Autowired
    public void setParttime(PartTimeEmployee parttime) {
        this.parttime = parttime;
    }

    public void printFullTime(String name, int salary, String company) {
        fulltime.print(name, salary, company);
    }

    public void printPartTime(String name, int salary, String company) {
        parttime.print(name, salary, company);
    }

    
In the above example, the autowired annotation is placed before the setter method, this is one way of placing the autowired annotation. We can even set the annotation to only the properties, constructor or we can mix the setter methods, properties and/or the constructor. Even we can eliminate the setter methods and constructor.

Now, we'll look how we can mix single argument constructor with a property.

 
package com.kasibsblog.spring.test;
import org.springframework.beans.factory.annotation.Autowired;


public class AbCompany {
     

    @Autowired
    private FullTimeEmployee fulltime;
    private PartTimeEmployee parttime;


    @Autowired
    public AbCompany(PartTimeEmployee parttime){
        this.partttime = parttime;
    }
    public void printFullTime(String name, int salary, String company) {
        fulltime.print(name, salary, company);
    }

    public void printPartTime(String name, int salary, String company) {
        parttime.print(name, salary, company);
    }

 

 

 



No comments :

Post a Comment