当前位置: 首页 > news >正文

Spring-注解

Spring 注解分类

在这里插入图片描述
在这里插入图片描述

Spring 注解驱动模型

Spring 元注解
@Documented
@Retention()
@Target()
// 可以继承相关的属性
@Inherited
@Repeatable()
Spirng 模式注解

@ComponentScan 原理
ClassPathScanningCandidateComponentProvider#findCandidateComponents

public Set<BeanDefinition> findCandidateComponents(String basePackage) {if (this.componentsIndex != null && indexSupportsIncludeFilters()) {return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);}else {return scanCandidateComponents(basePackage);}
}
组合注解

比如@RestController,@SpringBootApplication
注解转换为AnnotationAttributes然后进行合并统一

Spring属性别名

显性别名:
在这里插入图片描述
隐式别名:
这个组合注解
继承EnableAutoConfiguration的属性,用隐式别名来表达,也就是直接拿过来不需要改变它的值:
在这里插入图片描述
如果说我们要补充语义,引用某个属性,自己定义新的属性名称来引用父类的:
在这里插入图片描述
举例如果我们要派生这个注解,我们不可能在注解里面写死扫描路径,这个时候就要通过隐式别名来继承注解ComponentScan的属性,这样我们就可以配置scanBasePackages

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ComponentScan
public @interface MyComponentScan {@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")String scanBasePackages() default "";}

我们也可以这样:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ComponentScan
public @interface MyComponentScan {// 传递性别名 basePackages -> value value -> scanBasePackages@AliasFor(annotation = ComponentScan.class, attribute = "value")String scanBasePackages() default "";
}

Spring属性覆盖

注解和原注解出现同名属性的时候会覆盖原注解的属性

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ComponentScan
public @interface MyComponentScan {@AliasFor(annotation = ComponentScan.class, attribute = "value")String scanBasePackages() default "";
}@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@MyComponentScan
public @interface MyComponentScan2 {// 显示覆盖String[] scanBasePackages() default "";// 隐式覆盖 @AliasFor(attribute = "scanBasePackages")String[] packages() default {};
}
@MyComponentScan2(packages = {"com.yong.annotationpkg"})
// packages 覆盖scanBasePackages scanBasePackages覆盖MyComponentScan scanBasePackages scanBasePackages 引用的是ComponentScan

Spring @Enable 模块驱动

在这里插入图片描述
案例1通过@Configration实现

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DemoConfig.class)
public @interface EnableDemo {}
@Configuration
public class DemoConfig {@BeanPerson person() {Person person = new Person();person.setName("liy");person.setId(11L);return person;}
}@EnableDemo
public class EnableAnnotationDemo {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(EnableAnnotationDemo.class);context.refresh();Person bean = context.getBean(Person.class);System.out.println(bean);}
}

案例2通过ImportSelector接口实现:

public class DemoImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{"com.yong.annotationpkg.DemoConfig"};}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DemoImportSelector.class)
public @interface EnableDemo {}
@EnableDemo
public class EnableAnnotationDemo {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(EnableAnnotationDemo.class);context.refresh();Person bean = context.getBean(Person.class);System.out.println(bean);}
}

案例3通过

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DemoImportBeanDefinitionRegister.class)
public @interface EnableDemo {}
public class DemoImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {AnnotatedBeanDefinition annotatedBeanDefinition = new AnnotatedGenericBeanDefinition(DemoConfig.class);registry.registerBeanDefinition("demoConfig", annotatedBeanDefinition);}
}
Spring 条件注解

在这里插入图片描述

@Configuration
public class ProfileDemo {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(ProfileDemo.class);ConfigurableEnvironment environment = context.getEnvironment();// 兜底方案environment.setDefaultProfiles("odd");// 激活方案environment.setActiveProfiles("even");context.refresh();Object odd = context.getBean(Integer.class);System.out.println(odd);context.close();}// 激活不同的环境注册不同的Bean@Bean@Profile("odd")public Integer odd() {return 2;}@Bean@Profile("even")public Integer even() {return 1;}
}

自定义实现:

public class EvenProfileCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Environment environment = context.getEnvironment();return environment.acceptsProfiles("even");}
}
@Bean@Conditional(EvenProfileCondition.class)public Integer even() {return 1;}

ConditionEvaluator#shouldSkip
在这里插入图片描述

参考资料
小马哥核心编程思想

相关文章:

  • vue 打印、自定义打印、页面打印、隐藏页眉页脚
  • kotlin基础之协程
  • 【5.基础知识和程序编译及调试】
  • 第十三章 进程与线程
  • 探秘URL的奥义:JavaScript中轻松获取页面参数值的N种姿势【含代码示例】
  • 基于文本来推荐相似酒店
  • 最新文章合集
  • 前端加密的方式汇总
  • 【OpenCV 基础知识 13】高斯平滑处理图像
  • vue实现页面渲染时候执行某需求
  • Vue 前端加框 给div加红色框框 js实现
  • 【PB案例学习笔记】-12秒表实现
  • 【PostgreSQL17新特性之-事务级别超时参数transaction_timeout】
  • 虚拟机改IP地址
  • NIFT和BMP批量互相转换(matlab)
  • 2017年终总结、随想
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • JavaScript类型识别
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • MobX
  • orm2 中文文档 3.1 模型属性
  • Protobuf3语言指南
  • React系列之 Redux 架构模式
  • Redis 中的布隆过滤器
  • Redis在Web项目中的应用与实践
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • 将回调地狱按在地上摩擦的Promise
  • 近期前端发展计划
  • 排序算法之--选择排序
  • 入手阿里云新服务器的部署NODE
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 译有关态射的一切
  • 【干货分享】dos命令大全
  • 回归生活:清理微信公众号
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​2020 年大前端技术趋势解读
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #进阶:轻量级ORM框架Dapper的使用教程与原理详解
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • $.ajax()方法详解
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (16)Reactor的测试——响应式Spring的道法术器
  • (33)STM32——485实验笔记
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (纯JS)图片裁剪
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)十分简易快速 自己训练样本 opencv级联haar分类器 车牌识别
  • (一)使用Mybatis实现在student数据库中插入一个学生信息