spring自动装配
spring提供了自动装配(autowiring)和自动检测(autodiscovery)用来减少XML的配置数量。
自动装配bean属性
- byName——把与Bean的属性具有相同名字(或ID)的其他Bean自动装配到Bean的对应属性中。
示例:
import com.springinaction.springdol.Instrumentalist;
public class Instrumentalist{
private String song;
private Object instrument;
}
<bean id="instrument" class="com.springinaction.springidol.Saxophone"/> <bean id="kenny" class="com.springinaction.springdol.Instrumentalist"
autowire="byName">
<property name="song" value="Jingle Bells"/>
</bean>
- byType——把与Bean的属性具有相同类型的其他Bean自动装配到Bean的对应属性中。
- constructor——把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。
- autodetect——首先尝试使用constructor进行自动装配。如果失败,再尝试使用byType进行自动装配。
使用注解装配
从spring 2.5开始,最有趣的一种装配spring Bean的方式是使用注解自动装配Bean属性。spring容器默认禁用注解装配。所以在使用基于注解的自动装配前,我们需要在spring配置中启用它。最简单的启用方式是使用spring的context命名空间配置中的<context:annotation-config>元素。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/>
<!--bean declarations go here-->
</beans>
这样配置,<context:annotation-config>元素告诉spring我们打算使用基于注解的自动装配。一旦配置完成,我们可以对代码添加注解。spring3支持如下几种注解:
- @Autowired注解;
- @Inject注解;
- @Resource注解;
@Autowired
public void setInstrument(Instrument instrument){
this.instrument = instrument;
}
这样就可以移除对instrument通过属性<property>在XML中的装配了。
另外还可以使用@Autowired注解直接标注属性,并删除setting方法:
@Autowired
private Instrument instrument;
并且@Autowired不会受限于private关键字,仍然可以被自动装配。默认情况下,@Autowired具有强契约特征,如果没有Bean可以装配到@Autowired所标注的属性或参数中,自动装配就会失败,在这种情况下,可以通过设置@Autowired的required属性为false来配置自动装配是可选的。
@Autowired(require=false)
private Instrument instrument;
当有多个Bean满足装配条件时,也会抛出异常,为了确保我们需要的Bean被装配,要使用@qualifier注解。
@Autowired
@qualifier("guitar")
private Instrument instrument;
如上,@qualifier注解将尝试注入ID为guitar的Bean。
自动检测Bean
为了配置spring自动检测,需要使用<context:component-scan>元素代替<context:annotation-config>:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan
base-package="com.springinaction.springidol">
</context:component-scan> <!--bean declarations go here-->
</beans>
<context:component-scan>元素会扫描指定的包及其所有子包,并查找出能够自动注册为spring Bean的类。base-package属性标识了<context:component-scan>元素所有的包。
- @Component——通用的构造型注解,标识该类为spring组件;
- @Controller——标识将该类定义为spring mvc controller;
- @Repository——标识将该类定义为数据仓库;
- @Service——标识将该类定义为服务;
spring自动装配的更多相关文章
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- spring 自动装配 default-autowire="byName/byType"
<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType" ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring自动装配----注解装配----Spring自带的@Autowired注解
Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...
- Spring系列七:Spring 自动装配
相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...
- Spring自动装配(二)
为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...
- Spring自动装配歧义性笔记
Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...
- Spring自动装配与扫描注解
1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...
- Spring 自动装配及自动注册的相关配置
Spring支持好几种自动装配(Autowiring)的方式,以及自动扫描并注册Bean的配置(在beans.xml中配置). 下文我们进行一个小结. 1. <context: annotati ...
随机推荐
- 抓取百度音乐频道歌曲url
参考了 http://blog.csdn.net/banguijun/article/details/11815263 后写了一个抓取fm音乐url的模块,得到所有频道歌曲的url.缺点是百度会变更u ...
- obeject-c 与 swift 混编
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; color: #454545 } p.p2 ...
- Android Studio使用百度地图示例BaiduMapsApiASDemo
Android Studio使用百度地图示例BaiduMapsApiASDemo 用自己AVD下的debug.keystore替换掉项目中的debug.keystore 生成自己的签名 同样的方法生成 ...
- ajax实现jsonp跨域接口
HTML页面代码: <script type="text/javascript"> function UrlSearch(){ var name,value; var ...
- 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母
通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心 ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...
- Expected one result (or null) to be returned by selectOne(), but found 2
这个问题在于你查询sql返回结果是多个值.一个集合,但是你在service的实现层的dao都调用了.get方法.而是应该使用.getlist方法等.
- IIS内存溢出-设置IIS的应用程序池
在ASP.NET Web服务器上,ASP.NET所能够用到的内存,通常不会等同于所有的内存数量.在machine.config(C:/WINDOWS/Microsoft.NET/Framework/v ...
- SVN和CVS的区别
对版本控制就有了一定的理解,同时也应该知道SVN与CVS是比较流行的两款SCM工具.那么到底这两款工具有什么区别呢? 1.版本编号方面 例如,我们的版本库为A,其中有文件a,b,c. 在SVN中,新版 ...
- JS之函数表达式
度过一个愉快短暂的周末,又可以开始学习了!我爱学习,学习使人进步.今天学习函数表达式,着重学习下闭包函数. 函数表达式 可以在定义的函数声明之前调用它,但是不能在定义函数表达式之前调用它 /** * ...
- jqueryValidation使用
jq form表单前端校验可以使用jq插件jquery-validation.js.具体的使用方法: 1.引入文件: <link rel="stylesheet" href= ...