<spring实战>3:最小化springxml配置

     2022-05-09     723

关键词:

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--

 

maven实战笔记

maven实战(三)——多模块项目的POM重构1.重复举个栗子1<dependency>2<groupId>org.springframework</groupId>3<artifactid>spring-beans</artifactId>4<version>2.5</version>5</dependen 查看详情

spring专题「开发指南」夯实实战基础功底之解读logback-spring.xml文件的详解实现(代码片段)

logback的maven配置<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.3</version></dependency><dependency><grou 查看详情

案例实战爬虫url去重实战-springboot2.x+guava布隆过滤器

1.爬虫URL去重实战-SpringBoot2.x+Guava布隆过滤器创建项目加入maven依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot<... 查看详情

<spring实战>2:装配bean

1声明Bean1.1创建Spring配置  Spring容器提供两种配置Bean的方式:xml配置和基于注解配置。  Spring配置文件:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org 查看详情

spring3升级到spring4.3(代码片段)

...个广告:项目中有一个外购的老的系统,使用的spring3,太老了,也不符合公司安全要求,固将其升级为spring4。这里就记录一下修改内容。升级spring版本<spring.version>3.2.18.RELEASE</spring.version>升级到<spr... 查看详情

jeecgboot与mongodb集成实战文档

坐标引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>升级积木报表jeecgboot2.4.6/3.0等版本集成mong 查看详情

jeecgboot与mongodb集成实战文档(代码片段)

坐标引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>升级积木报表jeecgboot2.4.6/3.0等版本集成mongodb会报mongoTemplate错误,官方已经提供了解决方... 查看详情

前方高能预警!阿里大佬出品“spring实战学习笔记”震撼来袭

...对Spring展开了系统的讲解,包括Spring之旅、装配Bean、最小化SpringXML配置、面向切面的Spring、征服数据库、事务管理、使用SpringMVC构建Web应用程序、使用SpringWebFlow、保护Spring应用、使用远程服务、为Spring添加REST功能、Spring消... 查看详情

css实战3

 1. z-index 层级  div层<!DOCTYPEhtml><html><headlang="en"><metacharset="UTF-8"><title></title><style>/*.test{width:150px;height:300px;bo 查看详情

spring实战-手动创建spring项目结构

环境:MacOS+IntelliJIDEA2019.3.1(UltimateEdition)1、创建存放项目的文件夹2、创建pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:x 查看详情

前方高能预警!阿里大佬出品“spring实战学习笔记”震撼来袭!(代码片段)

...对Spring展开了系统的讲解,包括Spring之旅、装配Bean、最小化SpringXML配置、面向切面的Spring、征服数据库、事务管理、使用SpringMVC构建Web应用程序、使用SpringWebFlow、保护Spring应用、使用远程服务、为Spring添加REST功能、Spring消... 查看详情

springboot整合rabbitmq:5种模式实战(代码片段)

一、环境准备1、pom依赖<!--父工程依赖--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</vers 查看详情

使用spring的命名空间p装配属性-摘自《spring实战(第3版)》

...t;元素为Bean的属性装配值和引用并不太复杂。尽管如此,Spring的命名空间p提供了另一种Bean属性的装配方式,该方式不需要配置如此多的尖括号。命名空间p的schemaURL为http://www.springframework.org/schema/p。如果你想使用命名空间p,只... 查看详情

activiti7工作流引擎:实战篇准备工作(代码片段)

...0c;但是现在我只能买块豆腐。1.pom.xml引入最新版本activiti-spring-boot-starter依赖和spring-boot-starter-security。<dependency><groupId>org.activiti</groupId><artifactId>activiti-spring-boot-starter</artifactId><version>7.1.0.M6</version>&... 查看详情

springboot-整合-redisson(代码片段)

...一个分布式锁,还有很多功能,需要参考文档去springboot-整合https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter#spring-boot-starter配置yml1.先配置这个,配置的时候需要注意<dependency><groupId>org.redisson</gr... 查看详情

springboot揭秘与实战发布与部署-远程调试

...增加远程调试依赖。<plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring 查看详情

spring事务配置实战:过滤无需事务处理的查询之类操作

<tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="delete*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>< 查看详情

springcache实战遇坑

1.SpringCache实战遇坑1.1.pom主要是以下两个<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--配合r 查看详情