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自动装配的更多相关文章

  1. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  2. spring 自动装配 default-autowire=&quot;byName/byType&quot;

    <PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"   ...

  3. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  4. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

  5. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  6. Spring自动装配(二)

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...

  7. Spring自动装配歧义性笔记

    Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...

  8. Spring自动装配与扫描注解

    1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...

  9. Spring 自动装配及自动注册的相关配置

    Spring支持好几种自动装配(Autowiring)的方式,以及自动扫描并注册Bean的配置(在beans.xml中配置). 下文我们进行一个小结. 1. <context: annotati ...

随机推荐

  1. linux无线配置

    1. 找 wifi 热点 sudo iwlist scan 2. 生成热点的配置(wpa2加密) sudo wpa_passphrase "wifi热点名称" "wpa密 ...

  2. win7 双屏双任务栏

    扩展屏幕下都显示任务栏!!! 第一步:Dual Monitor Taskbar 下载 下载链接:链接: http://pan.baidu.com/s/1pKxYUFL 密码: gu5c 第二步:安装完 ...

  3. Endless Sky源码学习笔记-4

    事件处理: 事件包括:debug模式切换.切换到登陆窗口.退出.窗口大小变化.全屏切换和游戏中的鼠标键盘输入.处理方式分为两类,前几个为简单的if处理,最后一个涉及到游戏中的控制和交互,且事件由每一个 ...

  4. 运行WPS遇到的问题及解决办法

    http://www2.mmm.ucar.edu/wrf/OnLineTutorial/Class/cases/find_the_bugs.php# For this case we are goin ...

  5. ffmpeg 音频转换: use ffmpeg convert the audio from stereo to mono without changing the video part

    To convert the audio from stereo to mono without changing the video part, you can use FFmpeg: ffmpeg ...

  6. 模仿win10样式,基于jquery的时间控件

    工作需要,写了一个基于jquery的时间控件,仿win10系统时间控件格式. 目前基本功能都有了,但时间格式只实现少数,但由于结构设计已经充分优化,填充起来非常容易. 这个控件相对网上其他的时间控件, ...

  7. asp.net 琐记

    Page的AutoEventWireup作用是是否引发PreInit Load PreRender Unload几个页面处理流程事件 和控件的事件处理函数无关

  8. Spring HTTP下载

    (1)HTTP 协议可以在客户端和服务器之间传递任何类型的文件. HTTP协议下载文档到客户端时候, 必须通过响应头Content-Type设置文件类型. 例如: contentType=text/h ...

  9. ZooKeeper概述(转)

    译自http://zookeeper.apache.org/doc/trunk/zookeeperOver.html ZooKeeper是一个用于分布式应用的开源分布式协调服务.它提供了简单的原语集合 ...

  10. ArcMap中地图输出(Options)选项显示不完整

    首先,我开始遇到的时候,认为是高分辨屏幕的问题,所以修改了屏幕的分辨率,结果并没有改变. 然后,认为是对话窗口的显示,修改字体大小,也没有显示完整. 最后,是修改了ArcGIS的注册表才解决.解决方式 ...