Spring Boot 自动装配

it2025-03-13  24

Spring Boot 自动装配

Spring模式注解装配

模式注解是一种用于声明在应用中扮演“组件”角色的注解。如 Spring Framework 中的 @Repository 标注在任何类上 ,用 于扮演仓储角色的模式注解。 @Component 作为一种由 Spring 容器托管的通用模式组件,任何被 @Component 标准的组件均为组件扫描的候选对象。类 似地,凡是被 @Component 元标注(meta-annotated)的注解,如 @Service ,当任何组件标注它时,也被视作组件扫 描的候选对象

装配方式

<context:component-scan> 方式

<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 激活注解驱动特性 --> <context:annotation-config/> <!-- 找寻被 @Component 或者其派生 Annotation 标记的类(Class),将它们注册为 Spring Bean --> <context:component-scan base-package="com.*.pushpool"/> </beans>

@ComponentScan 方式

@ComponentScan(basePackages = "com.*.spring.boot") public class SpringConfiguration { ... }

自定义模式注解

@Component 具有派生性和层次性

派生性 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repository public @interface FirstLevelRepository { String value() default ""; }

2.层次性

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @FirstLevelRepository public @interface SecondLevelRepository { String value() default ""; }

Spring @Enable 模块装配

Spring Framework 3.1 开始支持”@Enable 模块驱动“。所谓“模块”是指具备相同领域的功能组件集合, 组合所形成一个独立 的单元。比如 Web MVC 模块、AspectJ代理模块、Caching(缓存)模块、JMX(Java 管 理扩展)模块、Async(异步处 理)模块等。

@EnableWebMvc Web MVC 模块 @EnableTransactionManagement 事务管理模块 @EnableCaching Caching 模块 @EnableMBeanExport JMX 模块 @EnableAsync 异步处理模块 @EnableWebFlux Web Flux 模块 @EnableAspectJAutoProxy AspectJ 代理模块

自定义 @Enable 模块 基于注解驱动实现 - @EnableHelloWorld

HelloWorldImportSelector -> HelloWorldConfiguration -> HelloWorld

public class HelloWorldImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{HelloWorldConfiguration.class.getName()}; } } public class HelloWorldConfiguration { @Bean public String helloWorld() { // 方法名即 Bean 名称 return "Hello,World"; } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import(HelloWorldImportSelector.class) public @interface EnableHelloWorld { }

Spring 条件装配

从 Spring Framework 3.1 开始,允许在 Bean 装配时增加前置条件判断

条件注解举例

@Profile 配置化条件装配 3.1 @Conditional 编程条件装配 4.0

Spring Boot 自动装配

在 Spring Boot 场景下,基于约定大于配置的原则,实现 Spring 组件自动装配的目的。其中使用了 底层装配技术

Spring 模式注解装配 Spring @Enable 模块装配 Spring 条件装配装配 Spring 工厂加载机制

实现类: SpringFactoriesLoader 配置资源: META-INF/spring.factories 自动装配举例 参考 META-INF/spring.factories

实现方法

1. 激活自动装配 - @EnableAutoConfiguration 2. 实现自动装配 - XXXAutoConfiguration 3. 配置自动装配实现 - META-INF/spring.factories
最新回复(0)