1.编写测试类
User是用户类, Dept是部门类, 用户与部门的关系为多对一. 要求是往User里面注入Dept
package com.limi.test;
public class User {
private String name;
private Integer age;
private String hoby;
private Dept dept;
public User(){
}
public User(String name, Integer age){
this.name = name;
this.age = age;
}
public void say(){
System.out.println("name="+name+" age="+age+" hoby="+hoby);
System.out.println("\n"+dept.getNo()+" "+dept.getName()+" "+dept.getLevel());
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setHoby(String hoby) {
this.hoby = hoby;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Dept getDept(){
return dept;
}
}
package com.limi.test;
public class Dept {
private Long no; //编号
private String name; //名称
private Integer level; //等级
public void setNo(Long no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setLevel(Integer level) {
this.level = level;
}
public Long getNo() {
return no;
}
public String getName() {
return name;
}
public Integer getLevel() {
return level;
}
}
package com.limi.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
//1.加载bean的xml文件, 以src为根目录
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
User user = context.getBean("User", User.class);
//3.使用User对象
user.say();
}
}
2.第一种注入方式:外部bean注入
- 要注入的Dept bean标签写在User 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">
<!--id是唯一标识, class是全类名 -->
<bean id="User" class="com.limi.test.User">
<property name="name" value="andy"></property>
<!-- 注入外部bean-->
<property name="dept" ref="Dept"></property>
<!--可以对引入的bean进行修改, 但是User类中一定要有dept的getter方法-->
<property name="dept.no" value="1111"></property>
<property name="dept.level" value="6666"></property>
</bean>
<!--Dept bean写在User bean的外部-->
<bean id="Dept" class="com.limi.test.Dept">
<property name="no" value="1"></property>
<property name="name" value="计算机学院"></property>
</bean>
</beans>
- 测试结果
3.第二种注入方式:内部bean注入
- 要注入的Dept bean标签写在User 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">
<!--id是唯一标识, class是全类名 -->
<bean id="User" class="com.limi.test.User">
<property name="name" value="andy"></property>
<!-- 注入内部bean-->
<property name="dept">
<!--Dept bean写在User bean的内部-->
<bean id="Dept" class="com.limi.test.Dept">
<property name="no" value="1"></property>
<property name="name" value="计算机学院"></property>
<property name="level" value="666"></property>
</bean>
</property>
</bean>
</beans>
- 测试结果