avatar

一条在知识海洋的咸鱼

这个家伙很懒,啥也没有留下😋

  • 首页
  • Linux
  • OCJP
  • Java核心技术卷
  • J2EE相关标准
  • 深入理解Java虚拟机
  • NIO与SOcket编程技术指南
  • Java多线程编程核心技术
  • Redis开发与运维
  • Spring Cloud Alibaba 微服务原理与实践
  • DevOps
  • Docker
  • MySQL必知必会
  • AI自学路线
  • Spring Boot 编程思想(核心篇)
主页 Spring注解驱动设计模式
文章

Spring注解驱动设计模式

发表于 2021-02-26 更新于 2026-06- 10
作者 Administrator
11~14 分钟 阅读
  • Spring @Enable模块驱动
    • @Enable模块驱动
    • 自定义@Enable模块驱动
      • 基于注解驱动实现@Enable模块
      • 基于“接口编程”实现@Enable模块
    • @Enable模块驱动原理
  • Spring Web 自动装配

Spring @Enable模块驱动

image.png
从Spring Framework 3.x开始,支持“@Enable模块驱动”

  • 模块:具备相同另据的功能组件集合,组合形成的一个独立的单元,如MVC、AspwctJ代理、Async异步处理模块等

@Enable模块驱动

Spring Framework 3.1种,框架实现者有意识地形成了一种新的设计模式,引入
“@Enable模块驱动”用于简化装配、实现按需装配、屏蔽组件集合装配的细节,在后续的Spring Framework、Spring Boot和Spring Cloud中都使用

。
该模式必须手动触发,实现的Annotation必须标注在某个配置Bean种,同时实现该模式的成本相对较高,尤其是在理解其中原理和加载机制及单元测试方面

  • Spring Framework的@Enable注解及激活模块
    1.@EnableWebMvc:WebMVC模块
    2.@EnableTransactionManagement:事务管理模块
    3.@EnableCaching:Caching模块
    4.@EnableMBeanExport:JMX模块(Java扩展管理)
    5.@EnableAsync:异步处理模块
    6.@EnableWebFlux:Web Flux模块
    7.@EnableAspectJAutoProxy:AspectJ代理模块
  • Spring Boot的@Enable注解及激活模块
    1.@EnableAutoConfiguration:自动装配模块
    2.@EnableManagementContext:Actuator管理模块
    3.@EnableConfigurationProperties:配置属性绑定模块
    4.@EnableOAuth2Sso:OAuth2单点登录模块
  • Spring Cloud的@Enable注解及激活模块
    1.@EnableEurekaServer:Eureka服务器模块
    2.@EnableConfigServer:配置服务器模块
    3.@EnableFeignClients:Feign客户端模块
    4.@EnableZuulProxy:服务网关Zuul模块
    5.@WnableCircuitBreaker:服务熔断模块

自定义@Enable模块驱动

@Enable模块驱动实现方式大致分为2类

  • 注解驱动:较容易(@Configuration类和@Bean方法声明类)
  • 接口编程:较难(ImpoetSelector或ImportBeanDefinitionRegistrar的实现类)
    无论如何实现均需要@Impor作为元注解,@ImportResource用于导入XML配置文件,@Import用于导入一个或多个ConfigurationClass,将其注册为Spring Bean,也可以用于声明至少一个@Bean方法的类,以及ImpoetSelector或ImportBeanDefinitionRegistrar的实现类

基于注解驱动实现@Enable模块

以@EnableWebMvc为例的实现样板
image.png
image.png

  • 自定义注解驱动
@EnableHelloWorld
@Configuration
public class EnableHelloWorldBootstrap {

    public static void main(String[] args) {
        // 构建 Annotation 配置驱动 Spring 上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册 当前引导类(被 @Configuration 标注) 到 Spring 上下文
        context.register(EnableHelloWorldBootstrap.class);
        // 启动上下文
        context.refresh();
        // 获取名称为 "helloWorld" Bean 对象
        String helloWorld = context.getBean("helloWorld", String.class);
        // 输出用户名称:"Hello,World"
        System.out.printf("helloWorld = %s \n", helloWorld);
        // 关闭上下文
        context.close();
    }
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldConfiguration.class) // 导入 HelloWorldConfiguration
public @interface EnableHelloWorld {
}
@Configuration
public class HelloWorldConfiguration {

    @Bean
    public String helloWorld() { // 创建名为"helloWorld" String 类型的Bean
        return "Hello,World";
    }
}

image.png
可见Spring自动装配加载了helloWorld Bean

基于“接口编程”实现@Enable模块

//TODO

@Enable模块驱动原理

//TODO

Spring Web 自动装配

“自动装配”是Spring Boot的三大特征,可分为Web应用和非Web应用



Spring Boot 编程思想(核心篇)
Spring
许可协议: 
分享

相关文章

2月 26, 2021

Spring注解驱动设计模式

Spring #Enable模块驱动@Enable模块驱动Spring #Enable模块驱动从Spring Framework 3.x开始,支持“@Enable模块驱动”模块:具备相同另据的功能组件集合,组合形成的一个独立的单元,如MVC、AspwctJ代理、Async异步处理模块等@Enable

2月 25, 2021

注解驱动编程

注解驱动发展史注解驱动启蒙时代:Spring Framework 1.x注解驱动过渡时代:Spring Framework 2.x注解驱动黄金时代:Spring Framework 3.xSpring Framework的IOC(控制反转)和DI(依赖注入)目的是为了应用系统不应以Java代码的方式

2月 24, 2021

Production-Ready特性

Spring Boot的自动装配及starter的引入,大量的Spring Bean的装配变成黑盒。为了支持以配置化的方式调整应用行为,如Web服务端口等,Spring Boot提供了Production-Ready

下一篇

SpringBoot编程思想

上一篇

注解驱动编程

最近更新

  • 55 异常兜底:工具失败、非法输出、空检索与超时
  • 54 Agent Memory:短期记忆、用户上下文与摘要
  • 53 Human-in-the-loop:让关键动作暂停、确认与恢复
  • 52 工单 Tool:字段校验、创建结果与失败补偿
  • 51 订单 Tool:Mock、鉴权、状态解析与脱敏

热门标签

java基础 微服务 maven Spring Tomcat DDD Linux Linux基础 SQL基础 数据结构算法

目录

©2026 一条在知识海洋的咸鱼. 保留部分权利。

使用 Halo 主题 Chirpy