1,复制项目Spring402 ,改名为Spring402-02。建包:com.cruise.entity .建类:People类,定义属性 id name age,get()和 set()方法,
package com.cruise.entity;
public class People {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
2,在beans.xml中,定义一个bean。
<?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.xsd">
<bean id="people" class="com.cruise.entity.People"></bean>
</beans>
3. 删除没用的包:com.cruise.service包,删除没有的类:在com.cruise.test包下,只要一个test2类即可,其他的类都删除掉。
4. 修改com.cruise.test包下Test2类,运行-测试
package com.cruise.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.entity.People;
public class Test2 {
public static void main(String[] args) {
ClassPathXmlApplicationContext CA = newClassPathXmlApplicationContext("beans.xml");
People people = (People)CA.getBean("people");//people与beans.xml中的id一致
System.out.println(people);
}
}
相关文章: