Saturday, June 8, 2013

Spring @Autowired Example.

        Assuming that you have already gone through the my previous post. In this Post i am going to discuss about @Autowired annotation how it will work. Just i did small code changes to my previous example. Packaging structure remains same.


applicationContext.xml

         This is my spring beans configuration file, just slight difference to my earlier post configuration file. Here i configured my bean HelloSpringWorldServiceImpl with the id helloSpringWorldService , but i haven't given any reference to the other bean but still the helloMessage bean will getting injected into my HelloSpringWorldServiceImpl bean, HOW? just look at the calls HelloSpringWorldServiceImpl.java.
Here if you observe carefully i have introduced new xml element context:component-scan, this will inform to the Spring container scan for any annotations under the specified base-package.

<?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="helloMessage" class="java.lang.String">
  <constructor-arg index="0" value="visitor" />
</bean>

<bean id="helloSpringWorldService"
  class="com.lova.springcore.service.impl.HelloSpringWorldServiceImpl">
</bean>

</beans>

HelloSpringWorldService.java

          Here is my interface HelloSpringWorldService.java.

   package com.lova.springcore.service;

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


HelloSpringWorldServiceImpl.java

         Here is my Implementation class for the above interface. Here you cant see any setter method for the property helloMessage, but still Spring container is able to set the value to this property using autowiring. Please note one thing here, here property name must be the same as id of the bean which we have already configured in beans xml.

 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 helloMessage;
@Override
public String sayHelloService() 
{
return "Hello dude! " + helloMessage + " Welcome to Spring world.";
}
 }


HelloSpringWorldTest.java

      Finally, Here is my test java class. similar to my earlier post.

package com.lova.springcore.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lova.springcore.service.HelloSpringWorldService;

/**
* @author Lovababu.P
*/
;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 Artifacts(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. I hope you understood, Have a nice day. :)

No comments:

Post a Comment

UA-41474183-1