Spring自动装配Bean实现过程详解
作者:流氓大队长 时间:2023-10-31 18:35:21
这篇文章主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
要使用自动装配,就需要配置 <bean> 元素的 autowire 属性。autowire 属性有五个值,具体说明如表 1 所示。
表 1 autowire 的属性和作用
名称 | 说明 |
---|---|
byName | 根据 Property 的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。 |
byType | 根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。 |
constructor | 根据构造方法的参数的数据类型,进行 byType 模式的自动装配。 |
autodetect | 如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式。 |
no | 默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。 |
下面通过案例演示如何实现自动装配。首先将 applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="personDao" class="com.mengma.annotation.PersonDaoImpl" />
<bean id="personService" class="com.mengma.annotation.PersonServiceImpl"
autowire="byName" />
<bean id="personAction" class="com.mengma.annotation.PersonAction"
autowire="byName" />
</beans>
在上述配置文件中,用于配置 personService 和 personAction 的 <bean> 元素中除了 id 和 class 属性以外,还增加了 autowire 属性,并将其属性值设置为 byName(按属性名称自动装配)。
默认情况下,配置文件中需要通过 ref 装配 Bean,但设置了 autowire="byName",Spring 会在配置文件中自动寻找与属性名字 personDao 相同的 <bean>,找到后,通过调用 setPersonDao(PersonDao personDao)方法将 id 为 personDao 的 Bean 注入 id 为 personService 的 Bean 中,这时就不需要通过 ref 装配了。
使用 JUnit 再次运行测试类中的 test() 方法,控制台的显示结果如图所示。
来源:https://www.cnblogs.com/lowerma/p/11752310.html
标签:Spring,自动,装配,Bean
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot搭建访客管理系统的实现示例
2023-09-02 13:10:41
![](https://img.aspxhome.com/file/2023/3/58463_0s.png)
android 仿微信demo——登录功能实现(服务端)
2023-10-04 13:40:55
![](https://img.aspxhome.com/file/2023/9/138789_0s.png)
Winform中Treeview实现按需加载的方法
2023-08-01 11:45:32
解析C#彩色图像灰度化算法的实现代码详解
2022-01-26 07:34:55
java实现ip地址与十进制数相互转换
2022-08-10 17:39:31
C#实现改变DataGrid某一行和单元格颜色的方法
2022-08-23 20:45:22
java中String、StringBuffer与StringBuilder的区别
2021-11-12 13:28:24
![](https://img.aspxhome.com/file/2023/7/61517_0s.jpg)
SpringBoot后端接口的实现(看这一篇就够了)
2021-11-05 04:07:41
java实现简单银行家算法
2022-01-31 23:33:19
![](https://img.aspxhome.com/file/2023/7/63167_0s.jpg)
java使用jaxb操作xml示例
2023-11-05 15:02:18
Android7.0开发实现Launcher3去掉应用抽屉的方法详解
2021-07-24 12:31:38
![](https://img.aspxhome.com/file/2023/0/138670_0s.jpg)
Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)
2023-10-30 19:42:57
谈C# using的用法与好处
2022-02-10 08:20:01
List集合对象中按照不同属性大小排序的实例
2023-06-07 14:27:41
Dubbo扩展点SPI实践示例解析
2021-12-14 12:56:45
RSA密钥--JAVA和C#的区别及联系
2022-09-18 12:16:44
c# 如何用lock解决缓存击穿
2022-12-03 14:27:55
java实现文件上传下载
2023-11-23 09:41:18
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
2023-11-28 22:04:56
c#汉诺塔的递归算法与解析
2022-08-13 08:13:59
![](https://img.aspxhome.com/file/2023/2/91372_0s.png)