Saturday, June 8, 2013

Spring @Autowired.

      Assuming that you have already gone through the my previous post about @Autowired. lets continue the discussion about @Autowired. In this example i am going to explain how many ways we can use @Autowired annotation. It Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. In my previous post we have discussed field level, now lets discuss constructor level, method level and setter method level. My project package structure remains same.

Spring @Autowired package structure.

     In this example I am going to autowire three different String messages into the my HelloSpringWorldServiceImpl bean. Just have quick look at my bean configuration file.

applicationContext.xml:

     This is my bean configuration xml is similar to my previous post. But here i configured three different java.lang.String bean with different ids. Each id i am going to use in my HelloSpringWorldServiceImpl class.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  
<context:component-scan base-package="com.lova.springcore"/>
<bean id="fieldMessage" class="java.lang.String">
<constructor-arg index="0" value="Hello dude." />
</bean>
<bean id="constructorMessage" class="java.lang.String">
<constructor-arg index="0" value="amazing spring fw" />
</bean>
<bean id="methodMessage" class="java.lang.String">
<constructor-arg index="0" value="its working as we expected." />
</bean>
<bean id="helloSpringWorldService"
class="com.lova.springcore.service.impl.HelloSpringWorldServiceImpl">
</bean>
</beans>

HelloSpringWorldService.java:

  package com.lova.springcore.service;

  /**
   * @author Lovababu.P
   **/
  public interface HelloSpringWorldService 
  {
    public String sayHelloService();
  }

HelloSpringWorldServiceImpl.java:

        This is my actual implementation class for above interface. Just observe very care fully, in this class i am using @Autowired in three different places. 
  1. On top of the field, you already known this.
  2. On top of the constructor.
  3. On top of the Method(it can be any setter method or Normal method).
     If you place @Autowired on field level spring container will automatically sets the value for this field using spring's dependency injection, here the field name should be the same as id name from configuration file and data type also must be match both field type and bean type.

      If you place @Autowired on Constructor level, spring container will instantiates the your bean by passing appropriate bean reference from the container, in my example spring container create the HelloSpringWorldServiceImpl bean by passing the reference bean with the name constructorMessage. here name of the Argument name in constructor must be same as Id from configuration file.

      If you place @Autowired on any method level, spring container will call the method by passing the appropriate bean reference after you bean instantiated successfully. In my example container invokes the method printMessage(..) by passing the bean reference methodMessage from container.  Here the method argument name must be same as the Id from configuration file.

        Note: if you configured only one String bean in your configuration file, by default spring container will inject the same bean into all the dependencies like field, method and constructor. just comment the two String bean configuration in configuration xml and try to run the program, then you will see spring container magic.


package com.lova.springcore.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import com.lova.springcore.service.HelloSpringWorldService;

/**
 * @author Lovababu.P
 **/
public class HelloSpringWorldServiceImpl implements HelloSpringWorldService 
{
@Autowired
private String fieldMessage;
private String methodMessage;
private String constructorMessage;
@Autowired //constructor level
private HelloSpringWorldServiceImpl(String constructorMessage)
           {
this.constructorMessage = constructorMessage;
}
@Autowired //method level
public void printMessage(String methodMessage)
            {
this.methodMessage = methodMessage;
}

@Override
public String sayHelloService() 
{
System.out.println("Field level     @Autowired annotation is      :"+fieldMessage);

System.out.println("Constructor level @Autowired annotation is:"+constructorMessage);

System.out.println("Method level  @Autowired annotation is     :"+methodMessage);

return "Keep Learning.. :)";
}
 }

HelloSpringWorldTest.java:

package com.lova.springcore.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lova.springcore.service.HelloSpringWorldService;
public class HelloSpringWorldTest
{
public static void main(String[] args)
{

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

HelloSpringWorldService service = (HelloSpringWorldService) context.getBean("helloSpringWorldService");

System.out.println("HelloSpringWorldTest.main() > " + service.sayHelloService());
}
}
Required Jars:

  • org.springframework.beans-3.1.1.RELEASE.jar
  • org.springframework.context-3.1.1.RELEASE.jar
  • org.springframework.core-3.1.1.RELEASE.jar
  • org.springframework.expression-3.1.1.RELEASE.jar
  • commons-collections-3.2.jar
  • commons-logging-1.1.1.jar
  • log4j-1.2.16.jar
  • org.springframework.asm-3.1.1.RELEASE.jar.

 Thank you.. Have a nice day.. :)

1 comment:

  1. Love your explanations of concepts. Whenever I need a visual note in my head of a concept, I always turn to your posts. I think it is genius to be able to say in such simple terms such convoluted topics. Keep posting!

    ReplyDelete

UA-41474183-1