学习笔记:Spring中default-autowire与autowire区别
default-autowire与autowire主要用于Spring的IOC的注解注入,明白两者的区别和用法将使你的开发事半功倍。
Spring 提供了Resource、Autowired这两个注解用于注入,另外在xml配置文件中,beans标签下有一个参数default-autowire用来设置默认的注入类型。
default-autowire和autowire的可选值
可选值 | 功能说明 |
---|---|
no | 默认不使用autowiring。 必须显示的使用”“标签明确地指定bean。 |
byName | 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。 |
byType | 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置 dependency-check=”objects”让Spring抛出异常。 |
constructor | 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。 |
autodetect | 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。 |
beans标签设置default-autowire参数
在spring的配置文件中可以参照如下设置default-autowire参数
<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"default-autowire="byName">
在beans标签设置default-autowire=”byName”后,在spring容器实例化bean(包含xml配置bean和注解方式bean)时,该bean内部的字段会自动根据byName装载spring容器里的bean。如下:
Spring配置文件中
<bean id="teacher" class="com.haiwi.spring.Teacher"></bean>
<bean id="student" class="com.haiwi.spring.Student"><property name="stu_name" value="Tom" />
</bean>
Student类
public class Teacher {String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}public class Student {private Teacher teacher;private String stu_name;public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}public String getStu_name() {return stu_name;}public void setStu_name(String stu_name) {this.stu_name = stu_name;}
}
此时spring容器启动加载bean时候由于在beans标签设置default-autowire=”byName”,那么在加载Student的Bean时候,会把字符串”Tom”注入到Student类的stu_name字段,而由于配置了自动注入所有在注入teacher字段时候,会扫描容器里id为teacher的Bean注入到该字段中,而不用再在id为student的Bean配置了。
autowire参数的用法
autowire可用在两次:XML中配置Bean的参数和@Autowired注解注入。两者都是把对应的类实现自动注入。
(1)XML中配置Bean的参数
Autowire配置的可选值依然如上述表格中的说明,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.xsd">
<bean id="teacher" class="com.haiwi.spring.Teacher"></bean>
<bean id="student" class="com.haiwi.spring.Student" autowire="byName"><property name="stu_name" value="Tom" />
</bean>
…..
</beans>
同样,spring容器启动加载bean时候,在加载到Student的Bean时候,会把字符串”Tom”注入到Student类的stu_name字段,而同时由于对这个类配置了autowire=”byName”参数,所以会扫描spring容器中id为teacher的Bean然后注入到该类的teacher字段中。
(2)@Autowired注解注入
@Autowired往往用在类中注解注入,在配置xml需要配置才能使用@Autowired标识
<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/context http://www.springframework.org/schema/beans/spring-beans.xsd"><context:annotation-config /><bean id="teacher" class="com.haiwi.spring.Teacher"></bean><bean id="student" class="com.haiwi.spring.Student" autowire="byName"><property name="stu_name" value="Tom" /></bean>…..
</beans>
在类中使用@Autowired标识注入字段
@Autowired
@Qualifier("teacher")
private Teacher teacher;
public void setTeacher(Teacher teacher) {this.teacher = teacher;
}
学习笔记:Spring中default-autowire与autowire区别相关推荐
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- r语言c函数怎么用,R语言学习笔记——C#中如何使用R语言setwd()函数
在R语言编译器中,设置当前工作文件夹可以用setwd()函数. > setwd("e://桌面//") > setwd("e:\桌面\") > ...
- c#学习笔记05-treeview中添加图标
创建树目录前面在学习笔记03中已经提到过 即树目录数据从XML文档中获取 添加图标主要用到ImageList控件 1.ImageList控件 在树目录对应存在的窗体中添加此控件 添加图标: 2.tre ...
- 20190328学习笔记 - JSP 中的 tag 文件
20190328学习笔记 - JSP 中的 tag 文件 对于tag 文件 1. 引入 tag 文件 2. 在/WEB-INF/tags/sys 下,新增一个gridselect.tag文件 3. 在 ...
- spring中ClassPathXmlApplication和FileSystemXmlApplicationContext使用区别
spring中ClassPathXmlApplication和FileSystemXmlApplicationContext使用区别 ApplicationContext ac = new Class ...
- spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?
问题:spring中@Inject和@Autowired的区别?分别在什么条件下使用呢? 我在浏览SpringSource上的一些博客,在其他一个博客中,那个作者用了@Inject,但是我觉得他用@A ...
- 学习笔记---Spring 5
Spring 5 1. 介绍 1.1 简介 历史:2002年,首次推出了 Spring 框架的雏形:interface 21.2004年3月24日,Spring诞生,发布了 1.0 正式版本. Rod ...
- redis spring 切面缓存_今日份学习: Spring中使用AOP并实现redis缓存?
笔记 在Spring中如何使用AOP? Spring是如何切换JDK动态代理和CGLIB的? spring.aop.proxy-target-class=true (在下方第二个链接中,原生doc中提 ...
- Ceph学习笔记2-在Kolla-Ansible中使用Ceph后端存储
环境说明 使用 Kolla-Ansible 请参考<使用 Kolla-Ansible 在 CentOS 7 单节点上部署 OpenStack Pike >: 部署 Ceph 服务请参考&l ...
- SilverLight学习笔记--Silverlight中WebRequest通讯
本文我们学习如何使用WebRequest类实现客户端和服务器端的通讯. 本例处理过程:在客户端,我们在文本框中输入任意文本,然后用POST方法向服务器端传递信息,服务器端收到从客户端传来的信 ...
最新文章
- apt-get常用命令
- mysql生成100000个数据并检验索引的效果
- 谷歌 AI 中国中心成立,人工智能势不可挡?
- Object C为UILabel添加点击事件
- Android复习07【创建数据库、insert()插入数据、查看数据库、根据列索引获取参数值、根据列名-返回索引、增删改查数据、数据分页、修改表结构、Room框架】
- modelform save
- 编写python程序_如何进行Python程序的编写
- CString .Format
- 白帽子讲web安全——认证与会话管理
- linux 程序运行出错的时候,如何进行调试动态库
- 2020 CPU性能排行天梯图
- 服务器搭建nginx集群
- java分布式文件存储服务实战
- 利用Arthas进行简单的故障注入
- 直击GITC2018 尚德机构苏万松:从消费互联网到产业互联网
- 【FFmpeg】通过编解码ID(AV_CODEC_ID_*)获取编解码器AVCodec指针的过程分析
- 点到平面的距离(最短距离)
- 信源编码与信道编码-Source coding Channel coding
- 命名实体识别python_命名实体识别的两种方法
- java锁源码分析-1获取锁的过程