首先是类的创建
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
主函数
public class mian {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Student st=(Student) app.getBean("st");
System.out.println(st.getName());
}
}
spring 配置文件 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"
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.1.xsd">
<bean id="st" class="cn.zs.com.Student">
<property name="name" value="zhangsan"></property>
<property name="age" value="12"></property>
</bean>
</beans>