Annotation based auto wiring in Spring framework

In this article, we will learn about the annotation based auto wiring in spring framework. If you want to use the annotations, you need to do the following. You must enable context namespace. Add <context:annotation-config/>.  Now you can use @Autowired annotation for the beans.
Example 1:

public class Hai{ }
publuc class A { }
public class Hello {
  @Autowired
  A aobj;

  @Autowired
  Hai hai;
}
<bean id=”ha” class=”...Hai”/>
<bean id=”ao” class=”...A”/>
<bean id=”hello” class=”...Hello”/>

Example 2:

Interface CustomerDao{ }
class JDBC customer Dao imp customer Dao { }
class Customer
Service {
  @Autowired
customer Dao cdao=null;
}
<bean id=”xcdao” class=”...JDBCCustomerDAO”/>
<bean id=”cs” class=”...CustomerService”/>

The important point as follows:

  • When you use @Autowired, then by default, beans will be detected based on by Type process and inject them.
  • By default, its functionality is the same as auto-wire =”byType”
  • When you want to detect the bean based on byName process then you need to use @Qualifier Annotation along with @Autowired.
  • When you use autowire=”byType” then beans will be detected based on dataType and injects using setter methods whereas when you use @Autowired then bean will be detected based on Data Type and injects without setter methods.
  • When you use autowire=” byName” then beans will be detected based on bean name and injects using setter methods whereas when you use @Autowired & @Qualifier then beans will be detected based on bean and injects without setter methods.

For Example:

class Hai { }
class Hello {
@Autowired
@Qualifier(“hai”)
Hai hai;
....
}
<bean id=”hai1” class=”....Hai”/>
<bean id=”hai2” class=”....Hai”/>
<bean id=”hello” class=”....hello”/>

A full example for Annotation based auto wiring:
Files required:

  1. Xadmin.java
  2. A.java
  3. B.java
  4. Hello.java
  5. xadmin.xml

xadmin.java

Package com.xadmin.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXml.ApplicationContext
public class xadmin
{
public static void main( String args[])
{
ApplicationContext ctx= new ClassPathXmlApplicationContext(“xadmin.xml”);
Hello hello= (Hello)ctx.getBean(“hello”);
Hello.show();
}
}

A.java

package com.xadmin.spring;
public class A
{
private int a;
private string msg;
public void seta(int a)
{
This.a=a;
}
public void setMsg(string msh)
{
This.msg=msg;
}
public String toString()
{
Return “”+a+”\”+msg;
}
}

B.java

Package com.xadmin.spring;
public class B
{
private int b;
private string str;
public void seta(int b, String str)
{
This.b=b;
This.str= str;
}

public String toString()
{
return “”+b+”\t”+str;
}
}

Hello.java

package com.xadmin.spring;
import org.springframework.beans.factory.annotation.Autowired.
public class Hello {
  @Autowired
  private A aobj;

  @Autowired
  private B obj;

public void show(){
system.out.println(aobj);
system.out.println(bobj);
}

Xadmin.xml

<context:annotation-config/>
<bean id=”ao” class=”com.xadmin.spring.A” p.a=”10” p:msg=”AAA”/>
<bean id=”bo” class=”com.xadmin.spring.B”>
<constructor-arg value=”20”/>
<constructor-arg value=”BB”/>
<bean>
<bean id=”hello” class=”com.xadmin.spring.Hello”/>
<beans>

Example spring program using @Qualifier annotation
Files required:

  1. Xadmin.java
  2. A.java
  3. B.java
  4. Hello.java
  5. xadmin.xml

consider, Xadmin.java, A.java, B.java files same as the previous example.
Hello.java

package com.xadmin.spring;
import org.springframework.beans.factory.annotation.Autowired.
import org.springframework.beans.factory.annotation.Qualifier.
public class Hello {
  @Autowired
  @ Qualifier(“ao1”)
  private A aobj;

  @Autowired
  @ Qualifier(“bo1”)
  private B obj;

public void show(){
system.out.println(aobj);
system.out.println(bobj);
}
}

Xadmin.xml

<beans>
<context:annotation-config/>
<bean id=”ao1” class=”com.xadmin.spring.A” p.a=”10” p:msg=”A10”/>
<bean id=”ao2” class=”com.xadmin.spring.A” p.a=”20” p:msg=”A20”/>
<bean id=”bo1” class=”com.xadmin.spring.B”>
<constructor-arg value=”99”/>
<constructor-arg value=”B99”/>
<bean>
<bean id=”bo2” class=”com.xadmin.spring.B”/>
<constructor-arg value=”88”/>
<constructor-arg value=”B88”/>
</bean>
<bean>
<bean id=”hello” class=”com.xadmin.spring.hello”/>
<beans>

Using JSR-250 Annotations
Following annotations provided in Javax.annotation package

  1. @PostConstruct
  2. @PreDestroy
  3. @Resource

@PostConstruct:

  • You can mark any method with @PostConstruct annotation to call that method at the time of loading of the call. All initialization is done in this method.

@PreDestroy:

  • You can mark any method with @PreDestroy annotation to call that method before destroying the instance
  • The method which is marked with @PreDestroy contains the code for cleaning the resources initialized with bean instance.

@Resource:

  • When you use @Resource, then bean will be detected either based on byName or byType process and inject them.

When name attribute is specified for @Resource then uses byName process.
When name attribute is not specified for @Resource then uses byType process.

Example1

class Hai { }
class A { }
@Resource
A aobj;

@Resource (name=”ha”)
Hai hai;
...
}
<bean id=”ha” class=”....Hai”/>
<bean id=”ao” class=”....A”/>
<bean id=”hello” class=”....Hello”/>

Example 2:

interface Customer Dao{ }
classs JDBCCustomerDAO imp CustomerDao{ }
classs HibernateCustomerDAO imp CustomerDao{ }

class Customer Service{ 
@Resource(name=”bcdao”}
customerDao cdao=null;
..
}
<bean id=”jcdao” class=”...JDBCCustomerDAO”/>
<bean id=”hcdao” class=”..HibernateCustomerDAO”/>
<bean id=”cs” class=”...CustomerService”/>

Conclusion:
In this article, we learned the annotation based auto wiring in spring framework with an example program and also learn the use of  JSR-250 Annotations. Please, feel free to drop a comment in the comment box. if you have any doubt regarding the topic or you want to share more information about the topic. Happy Learning!

 

  

Leave a Reply

Your email address will not be published. Required fields are marked *