前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Spring]04_最小化Spring XML配置

[Spring]04_最小化Spring XML配置

作者头像
静默虚空
发布2018-01-05 15:00:32
5700
发布2018-01-05 15:00:32
举报

4.1 自动装配 Bean

Spring 装配 bean 时,有时非常明确,就是需要将某个 bean 的引用装配给指定属性。

例如,若应用上下文中只有一个 javax.sql.DataSource 类型的 bean,那么任意一个依赖 DataSource 的其他 bean 就是需要这个 DataSource Bean。

为了应对这种明确的装配场景,Spring提供了自动装配(autowiring)。

4.1.1 四种类型的自动装配

Spring提供了4种自动装配策略

(1)byName——把与 Bean 的属性具有相同名字(或者ID)的其他 Bean 自动装配到 Bean 的对应属性中。如果没有跟属性的名字相匹配的 Bean, 则该属性不进行装配。

(2)byType——把与 Bean 的属性具有相同类型的其他 Bean 自动装配到 Bean 的对应属性中。如果没有跟属性的类型相匹配的 Bean,则该属性不被装配。

(3)constructor——把与 Bean 的构造器入参具有相同类型的其他 Bean 自动装配到 Bean 构造器的对应入参中。

(4)autodetect——首先尝试使用 constructor 进行自动装配。如果失败,再尝试使用 byType 进行自动装配。

在 Spring 中,使用自动装配的方式是在 bean 元素中注明 autowire 属性。

以 byName 为例

假如有一个歌手类,含两个属性,一个是演唱的曲目,一个是演奏的乐器。

<bean id="jack" class="com.spring.demo.Singer">
 <property name="song" value="Star" />
 <property name="instrument" ref="guitar" />
 </bean>

以上,是显示配置 instrument 的方法。

如果,定义定义吉他(Guitar) Bean 时,把 Bean 的 id 属性设置为 instrument:

<bean id="instrument" class="com.spring.demo.Guitar" />

可以发现Guitar Bean的 id 属性和 Singer Bean 的 name 属性的名字是一样的。

Spring 可以通过配置 autowire 属性,将两者自动关联起来,如下:

<bean id="jack" class="com.spring.demo.Singer" autowire="byName">
 <property name="song" value="Star" />
 </bean>

完成。

其他自动装配类型的使用方式和 byName 差不多,这里不再赘述。

4.1.2 默认自动装配

你是否会觉得给每一个 bean 都设置相同的 autowire 类型过于麻烦?

Spring允许在根元素 <beans> 上使用 fault-autowire 属性来设置所有 bean 的默认自动装配属性。

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
     default-autowire="byType">
 </beans>

在上例中, <beans> 标签下的所有 bean 如果不显示的指定 autowire 属性,那么它们的默认自动装配属性都是 byType 类型。

4.2 使用注解装配

从 Spring2.5 开始,引入了注解,从此可以使用注解自动装配 Bean 的属性。

使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细粒度的自动装配。

注意:Spring默认禁用注解装配,所以要使用它,必须先使用 <context:annotation-config> 来启用注解配置。

Spring3 支持几种不同的用于自动装配的注解:

  • Spring自带的 @Autowired 注解
  • JSR-330 的 @Inject 注解
  • JSR-250 的 @Resource 注解

4.2.1 @Autowired

对方法进行标注

@Autowired 可以标注需要自动装配Bean引用的任意方法(set方法、普通方法、构造方法)。

public class Audience {
 private Movie movie;
 
 // 标注构造方法
     @Autowired
 public Audience(Movie movie) {
 this.movie = movie;
     }
 
 // 标注set方法
     @Autowired
 public void setMovie(Movie movie) {
 this.movie = movie;
     }
 
 // 标注构造方法
  public void watchMovie(Movie movie) {
 this.movie = movie;
         System.out.println("Watch " + movie.toString());
     }
 }

对属性进行标注

可以使用 @Autowired 直接标注属性,并删除 setter 方法。

@Autowired
 private Movie movie;

@Autowired 不会受限于 private 关键字,即使属性是私有的,它仍然可以被自动装配。

可选的自动装配

默认情况下,@Autowired 具有强契约特征,其所标注的属性或参数必须是可装配的。

