码迷,mamicode.com
首页 > 编程语言 > 详细

spring自动装配

时间:2015-12-26 22:15:56      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

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自动装配

标签:

原文地址:http://www.cnblogs.com/cjingzm/p/5078498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!