Spring 注解
2022-10-26 15:51:39 0 举报
AI智能生成
登录查看完整内容
Spring 注解
作者其他创作
大纲/内容
@Transactional 注解
@ManagedResource
注解驱动启蒙时代:Spring Framework 1.x
@Component
@Repository
注解驱动过渡时代:Spring Framework 2.x
@ComponentScan
@Bean
@Lazy
@Primary
@Configuration
@Import
@Resource
。。。
注解驱动黄金时代:Spring Framework 3.x
@Profile(关于 Conditional 重构)
注解驱动完善时代:Spring Framework 4.x
@Indexed
注解驱动当下时代:Spring Framework 5.x
Spring 注解驱动编程发展历程
Spring 模式注解
装配注解
依赖注入注解
Spring 核心注解场景分类
元注解(Meta-Annotations)
Spring 模式注解(Stereotype Annotations)
Spring 组合注解(Composed Annotations)
Spring 注解属性别名和覆盖(Attribute Aliases and Overrides)
参考文档:元注解 https://github.com/spring-projects/spring-framework/wiki/Spring-Annotation-Programming-Model
Spring 注解编程模型
元注解是在另一个注解上声明的注解。可以修饰其他注解
定义
java.lang.annotation.Documented,[@Documented作用](https://blog.csdn.net/howeres/article/details/123340840)
java.lang.annotation.Inherited,@[Inherited](https://www.yht7.com/news/80973)
java.lang.annotation.Repeatable @[Repeatable](https://cloud.tencent.com/developer/article/1579167)
举例说明
代码示例
Spring 元注解
只要是被 @Component 标注或元标注的注解,就叫做模式注解。
定义
被 @Component 注解标注的具有派生性
@Repository 单一派生性
@Service
@Controller
@SpringBootConfiguration 多层次派生性
@Component 注解的派生性
@ComponetScan 注解扫描组件的源码分析
@ComponetScan 注解扫描组件的源码分析流程图
@ComponetScan 注解扫描组件的源码分析文章
@ComponetScan 注解扫描组件的源码分析文章
Spring 模式注解
Spring 组合注解(Composed Annotations)中的元注允许是 Spring 模式注解(Stereotype Annotation)与其他 Spring 功能性注解的任意组合。
@SpringBootApplication注解分析
组合注解示例
Spring 组合注解
当两个属性互相 @AliasFor ,可以使其具有相同的效果。@AliasFor 更类似于对属性进行引用,让该属性具有 @AliasFor 字段的作用
使用的时候,以下两种情况的运行效果一致。
示例
显性别名
@AliasFor 中标记组件扫描的类,引用的是那个字段
使用的时候,两种效果也一致
隐式别名
传递隐式别名
Spring 注解别名
注解和元注解出现属性相同时,注解中的属性会覆盖掉元注解中的属性
隐性覆盖
注解可以使用 @AliasFor 去标注元注解的属性,这样可以覆盖掉元注解的属性值
显性覆盖
Spring 注解属性覆盖
@Enable 模块驱动是以 @Enable 为前缀的注解驱动编程模型。所谓“模块”是指具备相同领域的功能组件集合,组合所形成⼀个独⽴的单元。⽐如 Web MVC 模块、AspectJ代理模块、Caching(缓存)模块、JMX(Java 管理扩展)模块、Async(异步处理)模块等。
@EnableWebMvc
@EnableTransactionManagement
@EnableCaching
@EnableMBeanExport
@EnableAsync
Spring 中的 Enable 注解
驱动注解:@EnableXXX (语义化,实际上不用这个名字也可以)
导入注解:@Import 具体实现
基于 Configuration Class
基于 ImportSelector 接口实现
基于 ImportBeanDefinitionRegistrar 接口实现
具体实现
自定义 @Enable 模块注解
Spring Enable 模块
关联对象 - org.springframework.core.env.Environment 中的 Profiles
实现变化:从 Spring 4.0 开始,@Profile 基于 @Conditional 实现
基于配置条件注解 - @org.springframework.context.annotation.Profile
关联对象 - org.springframework.context.annotation.Condition 具体实现
基于编程条件注解 - @org.springframework.context.annotation.Conditional
可以从 ConditionContext 中获取 BeanFactory 等对象
上下文对象 - org.springframework.context.annotation.ConditionContext
条件评估器,是否应该予以跳过
条件判断 - org.springframework.context.annotation.ConditionEvaluator
Configuration 的解析阶段
BeanDefinition 的定义阶段
配置阶段 -org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase
org.springframework.context.annotation.ConfigurationClassParser
判断入口 - org.springframework.context.annotation.ConfigurationClassPostProcessor
@Conditional 实现原理
ProfileDemo
Profile使用
Condition 类需要实现 Condition 接口并重写 matches 方法,我们可以在方法中去设置标注该注解的 Bean 的 Profile 的值。
等同于设置 @Profile(\"odd\")
OddProfileCondition
等同于设置 @Profile(\"even\")
EvenProfileCondition
**ProfileDemo** 示例,和上面的 @Profile(\"odd\")、@Profile(\"even\") 是同样的效果
Condition 使用
ConditionEvaluator 条件判断
Spring 条件注解
Spring Boot 注解
Spring Cloud 注解
Spring 模式注解有哪些?
@EventListener 的工作原理?
面试题
Spring 注解
收藏
0 条评论
回复 删除
下一页