我们将定义在 <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 的。

1.1 示例

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

1. 新建一个名为 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 + '\'' +
'}';
}
}

3. 在 net.biancheng.c 包下,创建一个名为 Employee 的类,代码如下。

  1. 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 +
    '}';
    }
    }

4. 在 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>

5. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。

  1. 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());
    }
    }

6. 执行 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 进行定义,格式如下。

  1. <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>

2.1 示例

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

1. 新建一个名为 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 + '\'' +
'}';
}
}

3. 在 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 +
'}';
}
}

4. 在 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>

5. 在 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());
}
}

6. 执行 MainApp 中的 main() 方法,控制台输出如下。

十二月 17, 2021 10:56:36 上午 net.biancheng.c.MainApp main
信息: Employee{empNo='002', empName='小李', dept=Dept{deptNo='005', deptName='运维部'}}

5.注入内部Bean的更多相关文章

  1. Spring Bean几种注入方式——setter(常用),构造器,注入内部Bean,注入集合,接口...

    依赖注入分为三种方式: 1.1构造器注入 构造器通过构造方法实现,构造方法有无参数都可以.在大部分情况下我们都是通过类的构造器来创建对象,Spring也可以采用反射机制通过构造器完成注入,这就是构造器 ...

  2. IoC容器-Bean管理XML方式(注入内部bean和级联赋值)

    注入属性-内部bean和级联赋值 (1)一对多关系:部分和员工 一个部门有多个员工,一个员工属于一个部门 部门是一,员工是多 (2)在实体类之间表示一对多关系 (3)在spring配置文件中进行配置 ...

  3. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  4. Spring4学习回顾之路04—引用其他Bean,集合数据注入,内部Bean

    引用其他Bean 组件应用程序的Bean经常需要相互协作以完成应用程序的功能,所以要求Bean能够相互访问,就必须在Bean配置文件中指定Bean的引用.在Bean的配置文件中可以用过<ref& ...

  5. 【spring bean】 spring中bean之间的引用以及内部bean

    在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1>  <ref  bean="另 ...

  6. Spring内部bean无法通过id获取

    内部Bean注入正常,但是直接在context中getBean是得不到的: <?xml version="1.0" encoding="UTF-8"?&g ...

  7. Spring内部bean实例

    在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean.内部bean支持setter注入“property”和构造器注入"constructor-arg“. ...

  8. Spring的引用内部Bean属性和给级联属性

    第一个是内部Bean的配置:               首先是要理解其中的原理,再去操作就很简单了,下面老表就给大家说一下自己的观点(有点简单,但是老表我第一次学习的时候看着视频上的代码确实有点懵逼 ...

  9. 【Spring实战】—— 6 内部Bean

    本篇文章讲解了Spring的通过内部Bean设置Bean的属性. 类似内部类,内部Bean与普通的Bean关联不同的是: 1 普通的Bean,在其他的Bean实例引用时,都引用同一个实例. 2 内部B ...

随机推荐

  1. 多线程-线程间通信-多生产者多消费者问题(JDK1.5后Lock,Condition解决办法及开发中代码范例)

    1 package multithread4; 2 3 import java.util.concurrent.locks.Condition; 4 import java.util.concurre ...

  2. MySQL数据库本地事务原理

    在经典的数据库理论里,本地事务具备四大特征: 原子性 事务中的所有操作都是以原子的方式执行的,要么全部成功,要么全部失败: 一致性 事务执行前后,所有的数据都应该处于一致性状态---即要满足数据库表的 ...

  3. web下载文件的头消息

    resp.setHeader("Content-disposition","attachment;filename="+filename);

  4. windows批处理详解

    转:https://mp.weixin.qq.com/s/Ktbl4P16Qye7OxDNEzJI5Q

  5. find直接copy大于某一个时间小于某一个时间的文件

    find ./ -type f -newermt '2000-01-04 10:30:00' ! -newermt '2019-10-28 10:57:00' -exec cp -a {} /var/ ...

  6. SpringBoot集成MongoDB之导入导出和模板下载

    前言 自己很对自己在项目中集成MongoDb做的导入导出以及模板下载的方法总结如下,有不到之处敬请批评指正! 1.pom.xml依赖引入 <!-- excel导入导出 --> <de ...

  7. Vue 之 Nginx 部署

    nginx 下载地址:http://nginx.org/en/download.html 下载后直接解压,cmd 进入到解压目录运行 start nginx 即可启动 常用命令: nginx -s s ...

  8. IntelliJ IDEA 官方网站 http://www.jetbrains.com/idea/

    IntelliJ IDEA 官方网站 http://www.jetbrains.com/idea/

  9. Java数组3种创建方式

    public static void main(String[] args){ /** * 1. 固定大小的空数组, 动态创建 */ String[] strArr1 = new String[3]; ...

  10. 鸟哥的Linux学习笔记-bash

    1. /bin/bash是linux预设的shell,也是Linux发行版的标准shell,它兼容sh,可以看作是sh的功能加强. 2. bash具有命令记录功能,在bash中通过上下键就可以翻找之前 ...