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
<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
/**
* @author Lovababu.P
*/
public interface HelloSpringWorldService {
public String sayHelloService();
}
HelloSpringWorldServiceImpl.java
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
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
Thank You. Have a nice day :)
No comments:
Post a Comment