Spring5基础
基于Spring 5.2.6 版本。
Spring概念

IOC容器

IOC底层原理的演进过程——本质就是为了高内聚,低耦合
在原始方式中,我们通过new创建对象来实现创建对象的逻辑,但是这样做当对象路径等发生变化时,就要修改代码,耦合度太高。
因此,我们引入了工厂模式来降低耦合,这样当我们的类发生变化要进行修改时,可以不改动调用类,而是去修改工厂类,这样就降低了耦合度。但是这样子耦合度就来到了工厂类身上。

Spring在工厂类中以配置文件加反射的方式来进一步降低耦合度,当类发生变化时,我们只需要修改配置文件而不需要去修改代码,进一步降低了耦合度,这就是IOC的本质就是一个对象工厂。
即:IOC容器 = 工厂模式 + xml解析(注解)+ 反射

Spring IOC容器相关实现类

IOC进行Bean管理
概念


基于xml方式进行bean管理
set方法注入属性


有参构造函数注入属性


p名称空间注入——为了简化set方法注入的写法,不建议使用

xml注入其他类型属性
注入null值和xml的特殊符号
字面量:指设置属性的固定值
]]> ,在mybatis的xml中的sql语句常用。

注入外部bean属性——工作中最常用方式

注入内部bean属性
如果一个bean要复用,写在外部,如果偶尔使用甚至是一次性的,用注入内部bean属性方式比较好。


级联赋值注入bean属性——建议还是用注入外部bean属性的方式




xml注入集合属性
在集合里面设置普通类型值



在集合里面设置对象类型值

提取集合作为公共部分用于注入复用


FactoryBean——工厂bean


bean的作用域



还有其他作用域(一般不使用):
request:表示bean对象在一次请求中生效。
session:表示bean对象在一次会话中生效。
bean的生命周期——重要
生命周期时指从对象创建到对象销毁的过程。



代码


xml自动装配
自动装配要求我们只要把bean实例创建出来,在bean配置上使用autowire属性值指定自动装配规则,Spring就会自动帮我们注入属性值。
手动装配就是我们自己在xml中配置property属性值。


xml读取外部属性文件——读取properties等配文件的配置
以配置德鲁伊数据库连接池为例。




基于注解方式进行bean管理




属性注入方式注解有3种
分别是@Autowired、@Qualifier、@Resource。





AOP
AOP基于动态代理实现,常见使用场景如:
- 全局出入参日志的打印
- 事务
- 全局异常的捕获处理
- 鉴权
AOP基本原理



JDK动态代理


代码示例




AOP术语


AspectJ基本概念和准备

切入点表达式(重点)


基于注解实现AspectJ——重点






抽取共同切入点写法



基于XML配置文件实现AspectJ

JDBCTemplate——相当于JDBC的封装版——了解



增

删

改

查
查询某个值——count


查询对象

查询对象列表

批量操作



事务管理

事务介绍


注解声明式事务管理


声明式事务参数配置






XML声明式事务管理



完全注解声明式事务管理——即使用配置类替换XML配置


Spring5新特性
整合slf4j2日志框架

1、引入依赖
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
2、创建配置文件——log4j2.xml
约定:配置名是固定的,放在项目resources根目录下,否则不会生效,只会走默认没有配置下的日志打印配置。
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出-->
<configuration status="DEBUG">
<!--先定义所有的appender-->
<appenders>
<!--输出日志信息到控制台-->
<console name="Console" target="SYSTEM_OUT">
<!--控制日志输出的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</console>
</appenders>
<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
<!--root:用于指定项目的根日志,如果没有单独指定Logger,则会使用root作为默认的日志输出-->
<loggers>
<root level="DEBUG">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>

@Nullable注解

Lambda表达式创建Spring容器对象

Spring5支持整合JUnit5——单元测试必备——重点



重点使用@SpringJUnitConfig复合注解来进行单元测试

SpringWebflux
核心是:基于reactor的响应式编程和基于java8的函数式编程。
gateway基于webflux来实现,普通的服务基于springmvc实现,像网关这种需要处理更多的请求的场景用webflux来异步非阻塞处理请求更加方便,可以在有限的资源中处理更多的请求。
响应式编程类似于电子表格中的计算,就是观察者模式。
同步与异步,阻塞与非阻塞
拿等公交车举例:你在公交车站等公交车,你是调用者,你在等公交车时就是等着,不干其他的,就是同步,你在等车时顺便听听歌,看看新闻,就叫异步;而公交车在收到你等待它的消息时,公交车是被调用者,如果他马上回应你,等车到站了才告诉你我车到了,叫做阻塞,而如果公交车马上回应你,说我还有3分钟到站,就是非阻塞。













基于注解编程模型








基于函数式编程模型






Spring5重点掌握点总结



【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2021-02-25 注解和反射学习笔记