First Java Spring Program using Eclipse
First Java Spring Program using Eclipse
Let us see First Java Spring program using Eclipse.
File -> New -> Project…
Java Project
Project Name : Piyush Spring Welcome
Right Click on src & Select New Package
Package Name : com.piyush
Right Click on com.piyush & Select New Java File
File Name: Welcome.java & myMain.java
Right Click on src & Select XML File
File Name: myBean.xml
Right Click on project name & Select Properties
Go to Java Build Path
Click on Add External Jar File
Add 2 Jar file : spring.jar and common-logging.jar
Your Project structures look like below.
Welcome.java
package com.piyush;
public class Welcome {
private String msg;
public void setMessage(String msg) {
this.msg = msg;
}
public void show() {
System.out.println(msg);
}
}
myMain.java
package com.piyush;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.core.io.ClassPathResource;
import
org.springframework.core.io.Resource;
public class myMain {
public static void main(String[] args) {
Resource
res = new ClassPathResource("myBean.xml");
BeanFactory
factory = new XmlBeanFactory(res);
Object
A = factory.getBean("id1");
Welcome
wb = (Welcome) A;
wb.show();
}
}
myBean.xml
<!DOCTYPE beans
PUBLIC "-//SPRING//DTD
BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="id1"
class="com.piyush.Welcome">
<property name="message"
value="Welcome To Spring Framework" />
</bean>
</beans>
Right Click on project name & Select Run As -> Java
Application
Output:
Welcome To Spring Framework
Comments
Post a Comment