如果没有 Bean 可以装配到 @Autowired 所标注的属性或参数中,自动装配失败,抛出 NoSuchBeanDefinitionException

如果,属性不一定非要装配,null 值也是可以接受的。这种情况下,可以通过设置 @Autowiredrequired 属性为 false 来配置自动装配是可选的。

@Autowired(required=false)
 private Movie movie;

注意:require 属性可以用于 @Autowired 所使用的任意地方。

但是,当使用构造器装配时,只有一个构造器可以将 @Autowired 的 required 属性设置为 true,其他的只能置为 false。

@Qualifier

如果有多个 Bean 完全满足装配条件,并且都可以被装配到属性或参数中。在这种情况下,@Autowired 注解没有办法选择哪一个

Bean 才是真正需要的。这时,可以使用 @Qualifier 注解来缩小选择范围。

@Autowired
 @Qualifier("cartoon")
 private Movie movie;

以上所示,@Qualifier 尝试注入ID为 cartoon 的 Bean。

创建自定义的Qualifier

如下演示了如何自定义一个限定器:

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 @Qualifier
 public @interface PrivateMovie {
 }

有了自定义的注解,就可以用它来代替 @Qualifier:

@Autowired
 @PrivateMovie
 private Movie movie;

4.2.2 @Inject

@Inject 注解是 JSR-330 的核心部件。该注解几乎可以完全替换 Spring 的 @Autowired 注解。

和 @Autowired 一样,@Inject 可以用来自动装配属性、方法和构造器。

与 @Autowired 不同的是,@Inject 没有 required 属性。因此,@Inject 注解所标注的依赖关系必须存在,如果不存在,则会抛出异常。

Provider

JSR-330中有个技巧:不直接注入一个引用,而是注入一个 Provider,Provider 接口可以实现 Bean 引用的延迟注入以及注入 Bean 的多个实例等功能。

4.3 自动检测 Bean

<context:annotation-config> 有助于完全消除 Spring 配置的 <property><constructor-arg> 元素,但是仍然需要使用 <bean> 元素显示定义 Bean。

<context:component-scan> 除了完成与 <context:annotation-config> 一样的工作,还允许 Spring 自动检测 Bean 和定义 Bean。

<?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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-packet="com.spring.demo">
 </context:component-scan>
</beans>

<context:component-scan> 元素会扫描指定的包及其所有子包,并查找出能够自动注册为 Spring Bean 的类。

base-package 属性标识了 <context:component-scan> 元素所扫描的包。

4.3.1 为自动检测标注 Bean

默认情况下,<context:component-scan> 查找使用构造器注解所标注的类。

这些特殊的注解如下:

  • @Component
  • @Controller
  • @Repository
  • @Service
  • 使用@Component标注的任意自定义注解

4.3.2 过滤组件扫描

<context:component-scan> 中有两个子元素,可以用来调整扫描行为:

  • <context:include-filter>——包含过滤器
  • <context:exclude-filter>——排除过滤器

<context:component-scan base-packet="com.spring.demo">
 <context:include-filter type="assignable" expression="com.spring.demo.Movie" />
</context:component-scan>

上例中的,typeexpression 属性一起协作来定义组件扫描策略。

type 有五种选择:

过滤器类型

描述

annotation

过滤器扫描使用指定注解所标注的那些类。通过 expression 属性指定要扫描的注解

assignable

过滤器扫描派生于 expression 属性所指定类型的那些类

aspectj

过滤器扫描与 expression 属性所指定的 AspectJ 表达式所匹配的那些类

custom

使用自定义的org.springframework.core.type.TypeFilter 实现类,该类由 expression 属性指定

regex

过滤器扫描类的名称与 expression 属性所指定的正则表达式所匹配的那些类

参考资料

Spring实战(第3版)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-06-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 4.1 自动装配 Bean
    • 4.1.1 四种类型的自动装配
      • 4.1.2 默认自动装配
      • 4.2 使用注解装配
        • 4.2.1 @Autowired
          • 对方法进行标注
          • 对属性进行标注
          • 可选的自动装配
          • @Qualifier
        • 4.2.2 @Inject
          • Provider
      • 4.3 自动检测 Bean
        • 4.3.1 为自动检测标注 Bean
          • 4.3.2 过滤组件扫描
          • 参考资料
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档