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——标识将该类定义为服务;

 

转载于:https://www.cnblogs.com/cjingzm/p/5078498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值