Spring之IOC(DI)基于XML装配bean(二)

当A对象中需要实例化B对象时,有两种方式可以注入对象B
下面由Service层和Dao层举例,Service层调用Dao层方法,需实例化对象时:
一、外部bean注入属性
Dao实现类:

public class UserDaoImpl implements UserDao{
    @Override
    public void print() {
        System.out.println("Dao.....");
    }
}

Service实现类

import cn.kexing.dao.UserDao;
public class UserServiceImpl implements UserService{
    private UserDao dao;

    public void setDao(UserDao dao) {
        this.dao = dao;
}

    @Override
    public void print() {
        System.out.println("service....");
        dao.print();
    }
}

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属性注入 ref-->
    <bean id="dao" class="cn.kexing.dao.UserDaoImpl"></bean>
    <bean id="service" class="cn.kexing.service.UserServiceImpl">
        <property name="dao" ref="dao"></property>
    </bean>
</beans>

通过ref属性外部引入对象实现外部bean注入

测试案例:

    @Test
    //测试外部bean属性注入
    public void testOutBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        UserService service = context.getBean("service", UserServiceImpl.class);
        service.print();
    }

程序结果:

service....
Dao.....

二、内部bean注入属性
Emp的javabean类:

/**
 * 员工
 */
public class Emp {
    private String id;
    private String name;
    private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", dept=" + dept +
                '}';
    }
}

dept的javabean类:

/**
 * 部门 一对多
 */
public class Dept {
    String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "name='" + name + '\'' +
                '}';
    }
}

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-->
    <bean id="emp" class="cn.kexing.bean.Emp">
        <property name="name" value="张三"></property>
        <property name="id" value="20180001"></property>
        <property name="dept">
            <bean id="dept" class="cn.kexing.bean.Dept">
                <property name="name" value="技术部"></property>
            </bean>
        </property>
    </bean>
</beans>

这里的内部bean类似于一个匿名内部类,只能使用一次,而外部bean可以多次引用

测试案例:

    @Test
    //测试内部bean属性注入
    public void testInnerBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Emp emp = context.getBean("emp",Emp.class);
        System.out.println(emp.getId()+"--"+emp.getName()+"--"+emp.getDept());
    }

程序结果:

20180001--张三--Dept{name='技术部'}

三、级联赋值(外部bean使用时注入属性)
基于emp和dept类
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="dept" class="cn.kexing.bean.Dept"></bean>

    <bean id="emp" class="cn.kexing.bean.Emp">
        <property name="id" value="20180002"></property>
        <property name="name" value="李四"></property>
        <property name="dept" ref="dept"></property>
        <property name="dept.name" value="宣传部"></property>
    </bean>
</beans>

测试案例:

    @Test
    //测试级联赋值
    public void testJiLian(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Emp emp = context.getBean("emp",Emp.class);
        System.out.println(emp.getId()+"--"+emp.getName()+"--"+emp.getDept());
    }
}

程序结果:

20180002--李四--Dept{name='宣传部'}
最后修改:2021 年 11 月 15 日 05 : 26 PM
如果觉得我的文章对你有用,请随意赞赏