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

SpringBoot前置知识02-spring注解发展史

springboot前置知识01-spring注解发展史

spring1.x

spring配置只能通过xml配置文件的方式注入bean,需要根据业务分配配置文件,通过import标签关联。

spring1.2版本出现@Transactional注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userService" class="com.shaoby.service.UserServiceImpl"></bean>
</beans>
public class UserServiceImpl {
}
public class StartApp {public static void main(String[] args) {ClassPathXmlApplicationContext cp = new ClassPathXmlApplicationContext("applicationContext.xml");Object userService = cp.getBean("userService");System.out.println(userService);}
}

spring2.X

  1. 简化配置,通过compoment-scan和@Component(2.0)、 @Controller(2.5)、@Service(2.5)、 @Repository(2.5)注解实现bean管理
  2. context:annotation-config标签向spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor,可以使用@ Resource 、@ PostConstruct、@ PreDestroy等注解
  3. 使用compoment-scan后可以将context:annotation-config移除
  4. 2.X版本没有脱离配置文件
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    <context:annotation-config/>--><context:component-scan base-package="com.shaoby.*" />
</beans>
@Component
public class OtherServiceImpl {
}@Component
public class UserServiceImpl {@Autowiredprivate OtherServiceImpl otherService;
}
public class AppStart {public static void main(String[] args) {ClassPathXmlApplicationContext cp = new ClassPathXmlApplicationContext("applicationContext.xml");for (String beanDefinitionName : cp.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}
}

spring3.X

spring3.0

  1. 新增@Configuration标记类为配置类,代替applicationContext.xml文件;@Bean注解注入对象,相当于xml中的bean标签;
  2. 无法脱离配置文件,需要借助context即xml中的compoment-scan标签,否则无法识别@Component、@Controller、@Service、@Repository注解;
  3. 提供@ImportResource注解,关联applicationContext配置文件;
  4. 所以就升级成@Configuration注解+applicationContext.xml配合Bean注解使用或者@Configuration注解+applicationContext.xml配合@Component等四个注解使用
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.shaoby.*" />
</beans>
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class AppConfig {@Beanpublic OtherServiceImpl otherService(){return new OtherServiceImpl();}
}
@Service
public class UserServiceImpl {
}public class OtherServiceImpl {
}
public class AppStart {public static void main(String[] args) {ApplicationContext ap = new AnnotationConfigApplicationContext(AppConfig.class);for (String beanDefinitionName : ap.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}
}

spring3.1

1. 新增@ComportScan注解,默认扫描当前包及其子包下是@Component等四个注解修饰的所有类,脱离配置文件;
@Configuration
@ComponentScan("com.shaoby.*")
public class AppConfig {
}
@Component
public class OtherServiceImpl {
}
@Service
public class UserServiceImpl {
}
2. 新增@Import注解
  1. 代替import标签,在配置类中导入其他配置类
@Configuration
public class OtherConfig {@Beanpublic OtherServiceImpl otherService(){return new OtherServiceImpl();}
}@Configuration
@ComponentScan("com.shaoby.*")
@Import({OtherConfig.class})
public class AppConfig {
}
  1. 可以在配置类中直接导入bean
@Configuration
@Import(StudentServiceImpl.class)
public class OtherConfig {@Beanpublic OtherServiceImpl otherService(){return new OtherServiceImpl();}
}public class StudentServiceImpl {
}
  1. 高级用法-动态注入

