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. oracle+ibatis 批量插入-支持序列自增

    首先请先看我前面一篇帖子了解oracle批量插入的sql:[oracle 批量插入-支持序列自增] 我用的ibatis2.0,sqlMap文件引入的标签如下: <!DOCTYPE sqlMap ...

  2. Centos apache + mysql + usvn 配置svn 服务器

    1.遇到问题 提交异常:'svn/!svn/me'path not found http://www.myexception.cn/cvs-svn/1262826.html 更改http.conf 配 ...

  3. cookie处理

    有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除cookie信息. webdriver 操 ...

  4. 调用别人提供的WebService

    在开发过程中,许多时候需要使用到别人提供的WebService接口,使用其中的方法. 在调用别人提供的接口时,会得到接口使用的文档,其中包括接口的网络地址及方法作用等. 找到WebService的ws ...

  5. dojo布局(layout)

    使用BorderContainer和ContentPane实现布局 1.效果图如下: 2.HTML代码: <div id="appLayout" class="de ...

  6. PLSQLDeveloper 提示不能初始化?

    原因: oracle数据库是64位的,而 PLSQL Developer 只有32位的! 下载PLSQL_Developer地址: http://pan.baidu.com/share/link?sh ...

  7. 30分钟groovy快速入门并掌握(ubuntu 14.04+IntelliJ 13)

    本文适合于不熟悉 Groovy,但想快速轻松地了解其基础知识的 Java开发人员.了解 Groovy 对 Java 语法的简化变形,学习 Groovy 的核心功能,例如本地集合.内置正则表达式和闭包. ...

  8. 记录第一次搭建svn服务器

    搭建svn服务器需要另外的软件, 在此以32位的为例: 安装过程非常简单, 一直下一步下一步确定就好了, svn安装完毕再安装中文语言包, 安装完成后可以在设置里面找到中文简体选择就OK了 主要记录一 ...

  9. 新的框架,新的感觉ASP.NET MVC 分享一个简单快速适合新手的框架

    在ASP.NET世界中摸爬滚打好几年,用过了各种框架,在最初的ASP.NET web from 到现在的MVC 在起初的经典三层,到现在的MVC  IOC  注入 . 突然发现,有些时候真不是跟风用一 ...

  10. web测试方法总结

    链接地址:http://www.cnblogs.com/Jessy/p/3539638.html 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~ ...