这篇文章主要为大家介绍了spring 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 0ok,到这里,其实就是手动装配的过程。
实现自动装配,在配置文件里,通过 bean 标签里的属性 autowire 来配置:
- autowire="byName":根据属性名称自动注入。
- autowire="byType":根据属性类型自动注入。
1)byName 演示
注入值的bean的 id 值和类属性名称一致,比如:

修改配置文件,加上 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 的类型与 属性里的一致,比如:

现在继续修改配置文件,加上 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 方式使用自动装配实际中是很少的,一般是以注解的方式,后续会学习到。
以上就是spring IOC容器的Bean管理XML自动装配过程的详细内容,更多关于spring IOC Bean管理XML装配的资料请关注编程学习网其它相关文章!
本文标题为:spring IOC容器的Bean管理XML自动装配过程
基础教程推荐
- RocketMQ Push 消费模型示例详解 2023-05-24
- Java Web开发中过滤器和监听器使用详解 2023-06-30
- java知识点7——面向过程和面向对象、面向对象的内存分析、构造方法 2023-09-01
- MyBatis-Plus自定义通用的方法实现 2023-07-15
- 一文带你搞懂Java中方法重写与方法重载的区别 2023-07-14
- Spring Cloud Config分布式配置中心使用介绍详解 2023-05-07
- 关于Java双大括号{{}}的具体使用 2023-03-15
- Java List的get方法 2023-10-08
- Java多线程学习笔记之三内存屏障与Java内存模型 2023-09-01
- Java实现插入排序算法可视化的示例代码 2023-04-23
