spring中autowire的用法
Autowire模式就是在spring的声明文件里用作进行对象间的关联关系自动绑定的,就是在spring beanfactory内的一个bean对其bean的引用可以自动进行,而不一定用ref=的方式显式声明。在reference的3.3.6节有详细的介绍,autowire主要有5种模式:
1 no
不使用Autowire,引用关系显示声明,spring的reference也建议不用autoware,因为这会破坏模块关系的可读性,原文如下:
Note: as has already been mentioned, for larger applications, it is discouraged to use autowiring because it
removes the transparency and the structure from your collaborating classes.
2 byName
用名称关联,如果指定了这种模式,如
<bean id="userManagerTarget" class="com.mdcchina.jianghu.logic.UserManager" autowire="byName">
<property name="baseDAO"/>
</bean>
这样对于bean userManagerTarget的属性baseDAO,spring就会自动去引用同名的bean,也就是上面的声明和下面是等价的:
<bean id="userManagerTarget" class="com.mdcchina.jianghu.logic.UserManager" autowire="no">
<property name="baseDAO">
<ref local="baseDAO"/>
</property>
</bean>
3 byType
和前面的byName类似的,就是这个属性会在整个beanFactory定义里找和这个属性一样的bean自动关联上,如果有2个或更多这个类型的bean在beanFactory的定义里,就直接抛异常了,如果没有,就什么都不发生,这个属性就是null,所以这个只适用与这个属性的类型有且只有一个同类的bean在spring里定义
4 constructor
这个的意思我没有确定的把握,不过感觉用途也不会大,好像是用构造函数新建一个属性类型的bean并关联上,reference原文是:
This is analogous to byType, but applies to constructor arguments. If there isn't exactly one
bean of the constructor argument type in the bean factory, a fatal error is raised.
5 autodetect
这个的意思好像是说自动决定用byType还是constructor方式,原文如下:
Chooses constructor or byType through introspection of the bean class. If a default
constructor is found, byType gets applied.
综上所述,感觉上只有byName比较实用一些,但是spring的reference还是不推荐在定义中用这个功能
spring中autowire的用法的更多相关文章
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- Spring中@Cacheable的用法
在Spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式.不过,在实际开发中,我们往往是通过Spring的@Cacheable来实现数据的缓存的,所以, ...
- Spring中HibernateCallback的用法(转)
Hibernate的复杂用法HibernateCallback HibernateTemplate还提供一种更加灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式.Hiber ...
- Spring中jdbcTemplate的用法实例
一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象. 第一种方式:我们可以在自己定 ...
- Spring 中classPath:用法
参考文章地址: http://hi.baidu.com/huahua035/item/ac8a27a994b55bad29ce9d39 http://blog.csdn.net/lushuaiyin/ ...
- spring 中StoredProcedure的用法--转载
StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...
- Spring中ApplicationContextAware的用法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...
- Spring中RedirectAttributes的用法
RedirectAttributes 是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类.他有两种带参的方式: 第一种: redirectAttribut ...
- 简介spring中MethodReplacer的用法
欢迎转载交流:个人博客地址http://www.cnblogs.com/shizhongtao/p/3468713.html org.springframework.beans.factory.sup ...
随机推荐
- grep 参数
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- 【BZOJ 3166】【HEOI 2013】Alo
http://www.lydsy.com/JudgeOnline/problem.php?id=3166 这道题难点在于求能对一个次大值有贡献的区间. 设这个次大值为\(a_i\),\(a_i\)左边 ...
- 【推导】Codeforces Round #411 (Div. 1) A. Find Amir
1 2 3 4 5 6 7 4-5-3-6-2-7-1 答案是(n-1)/2 #include<cstdio> using namespace std; int n; int main() ...
- 【递推+高精度】POJ2506-Tiling
思路别人那里讲的很清楚了,我就不阐述了.链接 #include<iostream> #include<cstdio> #include<cmath> #includ ...
- 20162321 实验一 Java开发环境的熟悉(Linux + Eclipse)
北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级: 1623 姓名: 王彪 学号:20162321 指导教师:娄嘉鹏 王志强 实验日期:3月25日 实验密级: 非密级 预 ...
- django查询
阅读目录 大于.大于等于 小于.小于等于 在...范围内 模糊查询 是否为空 不等于/不包含于 大于.大于等于 1 2 3 4 5 __gt 大于 __gte 大于等于 User.objects. ...
- 找出最小元素 Exercise07_09
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出最小元素 * */ public class Exercise07_0 ...
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) E. Robot Arm 线段树
E. Robot Arm 题目连接: http://www.codeforces.com/contest/618/problem/E Description Roger is a robot. He ...
- FIREMONEY手机虚拟键盘遮挡的解决
FIREMONEY手机虚拟键盘遮挡的解决 尝遍了网上人们提供的N种方法之后,发现还是老猫的方法才是彻底解决问题的办法. 老猫“不看后悔XXX”--->RAD10.2.3 Flying Wang ...
- Lua-简洁、轻量、可扩展的脚本语言
转自:http://rmingwang.com/The-Programming-Language-Lua.html 1. 一.Lua安装及常用库的安装 2. 1.1 Ubuntu $ sudo apt ...