创建Java类
创建xml文件以提供值
创建测试类
加载spring jar文件
运行测试类
package com.lidihuo; public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void displayInfo(){ System.out.println("Hello: "+name); } }
<?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-3.0.xsd"> <bean id="studentbean" class="com.lidihuo.Student"> <property name="name" value="Vimal Jaiswal"></property> </bean> </beans>
package com.lidihuo; 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 Test { public static void main(String[] args) { Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(resource); Student student=(Student)factory.getBean("studentbean"); student.displayInfo(); } }
org.springframework.core-3.0.1.RELEASE-A
com.springsource.org.apache.commons.logging-1.1.1
org.springframework.beans-3.0.1.RELEASE-A