Spring Framework : A Framework from SpringSource :: Spring First Example
- Normal Java program Without Spring
- Java program With Spring
- How to do Spring program With My Eclipse ( Step by Step)
- Download Examples
Normal Java program Without Spring |
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
TestDrive.java
package com.javaskool;
public class TestDrive {
public static void main(String[] args)
{
Item p=new Item();//Creating Object using New Operator
p.setItemId(205);
p.setItemName("Mouse");
p.setQty(40);
System.out.println(p.getItemId());
System.out.println(p.getItemName());
System.out.println(p.getQty());
}
}
Output
205
Mouse
40
Java program With Spring |
Item .java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.Item">
<property name="itemId" value="1001"/>
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
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 TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Item p=(Item)beans.getBean("obj"); //getting Object from BeanFactory
System.out.println(p.getItemId());
System.out.println(p.getItemName());
System.out.println(p.getQty());
}
}
Output
1001
Mouse
500
How to do Spring program With My Eclipse (Step by Step) |
Click Below to download the Examples
Recent Comments