Spring的自动装配Bean
spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>、<constructor-arg>)。IOC容器会自动建立javabean之间的关联关系。
如果没有采用自动装配的话,手动装配我们通常在配置文件中进行实现:以下代码就是手动装配:
|
1
2
3
4
5
6
7
8
9
10
|
xsi:schemaLocation="http://www.springframework.org/schema/beans <bean id="customerDAO" class="com.hebeu.customer.dao.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans> |
通过<property name="dataSource" ref="dataSource" />向customerDAO的bean中注入了dataSource
在Spring框架,可以用 auto-wiring 功能会自动装配Bean。要启用它,只需要在 <bean>定义“autowire”属性。
|
1
|
<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" /> |
在Spring中,支持 5 自动装配模式。
- no – 缺省情况下,自动配置是通过“ref”属性手动设定
- byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
- byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
- constructor – 在构造函数参数的byType方式。
第一种自动装配【根据属性名称自动装配】
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.hebeu.model; public class Customer { private Address address; public Customer() { } public Customer(int id, Address address) { super(); this.address = address; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.hebeu.model; public class Address { private String fulladdress; public Address(){ } public Address(String addr){ this.fulladdress = addr; } public String getFulladdress() { return fulladdress; } public void setFulladdress(String fulladdress) { this.fulladdress = fulladdress; } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
xsi:schemaLocation="http://www.springframework.org/schema/beans <bean id="customer" class="com.hebeu.model.Customer" autowire="byName"></bean> <bean id="address" class="com.hebeu.model.Address"> <property name="fulladdress" value="YiLong Road, CA 188"></property> </bean> </beans> |
这样就将address注入到Customer中。这就是自动注入ByName.在Customer bean中公开了一个属性address,Spring容器会找到address bean,并且装配。这里必须要注意,Customer中要被注入的bean的set方法要求必须是public的,否则会报空指针异常。还有配置的bean的id必须和Customer中声明的变量名相同。
第二种自动装配【根据数据类型自动装配】
声明的俩个bean同样为Customer以及Address,将applicationContext.xml转换为这样的就是实现根据数据类型进行自动装配。
|
1
2
3
4
5
6
7
8
9
10
11
|
xsi:schemaLocation="http://www.springframework.org/schema/beans <bean id="customer" class="com.hebeu.model.Customer" <strong><span style="color:#FF0000;">autowire="byType"</span></strong>></bean> <bean id="bean1" class="com.hebeu.model.Address"> <property name="fulladdress" value="YiLong Road, CA 188"></property> </bean> </beans> |
类型自动装配的意思是如果一个bean的数据类型与其他的bean属性的数据类型相同,将会自动兼容装配它。当然要求只能配置一个某一个类型的bean.如果配置成这样,那么是会出错的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
xsi:schemaLocation="http://www.springframework.org/schema/beans <bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean> <bean id="bean1" class="com.hebeu.model.Address"> <property name="fulladdress" value="YiLong Road, CA 188"></property> </bean> <bean id="bean2" class="com.hebeu.model.Address"> <property name="fulladdress" value="YiLong Road, CA 188"></property> </bean> </beans> |
第三种自动装配【根据构造方法自动装配】
案例同上,将applicationContext.xml转换为如下,就实现了按照构造方法自动装配:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
xsi:schemaLocation="http://www.springframework.org/schema/beans <bean id="customer" class="com.hebeu.model.Customer"> <!-- 构造方法的参数 --> <constructor-arg> <ref bean="bean1"/> </constructor-arg> </bean> <bean id="bean1" class="com.hebeu.model.Address"> <property name="fulladdress" value="YiLong Road, CA 188"></property> </bean> </beans> |
它实际上是构造函数的参数类型自动装配。这意味着如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么就自动装配。
转载:https://www.jb51.net/article/106856.htm
Spring的自动装配Bean的更多相关文章
- spring中自动装配bean
首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring自动装配Bean的五种方式
在Spring中,支持 5 自动装配模式. no – 缺省情况下,自动配置是通过“ref”属性手动设定,在项目中最常用byName – 根据属性名称自动装配.如果一个bean的名称和其他bean属性的 ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- spring的自动装配基础
当开始看别人的代码使用注解的时候,以为照着别人的代码写,也写一个注释就能实现这样的功能,但是,现在开始考虑自动装配时怎样实现的. 首先,如果如果知道如何手动在xml配置中"装配bean&qu ...
随机推荐
- 3分钟解决MySQL 1032 主从错误(转)
转自 https://blog.51cto.com/suifu/1845457 3分钟解决MySQL 1032主从错误 Part1:写在最前 1032错误----现在生产库中好多数据,在从库误删了, ...
- day00 预习 ------基础数据类型预习 ,int ,str ,bool ,dict ,set ,切片,等相关
知识点明确 1 int 2 str 3 元祖 4.列表 5. 字典 6 集合 7 布尔 1 int 数据类型 int 数据类型指的是. 数字型的内容 ,主要用于计算, 2 str 字符类型 str ...
- [BZOJ5248][2018九省联考]一双木棋
题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=5248 Solution 我们首先考虑放棋子的操作 发现它一定放棋子的部分是一个联通块 ...
- LOJ2229. 「BJOI2014」想法(随机化)
题目链接 https://loj.ac/problem/2229 题解 评分标准提示我们可以使用随机化算法. 首先,我们为每一道编号在 \([1, m]\) 以内的题目(这些题目也对应了 \(m\) ...
- 使用scp命令,远程上传下载文件/文件夹
1.从服务器下载文件 scp username@servername:/path/filename /local/path例如: scp ubuntu@117.50.20.56:/ygf/data/d ...
- maven工程下testng简单使用
创建maven工程后,将Repository仓库中maven代码粘贴复制到pom.xml文件中,仓库地址:<!-- https://mvnrepository.com/artifact/org. ...
- activity启动模式launchMode区别和优化
初学android的开发人员,可能会经常忽略这个重要的设置. Activity一共有以下四种launchMode:1.standard2.singleTop3.singleTask4.singleIn ...
- Java多线程(一)初步了解
1:线程 1.1:线程是什么?线程与进程. 进程:正在运行的程序.windows是多进程的系统.每一个进程有自己的内存区域,也就是每个进程都会占用一定的内存.一般打开应用程序就会有进程的了. 线程:进 ...
- Installing Vim 8.0 on Ubuntu 16.04 and Linux Mint 18
sudo add-apt-repository ppa:jonathonf/vim sudo apt update sudo apt install vim uninstall sudo apt re ...
- Veloce2 Emulator
High capacity, high-speed, multi-application powerhouse for simulation and emulation of SoC designs ...