1.创建pojo类Student
/**
*
*/
package testspring.bean;
/**
* @author 无痕菌
*
*/
public class Student {
private int id = 1;
private String name = "张三";
private String clazz = "大四一班";
/**
* @return id
*/
public int getId() {
return id;
}
/**
* @param id 要设置的 id
*/
public void setId(int id) {
this.id = id;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return clazz
*/
public String getClazz() {
return clazz;
}
/**
* @param clazz 要设置的 clazz
*/
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
2.在src下创建beans.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/affairs" />
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
-->
<bean id="student" class="testspring.bean.Student" >
<!-- collaborators and configuration for this bean go here -->
<property name = "clazz" value = "三年二班"></property>
</bean>
</beans>
3.创建调用Student类的测试类
public class BeanFactoryTest {
public static void main(String[] args) {
@SuppressWarnings("deprecation")
BeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));
Student student = (Student) bFactory.getBean("student");
System.err.println(student.getId());
}