Thursday, June 6, 2013

Spring Core Example with xml configuration.

Here is Simple Spring core application using Spring Version 2.5.
     Assuming that you have already gone through the my earlier Spring Introduction post.In this HelloSpringWorld example i am injecting the String to HelloSpringWorldServiceImpl object through configuration file. The below image shows my package structure.



applicationContext.xml

Below one is my spring bean configuration xml file,here i have configured the bean with the id helloMessage, the type of this bean is java.lang.String and i am passing the value visitor to this bean using the <value> tag . Here i have configured my HelloSpringWorldServiceImpl bean, with the id helloSpringWorldService , this can be any name you can give. Using this we can request to the spring container to get the Object of HelloSpringWorldService bean, here i am injecting the string bean helloMessage using attribute ref in <property> xml element. ref points to the another bean in the configuration file. Spring container creates first String object with the reference name helloMessage and then creates the bean helloSpringWorldService, and calls the setName() method of HelloSpringWorldServiceImpl class to set the value visitor using IOC.

<?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: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">

<bean id="helloMessage" class="java.lang.String">
<constructor-arg index="0" value="visitor" />
</bean>
<bean id="helloSpringWorldService" class="com.lova.spring.service.impl.HelloSpringWorldServiceImpl">
<property name="name" ref="helloMessage" />
</bean>
</beans>

HelloSpringWorldService.java

This is my HelloSpringWorldService interface, sayHelloService() method returns String.

package com.lova.spring.service;
/**
* @author Lovababu.P
*/
public interface HelloSpringWorldService {
public String sayHelloService();
}

HelloSpringWorldServiceImpl.java

This is my implementation class for the HelloSpringWorldService interface. Here i have declared one private property name and corresponding setter method. This property name i have configured into configuration file to inform to the spring container that the value visitor should be set into this property name.

package com.lova.spring.service.impl;
import com.lova.spring.service.HelloSpringWorldService;
/**
* @author Lovababu.P
*/
public class HelloSpringWorldServiceImpl implements HelloSpringWorldService
{
private String name;
public void setName(String name)
{
this.name = name;
}
@Override
public String sayHelloService()
{
return "Hello dude! "+name + " Welcome to Spring world.";
}
}

HelloSpringWorldTest.java

Finally, this is my test program, here i am using ApplicationContext to load my bean configuration file. Once we get the context object, we can call getObject(String id) method by passing desired bean id (in my example id is helloSpringWorldService). The return type of the getObject() method is java.lang.Object, so we need to do explicit type casting to get the our bean object.

package com.lova.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lova.spring.service.HelloSpringWorldService;
import com.lova.spring.service.impl.HelloSpringWorldServiceImpl;
/**
* @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
  • commons-collections-3.2.jar
  • commons-logging-1.1.1.jar
  • log4j-1.2.16.jar
  • commons-attributes-api.jar
  • commons-attributes-compiler.jar

Woh..! we finished our Spring2.5 Core example, to know more about what is Spring IOC and Dependency Injection Click here.
Thank You. Have a nice day :)

No comments:

Post a Comment

UA-41474183-1