【Spring 从0开始】IOC容器的Bean管理 - 基于XML - 自动装配

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 【Spring 从0开始】IOC容器的Bean管理 - 基于XML - 自动装配
+关注继续查看

什么是自动装配?


在之前的内容中,每给属性注入值都要一个个的用 property 标签来完成,比如:


<bean id="book" class="com.pingguo.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>


这就是手动装配。


而自动装配中,spring 会根据指定装配规则(属性名称或者属性类型) 来自动的将匹配的属性值进行注入。


自动装配过程


1. 创建 2 个类


分别是部门类 Department 和员工类 Employee 。


package com.pingguo.spring5.autowire;
public class Department {
    @Override
    public String toString() {
        return "Department{}";
    }
}


员工类有个 部门的属性,表示员工所属的一个部门。其他方法是为了后续方便演示输出。


package com.pingguo.spring5.autowire;
public class Employee {
    private Department department;
    public void setDepartment(Department department) {
        this.department = department;
    }
    @Override
    public String toString() {
        return "Employee{" +
                "department=" + department +
                '}';
    }
    public void test() {
        System.out.println(department);
    }
}


2. 配置文件


<?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="employee" class="com.pingguo.spring5.autowire.Employee">
        <property name="department" ref="department"></property>
    </bean>
    <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>
</beans>


3. 测试方法


@Test
    public void test5() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean5.xml");
        Employee employee = context.getBean("employee", Employee.class);
        System.out.println(employee);
    }


运行结果:


Employee{department=Department{}}
Process finished with exit code 0


ok,到这里,其实就是手动装配的过程。


实现自动装配,在配置文件里,通过 bean 标签里的属性 autowire 来配置:


  • autowire="byName":根据属性名称自动注入。
  • autowire="byType":根据属性类型自动注入。


1)byName 演示


注入值的bean的 id 值和类属性名称一致,比如:


1268169-20210731174149303-2079122289.png


修改配置文件,加上 autowire="byName",然后注释掉 property。


<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byName">
        <!--<property name="department" ref="department"></property>-->
    </bean>
    <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>


执行测试函数:


Employee{department=Department{}}
Process finished with exit code 0


跟使用 property 手动装配结果一致。


2)byType 演示


要注入值的 bean 的类型与 属性里的一致,比如:


1268169-20210731174542924-1046999257.png


现在继续修改配置文件,加上 autowire="byType",然后注释掉 property。


<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byType">
        <!--<property name="department" ref="department"></property>-->
    </bean>
    <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>


再次执行测试:


Employee{department=Department{}}
Process finished with exit code 0


跟使用 property 手动装配结果一致。


不过,用 xml 方式使用自动装配实际中是很少的,一般是以注解的方式,后续会学习到。

相关文章
|
2天前
|
安全 Java 数据库连接
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
22 1
|
3天前
|
Java API 容器
[读书笔记]IOC容器的依赖注入详解
[读书笔记]IOC容器的依赖注入详解
23 0
|
7天前
|
容器
IOC容器初始化过程中单例Bean的预实例化
IOC容器初始化过程中单例Bean的预实例化
17 0
|
7天前
|
XML Java 数据格式
Spring IOC容器注解大全—基于Java的容器配置(@Bean 、 @Configuration、@PropertySource)
Spring IOC容器注解大全—基于Java的容器配置(@Bean 、 @Configuration、@PropertySource)
24 0
|
2月前
|
容器
.net core Autofac IOC 容器的简单使用
## 书接上回,介绍了[.net core 读取配置文件的几种方式](https://developer.aliyun.com/article/1363340?spm=a2c6h.13148508.setting.14.21764f0ehMR1KI ".net core 读取配置文件的几种方式"),本文学习Autofac的同时再次增加一种读取配置文件的方法。 ## 本文介绍Auofac,一个优秀的.NET IOC框架 ## 源码地址:https://github.com/autofac/Autofac # 1、打开NuGet包管理器安装Autofac.Extensions.Dependenc
21 0
|
2月前
|
缓存 Java 数据库连接
Spring之ioc容器
Spring之ioc容器
23 0
|
2月前
|
开发框架 Java 数据库连接
Spring讲解和ioc用途及Web容器的整合
Spring讲解和ioc用途及Web容器的整合
75 0
|
2月前
|
Java 应用服务中间件 数据库连接
Spring之IoC容器篇
Spring之IoC容器篇
13 0
|
2月前
|
XML Java 数据格式
Spring Framework的核心:IoC容器的实现(1)
Spring Framework的核心:IoC容器的实现(1)
29 0
|
2月前
|
缓存 Java 容器
【Spring IOC容器加载过程】
【Spring IOC容器加载过程】
相关产品
容器镜像服务
容器服务Kubernetes版
推荐文章
更多