Spring 注入内部 Bean

我们将定义在 <bean> 元素的 <property> 或 <constructor-arg> 元素内部的 Bean,称为“内部 Bean”。

setter 方式注入内部 Bean

我们可以通过 setter 方式注入内部 Bean。此时,我们只需要在 <bean> 标签下的 <property> 元素中,再次使用 <bean> 元素对内部 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-3.0.xsd">

        <bean id="outerBean" class="……">
            <property name="……" >
                <!-- 定义内部 Bean -->
                <bean class="……">
                    <property name="……" value="……" ></property>
                    ……
                </bean>
            </property>
        </bean>

    </beans>

注意:内部 Bean 都是匿名的,不需要指定 id 和 name 的。即使制定了,IoC 容器也不会将它作为区分 Bean 的标识符,反而会无视 Bean 的 Scope 标签。因此内部 Bean 几乎总是匿名的,且总会随着外部的 Bean 创建。内部 Bean 是无法被注入到它所在的 Bean 以外的任何其他 Bean 的。

示例

下面我们就通过一个实例,演示下如何使用 setter 方法注入内部 Bean。

  1. 参考《第一个 Spring 程序》,新建一个名为 my-spring-demo2 的 Java 项目。
  2. 在 net.biancheng.c 包中,创建一个名为 Dept 的类,代码如下
    package net.biancheng.c;

    public class Dept {
        //部门编号
        private String deptNo;
        //部门名称
        private String deptName;

        public void setDeptNo(String deptNo) {
            this.deptNo = deptNo;
        }

        public void setDeptName(String deptName) {
            this.deptName = deptName;
        }

        @Override
        public String toString() {
            return "Dept{" +
                    "deptNo='" + deptNo + '\'' +
                    ", deptName='" + deptName + '\'' +
                    '}';
        }
    }
  1. 在 net.biancheng.c 包下,创建一个名为 Employee 的类,代码如下
    package net.biancheng.c;

    public class Employee {
        //员工编号
        private String empNo;
        //员工姓名
        private String empName;
        //部门信息
        private Dept dept;

        public void setEmpNo(String empNo) {
            this.empNo = empNo;
        }

        public void setEmpName(String empName) {
            this.empName = empName;
        }

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

        @Override
        public String toString() {
            return "Employee{" +
                    "empNo='" + empNo + '\'' +
                    ", empName='" + empName + '\'' +
                    ", dept=" + dept +
                    '}';
        }
    }
  1. 在 src 目录下创建 Spring 配置文件 Beans.xml,配置如下
    <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-3.0.xsd">

        <bean id="employee" class="net.biancheng.c.Employee">
            <property name="empNo" value="001"></property>
            <property name="empName" value="小王"></property>
            <property name="dept">
                <!--内部 Bean-->
                <bean class="net.biancheng.c.Dept">
                    <property name="deptNo" value="004"></property>
                    <property name="deptName" value="技术部"></property>
                </bean>
            </property>
        </bean>

    </beans>
  1. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下
    package net.biancheng.c;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class MainApp {
        private static final Log LOGGER = LogFactory.getLog(MainApp.class);

        public static void main(String[] args) {
            //获取 ApplicationContext 容器
            ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
            //获取名为 employee 的 Bean
            Employee employee = context.getBean("employee", Employee.class);
            //通过日志打印员工信息
            LOGGER.info(employee.toString());
        }
    }
  1. 执行 MainApp 中的 main() 方法,控制台输出如下
十二月 17, 2021 10:46:06 上午 net.biancheng.c.MainApp main
信息: Employee{empNo='001', empName='小王', dept=Dept{deptNo='004', deptName='技术部'}}

构造函数方式注入内部 Bean

我们可以通过构造方法注入内部 Bean。此时,我们只需要在 <bean> 标签下的 <constructor-arg> 元素中,再次使用 <bean> 元素对内部 Bean 进行定义,格式如下。

    <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-3.0.xsd">

        <bean id="……" class="……">
            <constructor-arg name="……">
                <!--内部 Bean-->
                <bean class="……">
                    <constructor-arg name="……" value="……"></constructor-arg>
                    ……
                </bean>
            </constructor-arg>
        </bean>

    </beans>

示例

下面我们就通过一个实例,演示下如何在通过构造方法的方式注入内部 Bean。

  1. 参考《第一个 Spring 程序》,新建一个名为 my-spring-demo3 的 Java 项目。
  2. 在 net.biancheng.c 包中,创建一个名为 Dept 的类,代码如下
    package net.biancheng.c;

    public class Dept {
        //部门编号
        private String deptNo;
        //部门名称
        private String deptName;

        public Dept(String deptNo, String deptName) {
            this.deptNo = deptNo;
            this.deptName = deptName;
        }

        @Override
        public String toString() {
            return "Dept{" +
                    "deptNo='" + deptNo + '\'' +
                    ", deptName='" + deptName + '\'' +
                    '}';
        }
    }
  1. 在 net.biancheng.c 包下,创建一个名为 Employee 的类,代码如下
    package net.biancheng.c;

    public class Employee {
        //员工编号
        private String empNo;
        //员工姓名
        private String empName;
        //部门信息
        private Dept dept;

        public Employee(String empNo, String empName, Dept dept) {
            this.empNo = empNo;
            this.empName = empName;
            this.dept = dept;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "empNo='" + empNo + '\'' +
                    ", empName='" + empName + '\'' +
                    ", dept=" + dept +
                    '}';
        }
    }
  1. 在 src 目录下创建 Spring 配置文件 Beans.xml,配置如下
    <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-3.0.xsd">

        <bean id="employee" class="net.biancheng.c.Employee">
            <constructor-arg name="empNo" value="002"></constructor-arg>
            <constructor-arg name="empName" value="小李"></constructor-arg>
            <constructor-arg name="dept">
                <!--内部 Bean-->
                <bean class="net.biancheng.c.Dept">
                    <constructor-arg name="deptNo" value="005"></constructor-arg>
                    <constructor-arg name="deptName" value="运维部"></constructor-arg>
                </bean>
            </constructor-arg>
        </bean>

    </beans>
  1. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下
    package net.biancheng.c;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class MainApp {
        private static final Log LOGGER = LogFactory.getLog(MainApp.class);

        public static void main(String[] args) {
            //获取 ApplicationContext 容器
            ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
            //获取名为 employee 的 Bean
            Employee employee = context.getBean("employee", Employee.class);
            //通过日志打印员工信息
            LOGGER.info(employee.toString());
        }
    }
  1. 执行 MainApp 中的 main() 方法,控制台输出如下
十二月 17, 2021 10:56:36 上午 net.biancheng.c.MainApp main
信息: Employee{empNo='002', empName='小李', dept=Dept{deptNo='005', deptName='运维部'}}
posted @ 2023-01-17 16:59  HopeLive  阅读(78)  评论(0)    收藏  举报