    @Import注解如果引入了实现ImportSelector注解的类,不会将该类型注入到容器中,而是将selectImports方法返回类型的全类路径字符串的数据注入到容器中

public class MyImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{Logger.class.getName(),Redis.class.getName()};}
}@Configuration
@Import(MyImportSelector.class)
public class JavaConfig {public static void main(String[] args) {ApplicationContext ap = new AnnotationConfigApplicationContext(JavaConfig.class);for (String beanDefinitionName : ap.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
javaConfig
com.shaoby.importtest.Logger
com.shaoby.importtest.Redis

@Import注解如果引入了实现ImportBeanDefinitionRegistrar的类也可以

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {registry.registerBeanDefinition("logger", new RootBeanDefinition(Logger.class));registry.registerBeanDefinition("redis", new RootBeanDefinition(Redis.class));}
}@Configuration
@Import(MyImportBeanDefinitionRegistrar.class)
public class JavaConfig {public static void main(String[] args) {ApplicationContext ap = new AnnotationConfigApplicationContext(JavaConfig.class);for (String beanDefinitionName : ap.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
javaConfig
logger
redis
3. 新增Enabled模块,即EnabledXXX注解,它是结合@Import注解使用的
@Configuration
public class RedisAutoConfiguration {@Beanpublic RedisTemplate redisTemplate(){return new RedisTemplate();}
}public class RedisTemplate {
}

定义开关注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(RedisAutoConfiguration.class)
public @interface EnabledAutoRedisConfiguration {
}
@Configuration
@EnabledAutoRedisConfiguration
public class AppConfig {
}
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
appConfig
com.shaoby.ebabledtest.RedisAutoConfiguration
redisTemplate

注掉@EnabledAutoRedisConfiguration

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
appConfig

spring4.X

新增注解:@Conditional(条件注解),@EventListListener(事件监听),@AliasFor(别名),@CrossOrigin(解决跨域问题)

@Conditional注解

这个注解控制在什么条件下注入bean,这个注解传入一个实现Condition接口的类

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {/*** All {@link Condition} classes that must {@linkplain Condition#matches match}* in order for the component to be registered.*/Class<? extends Condition>[] value();}

在Condition的实现类中可以根据需求判断是否需要注入容器中

@FunctionalInterface
public interface Condition {/*** Determine if the condition matches.* @param context the condition context* @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata class}* or {@link org.springframework.core.type.MethodMetadata method} being checked* @return {@code true} if the condition matches and the component can be registered,* or {@code false} to veto the annotated component's registration*/boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);}

实战

public class JavaConfig {/** 根据MyConditionalOnClass中matches方法返回值判断是否将UsersService注入到容器中*/@Conditional(MyConditionalOnClass.class)@Beanpublic UserService userService(){return new UserService();}public static void main(String[] args) {ApplicationContext ap = new AnnotationConfigApplicationContext(JavaConfig.class);for (String beanDefinitionName : ap.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}
}public class MyConditionalOnClass implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {/** 根据需求判断是否需要将bean注入到容器中*/boolean userServiceFlag = context.getRegistry().containsBeanDefinition("userService");return userServiceFlag;}
}public class UserService {
}

spring5.X

新增@Indexed(类索引),需要引入依赖。它被标识在@Component注解上,会在编译时会在MATE-INF文件夹下生成spring.components文件夹存储要DI的所有类的全类路径。这样会提高代码启动速度。

<dependency><groupId>org.springframework</groupId><artifactId>spring-context-indexer</artifactId>
</dependency>

总结

在spring的发展中注解的开发已经为SpringBoot的诞生做好了铺垫,其中@EnabledAutoXXXConfiguration、@ConditionalOnXXX、@Import等注解是SpringBoot自动装配原理的核心注解。

相关文章:

SpringBoot前置知识02-spring注解发展史

springboot前置知识01-spring注解发展史 spring1.x spring配置只能通过xml配置文件的方式注入bean,需要根据业务分配配置文件&#xff0c;通过import标签关联。 spring1.2版本出现Transactional注解 <?xml version"1.0" encoding"UTF-8"?> <be…...

C++ TCP发送Socket数据

DEVC需要加入ws2_32库 #include <iostream> #include <winsock2.h>#pragma comment(lib, "ws2_32.lib")void sendData(const char* ip, int port, const char* data) {WSADATA wsaData;SOCKET sockfd;struct sockaddr_in server_addr;// 初始化Winsock…...

鸿蒙HarmonyOS开发中的易混点归纳-持续补充中

相关文章目录 鸿蒙HarmonyOS开发术语全解&#xff1a;小白也能看懂&#xff01; 文章目录 相关文章目录前言一、build()函数和Builder装饰器&#xff1f;二、自定义组件和系统组件&#xff08;内置组件&#xff09;三、组件和页面四、自定义弹窗和其他弹窗总结 前言 一、build…...

ue引擎游戏开发笔记(45)——添加游戏音效

1.需求分析&#xff1a; 截至目前&#xff0c;我们仍然在一个无声的世界游玩游戏&#xff0c;所以有必要为游戏增添一些声音&#xff0c;例如开火声&#xff0c;子弹撞击声等等。 2.操作实现&#xff1a; 1.这是一个较为简单的功能&#xff0c;类似特效的实现方法&#xff0c…...

202472读书笔记|《首先你要快乐,其次都是其次》——快乐至上,允许一切发生

202472读书笔记|《首先你要快乐&#xff0c;其次都是其次》——快乐至上&#xff0c;允许一切发生 《首先你要快乐&#xff0c;其次都是其次》作者林小仙&#xff0c;挺轻松的小漫画&#xff0c;清新的文字。 生而为人&#xff0c;我很抱歉&#xff0c;大可不必。 生活已经很难…...

8.STL中Vector容器的常见操作(附习题)

目录 1.vector的介绍 2 vector的使用 2.1 vector的定义 2.2 vector iterator 的使用 2.3 vector 空间增长问题 2.3 vector 增删查改 2.4 vector 迭代器失效问题 2.5 vector 在OJ中的使用 1.vector的介绍 vector是表示可变大小数组的序列容器。 就像数组一样&#xff0…...

5.23小结

1.java项目创新 目前想添加一个自动回复的功能和设置验证方式有&#xff08;允许任何人添加&#xff0c;禁止添加&#xff0c;设置回答问题添加&#xff0c;普通验证添加&#xff09; 目前只完成画好前端界面&#xff0c;前端发送请求&#xff0c;还有表的修改 因为涉及表字…...

文心一言 VS 讯飞星火 VS chatgpt (265)-- 算法导论20.1 4题

四、假设不使用一棵叠加的度为 u \sqrt{u} u ​ 的树&#xff0c;而是使用一棵叠加的度为 u 1 k u^{\frac{1}{k}} uk1​的树&#xff0c;这里 k 是大于 1 的常数&#xff0c;则这样的一棵树的高度是多少&#xff1f;又每个操作将需要多长时间&#xff1f;如果要写代码&#xf…...

Flutter 中的 EditableText 小部件:全面指南

Flutter 中的 EditableText 小部件&#xff1a;全面指南 在Flutter中&#xff0c;EditableText是一个低级别的文本编辑组件&#xff0c;它提供了构建自定义文本编辑界面的能力。与TextField和TextFormField不同&#xff0c;EditableText提供了更多的灵活性&#xff0c;允许开发…...

H800基础能力测试

H800基础能力测试 参考链接A100、A800、H100、H800差异H100详细规格H100 TensorCore FP16 理论算力计算公式锁频安装依赖pytorch FP16算力测试cublas FP16算力测试运行cuda-samples 本文记录了H800基础测试步骤及测试结果 参考链接 NVIDIA H100 Tensor Core GPU Architecture…...

2024/5/24 Day38 greedy 435. 无重叠区间 763.划分字母区间 56. 合并区间

2024/5/24 Day38 greedy 435. 无重叠区间 763.划分字母区间 56. 合并区间 遇到两个维度权衡的时候&#xff0c;一定要先确定一个维度&#xff0c;再确定另一个维度。如果两个维度一起考虑一定会顾此失彼。 重叠区间问题 435. 无重叠区间 题目链接 435 给定一个区间的集合 i…...

【python】使用函数名而不加括号是什么情况?

使用函数名而不加括号通常是为了表示对函数本身的引用&#xff0c;而不是调用函数。这种用法通常出现在下面这几种情况&#xff1a; 作为回调函数传递&#xff1a;将函数名作为参数传递给其他函数&#xff0c;以便在需要时调用该函数。例如&#xff0c;在事件处理程序或高阶函数…...

全文检索ElasticSearch简介

1 全文检索 1.1 什么是全文检索 全文检索是一种通过对文本内容进行全面索引和搜索的技术。它可以快速地在大量文本数据中查找包含特定关键词或短语的文档,并返回相关的搜索结果。全文检索广泛应用于各种信息管理系统和应用中,如搜索引擎、文档管理系统、电子邮件客户端、新闻…...

Github上传时报错The file path is empty的解决办法

问题截图 文件夹明明不是空的&#xff0c;却怎么都上传不上去。 解决方案&#xff1a; 打开隐藏文件的开关&#xff0c;删除原作者的.git文件 如图所示&#xff1a; 上传成功&#xff01;...

Adobe Bridge BR v14.0.3 安装教程 (多媒体文件组织管理工具)

Adobe系列软件安装目录 一、Adobe Photoshop PS 25.6.0 安装教程 (最流行的图像设计软件) 二、Adobe Media Encoder ME v24.3.0 安装教程 (视频和音频编码渲染工具) 三、Adobe Premiere Pro v24.3.0 安装教程 (领先的视频编辑软件) 四、Adobe After Effects AE v24.3.0 安装…...

嵌入式学习——3——TCP-UDP 数据交互,握手,挥手

1、更新源 cd /etc/apt/ sudo cp sources.list sources.list.save 将原镜像备份 sudo vim sources.list 将原镜像修改成阿里源/清华源&#xff0c;如所述 阿里源 deb http://mirrors.aliyun.com/ubuntu/ bionic main …...

【LeetCode】【3】无重复字符的最长子串(1113字)

文章目录 [toc]题目描述样例输入输出与解释样例1样例2样例3 提示Python实现滑动窗口 个人主页&#xff1a;丷从心 系列专栏&#xff1a;LeetCode 刷题指南&#xff1a;LeetCode刷题指南 题目描述 给定一个字符串s&#xff0c;请你找出其中不含有重复字符的最长子串的长度 样…...

溪谷联运SDK功能全面解析

近期&#xff0c;备受用户关注的手游联运10.0.0版本上线了&#xff0c;不少用户也选择了版本更新&#xff0c;其中也再次迎来了SDK的更新。溪谷软件和大家一起盘点一下溪谷SDK的功能都有哪些吧。 一、溪谷SDK具有完整的运营功能和高度扩展性 1.登录&#xff1a;登录是SDK最基础…...

Vitis HLS 学习笔记--控制驱动TLP - Dataflow视图

目录 1. 简介 2. 功能特性 2.1 Dataflow Viewer 的功能 2.2 Dataflow 和 Pipeline 的区别 3. 具体演示 4. 总结 1. 简介 Dataflow视图&#xff0c;即数据流查看器。 DATAFLOW优化属于一种动态优化过程&#xff0c;其完整性依赖于与RTL协同仿真的完成。因此&#xff0c;…...

蓝桥杯物联网竞赛_STM32L071KBU6_关于sizo of函数产生的BUG

首先现象是我在用LORA发送信息的时候&#xff0c;左边显示长度是8而右边接收到的数据长度却是4 我以为是OLED显示屏坏了&#xff0c;又或者是我想搞创新用了const char* 类型强制转换数据的原因&#xff0c;结果发现都不是 void Function_SendMsg( unsigned char* data){unsi…...

springboot 百货中心供应链管理系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;百货中心供应链管理系统被用户普遍使用&#xff0c;为方…...

简易版抽奖活动的设计技术方案

1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...

Redis相关知识总结(缓存雪崩,缓存穿透,缓存击穿,Redis实现分布式锁,如何保持数据库和缓存一致)

文章目录 1.什么是Redis&#xff1f;2.为什么要使用redis作为mysql的缓存&#xff1f;3.什么是缓存雪崩、缓存穿透、缓存击穿&#xff1f;3.1缓存雪崩3.1.1 大量缓存同时过期3.1.2 Redis宕机 3.2 缓存击穿3.3 缓存穿透3.4 总结 4. 数据库和缓存如何保持一致性5. Redis实现分布式…...

视频字幕质量评估的大规模细粒度基准

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 摘要 视频字幕在文本到视频生成任务中起着至关重要的作用&#xff0c;因为它们的质量直接影响所生成视频的语义连贯性和视觉保真度。尽管大型视觉-语言模型&#xff08;VLMs&#xff09;在字幕生成方面…...

【Go语言基础【13】】函数、闭包、方法

文章目录 零、概述一、函数基础1、函数基础概念2、参数传递机制3、返回值特性3.1. 多返回值3.2. 命名返回值3.3. 错误处理 二、函数类型与高阶函数1. 函数类型定义2. 高阶函数&#xff08;函数作为参数、返回值&#xff09; 三、匿名函数与闭包1. 匿名函数&#xff08;Lambda函…...

莫兰迪高级灰总结计划简约商务通用PPT模版

莫兰迪高级灰总结计划简约商务通用PPT模版&#xff0c;莫兰迪调色板清新简约工作汇报PPT模版&#xff0c;莫兰迪时尚风极简设计PPT模版&#xff0c;大学生毕业论文答辩PPT模版&#xff0c;莫兰迪配色总结计划简约商务通用PPT模版&#xff0c;莫兰迪商务汇报PPT模版&#xff0c;…...

【SpringBoot自动化部署】

SpringBoot自动化部署方法 使用Jenkins进行持续集成与部署 Jenkins是最常用的自动化部署工具之一&#xff0c;能够实现代码拉取、构建、测试和部署的全流程自动化。 配置Jenkins任务时&#xff0c;需要添加Git仓库地址和凭证&#xff0c;设置构建触发器&#xff08;如GitHub…...

水泥厂自动化升级利器:Devicenet转Modbus rtu协议转换网关

在水泥厂的生产流程中&#xff0c;工业自动化网关起着至关重要的作用&#xff0c;尤其是JH-DVN-RTU疆鸿智能Devicenet转Modbus rtu协议转换网关&#xff0c;为水泥厂实现高效生产与精准控制提供了有力支持。 水泥厂设备众多&#xff0c;其中不少设备采用Devicenet协议。Devicen…...

Vue3 PC端 UI组件库我更推荐Naive UI

一、Vue3生态现状与UI库选择的重要性 随着Vue3的稳定发布和Composition API的广泛采用&#xff0c;前端开发者面临着UI组件库的重新选择。一个好的UI库不仅能提升开发效率&#xff0c;还能确保项目的长期可维护性。本文将对比三大主流Vue3 UI库&#xff08;Naive UI、Element …...

PH热榜 | 2025-06-08

1. Thiings 标语&#xff1a;一套超过1900个免费AI生成的3D图标集合 介绍&#xff1a;Thiings是一个不断扩展的免费AI生成3D图标库&#xff0c;目前已有超过1900个图标。你可以按照主题浏览&#xff0c;生成自己的图标&#xff0c;或者下载整个图标集。所有图标都可以在个人或…...