当前位置: 首页 > 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…...

聊聊 Pulsar:Producer 源码解析

一、前言 Apache Pulsar 是一个企业级的开源分布式消息传递平台&#xff0c;以其高性能、可扩展性和存储计算分离架构在消息队列和流处理领域独树一帜。在 Pulsar 的核心架构中&#xff0c;Producer&#xff08;生产者&#xff09; 是连接客户端应用与消息队列的第一步。生产者…...

SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现

摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序&#xff0c;以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务&#xff0c;提供稳定高效的数据处理与业务逻辑支持&#xff1b;利用 uniapp 实现跨平台前…...

【git】把本地更改提交远程新分支feature_g

创建并切换新分支 git checkout -b feature_g 添加并提交更改 git add . git commit -m “实现图片上传功能” 推送到远程 git push -u origin feature_g...

OPENCV形态学基础之二腐蚀

一.腐蚀的原理 (图1) 数学表达式&#xff1a;dst(x,y) erode(src(x,y)) min(x,y)src(xx,yy) 腐蚀也是图像形态学的基本功能之一&#xff0c;腐蚀跟膨胀属于反向操作&#xff0c;膨胀是把图像图像变大&#xff0c;而腐蚀就是把图像变小。腐蚀后的图像变小变暗淡。 腐蚀…...

如何在网页里填写 PDF 表格?

有时候&#xff0c;你可能希望用户能在你的网站上填写 PDF 表单。然而&#xff0c;这件事并不简单&#xff0c;因为 PDF 并不是一种原生的网页格式。虽然浏览器可以显示 PDF 文件&#xff0c;但原生并不支持编辑或填写它们。更糟的是&#xff0c;如果你想收集表单数据&#xff…...

(一)单例模式

一、前言 单例模式属于六大创建型模式,即在软件设计过程中,主要关注创建对象的结果,并不关心创建对象的过程及细节。创建型设计模式将类对象的实例化过程进行抽象化接口设计,从而隐藏了类对象的实例是如何被创建的,封装了软件系统使用的具体对象类型。 六大创建型模式包括…...

AI语音助手的Python实现

引言 语音助手(如小爱同学、Siri)通过语音识别、自然语言处理(NLP)和语音合成技术,为用户提供直观、高效的交互体验。随着人工智能的普及,Python开发者可以利用开源库和AI模型,快速构建自定义语音助手。本文由浅入深,详细介绍如何使用Python开发AI语音助手,涵盖基础功…...

Spring Security 认证流程——补充

一、认证流程概述 Spring Security 的认证流程基于 过滤器链&#xff08;Filter Chain&#xff09;&#xff0c;核心组件包括 UsernamePasswordAuthenticationFilter、AuthenticationManager、UserDetailsService 等。整个流程可分为以下步骤&#xff1a; 用户提交登录请求拦…...

6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础

第三周 Day 3 &#x1f3af; 今日目标 理解类&#xff08;class&#xff09;和对象&#xff08;object&#xff09;的关系学会定义类的属性、方法和构造函数&#xff08;init&#xff09;掌握对象的创建与使用初识封装、继承和多态的基本概念&#xff08;预告&#xff09; &a…...

沙箱虚拟化技术虚拟机容器之间的关系详解

问题 沙箱、虚拟化、容器三者分开一一介绍的话我知道他们各自都是什么东西&#xff0c;但是如果把三者放在一起&#xff0c;它们之间到底什么关系&#xff1f;又有什么联系呢&#xff1f;我不是很明白&#xff01;&#xff01;&#xff01; 就比如说&#xff1a; 沙箱&#…...