加载xml
创建一个bean
public class User {
private Integer id;
private Integer age;
private String name;
//省略set get 方法
}
2.配置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.xsd">
<bean id="user" class="entity.User"/>
</beans>
3.测试获取bean
```java
public class TestMain {
public static void main(String[] args) {
FileSystemXmlApplicationContext ac=new FileSystemXmlApplicationContext("src/main/config/ApplicationContext.xml");
User user = (User) ac.getBean("user");
user.setAge(12);
user.setId(1);
user.setName("Tom");
System.out.println(user.toString());
}
//输出:
//User{id=1, age=12, name='Tom'}
4.加载带构造器的javabean
<constructor-arg index="0" name="id" type="java.lang.Integer" value="1"></constructor-arg>
<constructor-arg index="1" name="age" type="java.lang.Integer" value="12"></constructor-arg>
<constructor-arg index="2" name="name" type="java.lang.String" value="Tom"></constructor-arg>
5.创建javabean
User user = (User) ac.getBean("user");
Integer id = user.getId();
Integer age = user.getAge();
String name = user.getName();
System.out.println("id="+id+","+"name="+name+","+"age="+age);
System.out.println(user.toString());
//id=1,name=Tom,age=12
//User{id=1, age=12, name='Tom'}