First to develop applications with the Spring framework need to setup the environment . Development requirements are-
1. Eclipse.
2.JDK 1.6.0
3.Spring framework 3.0(Click here to download).
Add the spring jar files into CLASSPATH or Eclipse Library.
1.create a simple java project.The directory structure of program as-
Create class Triangle.java
package com.test;
public class Triangle
{
public void draw()
{
System.out.println("Hello Triangle");
}
}
create class DrawApp.java(Main Class)public class Triangle
{
public void draw()
{
System.out.println("Hello Triangle");
}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawApp {
public static void main(String a[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.draw();
}
}
create Xml file spring.xmlimport org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawApp {
public static void main(String a[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.draw();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="com.test.Triangle">
</bean>
</beans>
Now run the project and output is-
Hello Triangle
Click to Download Example With Jar file
For Further Reading,
0 comments:
Post a Comment