<Spring实战>3:最小化Spring XML配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了<Spring实战>3:最小化Spring XML配置相关的知识,希望对你有一定的参考价值。

1 自动装配 Bean 属性

1.1 4 种类型的自动装配

  • byName:把与 Bean 的属性具有相同名字或 ID 的其他 Bean 自动装配到 Bean 的对应属性中
  • byType:把与 Bean 的属性具有相同类型的其他 Bean 自动装配到 Bean 的对应属性中
  • constructor:把与 Bean 的构造器入参具有相同类型的其他 Bean 自动装配到 Bean 构造器的对应入参中
  • autodetect:首先尝试使用 constructor 进行自动装配,如果失败再尝试使用 byType 进行自动装配

1.2 默认自动装配

1.3 混合使用自动装配和显示装配

 

2 使用注解装配

  Spring 默认禁用注解装配,需要在配置中启用:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

Spring 3 支持多种不同的注解:

  • @Autowired:Spring 自带
  • @Inject:JSR-330
  • @Resource:JSR-250

 

2.1 使用@Autowired

  @Autowired 可以标注 setter 方法,代替 <property> 元素,会尝试对该方法执行 byType 自动装配。也可以标注需要自动装配 Bean 引用的任意方法。

  @Autowired 也可以标注构造器,即使在 xml 配置文件中没有使用 <constructor-arg> 元素,也进行自动装配。

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

问题:应用中必须只能有一个 Bean 适合装配到注解所标注的属性或参数中。

如果属性不一定要装配,null 也是可以接受的,可以设置 required 属性为 false,如:

@Autowired(required = false)
private Instrument instrument;

如果有多个 Bean 满足装配条件,可以配合 Spring 的 @Qualifier 注解来指定 Bean:

@Autowired
@Qualifier("saxophone")
private Instrument instrument;

此时将尝试注入 id 为 saxophone 的 Bean。

创建自定义的 Qualifier:

import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface StringedInstrument {
}
import com.hundsun.idol.Instrument;
import com.hundsun.idol.StringedInstrument;

@StringedInstrument
public class Guitar implements Instrument {
    @Override
    public void play() {
        //
    }
}

当使用 @StringedInstrument 对自动装配的属性进行限定:

@Autowired
@StringedInstrument
private Instrument instrument;

Spring 会把 Bean 缩小到只有被 @StringedInstrument 注解所标注的 Bean。

 

2.2 借助 @Inject 实现基于标准的自动装配

 

2.3 在注解注入中使用表达式

  @Value

 

3 自动检测 Bean

  在配置中增加 <context:annotation-config> 有助于完全消除 <constructor-arg> 和 <property> 元素,但仍需要显示定义 <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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描指定的包和子包,找出能够注册为 Spring Bean 的类-->
    <context:component-scan base-package="com.hundsun.idol"/>

</beans>

 

3.1 为自动检测标注 Bean

默认情况下,<context:component-scan> 查找使用构造型注解所标注的类,这些特殊的注解如下:

  • @Component:通用的构造型注解,标识为 Spring 组件
  • @Controller:标识为 Spring MVC controller
  • @Repository:标识为数据仓库
  • @Service:标识为服务
  • 使用 @Component 标注的任意自定义注解
@Component // 默认ID为无限定类名
public class Piano implements Instrument {

}

@Component("kenny") // ID显示命名为:kenny
public class Instrumentalist implements Performer {

}

 

3.2 过滤组件扫描

  通过为 <context:component-scan> 配置 <context:include-filter> 和 <context:exclude-fliter> 子元素,可以随意调整扫描行为。

  • annotation:过滤器扫描使用指定注解所标注的类,通过 expression 属性指定要扫描的注解
  • assignable:过滤器扫描派生于 expression 属性指定类型的类
  • aspectj:过滤器扫描与 expression 属性所指定的 AspectJ 表达式所匹配的类
  • custom:使用自定义的 TypeFilter 实现类,该类由 expression 属性指定
  • regex:过滤器扫描类的名称与 expression 属性指定的正则表达式匹配的类
<context:component-scan base-package="com.hundsun.idol">
        <!--自动注册所有Instrument的实现类-->
        <context:include-filter type="assignable" expression="com.hundsun.idol.Instrument"/>
</context:component-scan>

 

4 使用 Spring 基于 Java 的配置

 

--EOF--

 

以上是关于<Spring实战>3:最小化Spring XML配置的主要内容,如果未能解决你的问题,请参考以下文章

spring3升级到spring4.3

Spring 5企业级开发实战

《项目实战》springcloud 之eureka 注册中心生产者

redis实战_08_redis集群与spring整合

Spring Boot 构建 WAR和JAR 文件

spring与dubbo分布式REST服务开发实战