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

参考资料
小马哥核心编程思想
相关文章:
Spring-注解
Spring 注解分类 Spring 注解驱动模型 Spring 元注解 Documented Retention() Target() // 可以继承相关的属性 Inherited Repeatable()Spirng 模式注解 ComponentScan 原理 ClassPathScanningCandidateComponentProvider#findCandidateComponents public Set<BeanDefin…...
旧手机翻身成为办公利器——PalmDock的介绍也使用
旧手机有吧!!! 破电脑有吧!!! 那恭喜你,这篇文章可能对你有点用了。 介绍 这是一个旧手机废物利用变成工作利器的软件。可以在 Android 手机上快捷打开 windows 上的文件夹、文件、程序、命…...
期货交易的雷区
一、做自己看不懂的行情做交易计划一样要做有把握的,倘若你在盘中找机会交易,做自己看不懂的行情,即便你做进去了,建仓时也不会那么肯定,自然而然持仓也不自信,有点盈利就想平仓,亏损又想扛单。…...
东方通TongWeb结合Spring-Boot使用
一、概述 信创需要; 原状:原来的服务使用springboot框架,自带的web容器是tomcat,打成jar包启动; 需求:使用东方通tongweb来替换tomcat容器; 二、替换步骤 2.1 准备 获取到TongWeb7.0.E.6_P7嵌入版 这个文件,文件内容有相关对应的依赖包,可以根据需要来安装到本地…...
6.S081的Lab学习——Lab5: xv6 lazy page allocation
文章目录 前言一、Eliminate allocation from sbrk() (easy)解析: 二、Lazy allocation (moderate)解析: 三、Lazytests and Usertests (moderate)解析: 总结 前言 一个本硕双非的小菜鸡,备战24年秋招。打算尝试6.S081࿰…...
在WHM中如何调整max_post_size参数大小
今日我们在搭建新网站时需要调整一下PHP参数max_post_size 的大小,我们公司使用的Hostease的美国独立服务器产品默认5个IP地址,也购买了cPanel面板,因此联系Hostease的技术支持,寻求帮助了解到如何在WHM中调整PHP参数,…...
智能监控技术助力山林生态养鸡:打造智慧安全的养殖新模式
随着现代科技的不断发展,智能化、自动化的养殖方式逐渐受到广大养殖户的青睐。特别是在山林生态养鸡领域,智能化监控方案的引入不仅提高了养殖效率,更有助于保障鸡只的健康与安全。视频监控系统EasyCVR视频汇聚/安防监控视频管理平台在山林生…...
那些不起眼但很好玩的API合辑
那些不起眼但很好玩的API,为我们带来了许多出人意料的乐趣和惊喜。这些API可能看起来并不起眼,但它们却蕴含着无限的创意和趣味性。它们可以是一些小游戏API,让我们可以在闲暇时刻尽情娱乐;也可以是一些奇特的音乐API,…...
java —— 克隆对象、枚举
一、克隆对象 (一)在基本数据类型中,直接将对象 A 的值赋给对象 B,当更改对象 B 的时候,对象 A 的值保持不变。例如: public static void main(String[] args) {int a5;int ba; //将…...
STM32-GPIO八种输入输出模式
图片取自 江协科技 STM32入门教程-2023版 细致讲解 中文字幕 p5 【STM32入门教程-2023版 细致讲解 中文字幕】 https://www.bilibili.com/video/BV1th411z7sn/?p5&share_sourcecopy_web&vd_source327265f5c70f26411a53a9226af0b35c 目录 编辑 一.STM32的四种输…...
windows镜像虚拟机创建共享文件夹详细步骤 -- 和本地电脑传输文件
第一步:关闭客户机 第二步:右击“虚拟机名称”或菜单栏的“虚拟机”–>“设置” 网络适配器选择NAT或者其他的都可以 来到“选项”,启用共享文件夹,具体如下图:点击添加,添加主机文件夹。然后确定 第三步…...
通关!游戏设计之道Day18
过场动画,或者说根本没人看的东西 过场动画是一系列的动画或实时的动作序列,用来推进剧情制造大场面,烘托气氛,展示对话和角色成长,以及显现在某些情况下被玩家忽略的相关线索。 过场动画是一把双刃剑,一方…...
写Python时不用import,你会遭遇什么
from *** import *** 想必你已经再熟悉不过这样的python语法。 当你的 python 代码需要获取外部的一些功能(一些已经造好的轮子),你就需要使用到 import 这个声明关键字。import可以协助导入其他 module 。(类似 C 预约的 inclu…...
java网络:过滤器修改请求头
目录 一、gateway的全局过滤器 二、web的OncePerRequestFilter以及常见过滤器Filter 三、过滤器排序 一、gateway的全局过滤器 Component Slf4j public class GatewayAuthFilter implements GlobalFilter, Ordered {Overridepublic Mono<Void> filter(ServerWebExchan…...
yolov10 快速使用及训练
参考: https://docs.ultralytics.com/models/yolov10/ ultralytics其实大多数系列都能加载使用: 官方: https://github.com/THU-MIG/yolov10.git 代码参考: https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolov10-object-…...
CSS变量 -- var() 使用教程
目录 1 CSS变量的基本使用1.1 变量定义1.1 变量使用1.3 全局/局部变量 2 CSS变量的参数3 CSS变量的拼接和计算3.1 拼接3.2 计算 4 JS 修改 CSS变量 CSS 自定义属性(有时候也被称作CSS 变量或者级联变量),它的值可以在整个文档中重复使用。 复…...
python基础-数据结构-leetcode刷题必看-queue---队列-python的底层构建
文章目录 队列双端队列 deque底层存储deque接口1. __init__(self, iterable: Iterable[_T], maxlen: int | None None) -> None2. append(self, __x: _T) -> None3. appendleft(self, __x: _T) -> None4. copy(self) -> Self5. count(self, __x: _T) -> int6. …...
深入理解Spring Security:保护你的Web应用程序
深入理解Spring Security:保护你的Web应用程序 这听起来像是一部詹姆斯邦德电影,邦德试图进入坏家伙的藏身之处。坏家伙设置了一系列超级安全措施,有多层次的安全防御。邦德克服了其中一层,进入了隐藏处,但又遇到了下一个陷阱。他战胜了一个又一个陷阱,最终克服了所有障…...
【车载开发系列】Vector工具链的安装
【车载开发系列】Vector工具链的安装 【车载开发系列】Vector工具链的安装 【车载开发系列】Vector工具链的安装一. VectorDriver二. DaVinci_Developer三. DaVinci Configurator 一. VectorDriver Vector Driver Setup是Vector产品链中重要的驱动软件,所有的硬件设备进行连接…...
Windows系统部署YOLOv5 v6.1版本的训练与推理环境保姆级教程
文章目录 一 概述二 依赖环境(prerequisites)2.1 硬件环境2.2 软件环境 三 环境安装3.1 创建并激活虚拟环境3.2 安装Pytorch与torchvision3.3 校验Pytorch安装3.4 下载 YOLOv5 v6.1 源码3.5 安装 YOLOv5 依赖3.6 下载预训练模型3.7 安装其他依赖3.8 测试环境安装3.9 测试训练流…...
Android Wi-Fi 连接失败日志分析
1. Android wifi 关键日志总结 (1) Wi-Fi 断开 (CTRL-EVENT-DISCONNECTED reason3) 日志相关部分: 06-05 10:48:40.987 943 943 I wpa_supplicant: wlan0: CTRL-EVENT-DISCONNECTED bssid44:9b:c1:57:a8:90 reason3 locally_generated1解析: CTR…...
Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?
Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以? 在 Golang 的面试中,map 类型的使用是一个常见的考点,其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...
Nuxt.js 中的路由配置详解
Nuxt.js 通过其内置的路由系统简化了应用的路由配置,使得开发者可以轻松地管理页面导航和 URL 结构。路由配置主要涉及页面组件的组织、动态路由的设置以及路由元信息的配置。 自动路由生成 Nuxt.js 会根据 pages 目录下的文件结构自动生成路由配置。每个文件都会对…...
第25节 Node.js 断言测试
Node.js的assert模块主要用于编写程序的单元测试时使用,通过断言可以提早发现和排查出错误。 稳定性: 5 - 锁定 这个模块可用于应用的单元测试,通过 require(assert) 可以使用这个模块。 assert.fail(actual, expected, message, operator) 使用参数…...
如何将联系人从 iPhone 转移到 Android
从 iPhone 换到 Android 手机时,你可能需要保留重要的数据,例如通讯录。好在,将通讯录从 iPhone 转移到 Android 手机非常简单,你可以从本文中学习 6 种可靠的方法,确保随时保持连接,不错过任何信息。 第 1…...
EtherNet/IP转DeviceNet协议网关详解
一,设备主要功能 疆鸿智能JH-DVN-EIP本产品是自主研发的一款EtherNet/IP从站功能的通讯网关。该产品主要功能是连接DeviceNet总线和EtherNet/IP网络,本网关连接到EtherNet/IP总线中做为从站使用,连接到DeviceNet总线中做为从站使用。 在自动…...
高防服务器能够抵御哪些网络攻击呢?
高防服务器作为一种有着高度防御能力的服务器,可以帮助网站应对分布式拒绝服务攻击,有效识别和清理一些恶意的网络流量,为用户提供安全且稳定的网络环境,那么,高防服务器一般都可以抵御哪些网络攻击呢?下面…...
Spring AI与Spring Modulith核心技术解析
Spring AI核心架构解析 Spring AI(https://spring.io/projects/spring-ai)作为Spring生态中的AI集成框架,其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似,但特别为多语…...
Xen Server服务器释放磁盘空间
disk.sh #!/bin/bashcd /run/sr-mount/e54f0646-ae11-0457-b64f-eba4673b824c # 全部虚拟机物理磁盘文件存储 a$(ls -l | awk {print $NF} | cut -d. -f1) # 使用中的虚拟机物理磁盘文件 b$(xe vm-disk-list --multiple | grep uuid | awk {print $NF})printf "%s\n"…...
代码随想录刷题day30
1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...
