Spring Framework
1、Spring
1.1、简介
- Spring春天……>给软件行业带来了春天
- 2002,首次推出了Spring框架的雏形,interface21框架
- 2004年3月24日发布1.0版本
- Rod Johnson,Spring Framework创始人,悉尼大学音乐学博士
- Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
- Spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架
- SSH:Struct2+Spring+Hibernate
- SSM:SpringMvc+Spring+Mybatis
Spring的基本组成:
- 最完善的轻量级核心框架。
- 通用的事务管理抽象层。
- JDBC抽象层。
- 集成了Toplink, Hibernate, JDO, and iBATIS SQL Maps。
- AOP功能。
- 灵活的MVC Web应用框架。
框架文档:https://docs.spring.io/spring-framework/docs/current/reference/html/index.html
GItHub:https://github.com/spring-projects/spring-framework
Mvaen依赖:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.21</version>
</dependency>
<!-- 顺便多添加一个JDBC的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.21</version>
</dependency>
1.2、优点
- Spring是一个开源的免费框架(容器)
- Spring是一个轻量级、非入侵式的框架
- 控制反转(IOC),面向切面编程(AOP)
- 支持事物的处理,对框架整合的支持
总结一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!
1.3、组成
1.4、扩展
在Spring的官网有这个介绍:现代化的java开发!说白了就是基于Spring的开发
- Spring Boot
- 一个快速开发的脚手架
- 基于SpringBoot可以快速的开发单个微服务
- 约定大于配置
- Spring Cloud
- SpringCloud是基于SpringBoot实现的
因为现在大多数公司都在使用SpringBoot进行快速开发,学习Spring的前提,需要完全掌握Spring及SpringMVC!承上启下的作用!
弊端:发展太久之后,违背了原来的理念!配置十分繁琐,人称“配置地狱”
2、IOC理论推导
- UserDao接口
- UserDaoImpl实现类
- UserService业务接口
- UserServieImpl业务实现类
在我们之前大的业务中,用户的需求可能会影响到我们原来的代码,我们需要根据用户的需求去修改源代码!如果程序代码了十分大,修改一次的成本代价十分昂贵!
我们使用一个Set接口实现,已经发生了革命性的变化
private UserDao userDao;
//利用set进行动态实现值的注入!
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
- 之前,程序是主动创建对象!控制权在程序员手上
- 使用了set注入后,程序不再具有主动性,而是变成了被动接受的对象
- 现在控制权在用户手上
IoC 是 Inversion of Control 的简写,译为“控制反转”,它不是一门技术,而是一种设计思想,是一个重要的面向对象编程法则,能够指导我们如何设计出松耦合、更优良的程序。
这种思想,从本质上解决了问题,我们程序员不用再去管理对象的管理了。系统的耦合性大大降低,可以更加专注的在业务的实现上
IOC,控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IOC容器,其实现方法是依赖注入(Dependency Injection,DI)
3、HelloSpring!
创建实体类
package com.pojo;
public class Hello {
private String srt;
public String getSrt() {
return srt;
}
public void setSrt(String srt) {
this.srt = srt;
}
@Override
public String toString() {
return "Hello{" +
"srt='" + srt + '\'' +
'}';
}
}
springBean配置文件
<?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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用Spring在创建对象,在spring这些都成称为Bean
类型 变量名 = new 类型();
Hello hello = new Hello();
id = 变量名;
class = new 的对象
property 相当于给对象中的属性设置一个值!
-->
<!-- ref:引用Spring容器中的创建好的对象
value:具体的值-->
<bean id="hello" class="com.pojo.Hello">
<property name="srt" value="Spring"></property>
</bean>
</beans>
测试实现
public class MyTest {
public static void main(String[] args) {
//获取spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象现在都在spring中管理了,我们要使用,直接去里面取出来
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
4、IOC创建对象的方式
-
使用无参构造创建对象,默认实现
public class MyTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); //这一步就带表创建了无参构造,xml里面的bean都会被注册 User userClass = (User) applicationContext.getBean("userClass"); userClass.show(); } } ================================================================= ----> User的无参构造 ----> name=张三
-
使用有参构造创建对象
-
下标赋值
<bean id="userClass" class="com.pojo.User"> <constructor-arg index="0" value="张三"></constructor-arg> </bean>
-
类型赋值,不建议使用
<bean id="userClass" class="com.pojo.User"> <constructor-arg type="java.lang.String" value="张三"></constructor-arg> </bean>
-
参数名
<bean id="userClass" class="com.pojo.User"> <constructor-arg name="name" value="张三"></constructor-arg> </bean>
-
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了
5、Spring配置
- 别名
<alias name="userClass" alias="user"></alias>
- Bean的配置
id:bean的唯一标识符
class:bean对象所对应的类型:包名+类型
name:也是别名,而且可以同时取多个别名
scope:作用域
- import
这个import,一般用户团队开发使用,它可以将多个配置文件,导入合并为一个
beans.xml
<bean id="userClass" class="com.pojo.User" >
<constructor-arg name="name" value="张三"></constructor-arg>
</bean>
applicationContext.xml
<import resource="beans.xml"></import>
测试
public static void main(String[] args) {
//获取spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//我们的对象现在都在spring中管理了,我们要使用,直接去里面取出来
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
6、依赖注入
6.1、构造器注入
上文以提到
6.2、Set方式注入【重点】
- 依赖注入set
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
-
复杂类型
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
-
真实测试对象
public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; …… }
-
applicationContext.xml
<bean id="address" class="com.pojo.Address"> <property name="address" value="北京"></property> </bean> <bean id="student" class="com.pojo.Student"> <!-- 第一种,普通值注入,value--> <property name="name" value="张三"></property> <!-- 第二种,Bean注入,ref--> <property name="address" ref="address"></property> <!-- 第三种,数组注入,value --> <property name="books"> <array> <value>西游记</value> <value>红楼梦</value> <value>三国演义</value> </array> </property> <!-- 第四种,List注入,value --> <property name="hobbies"> <list> <value>学习</value> <value>看书</value> <value>听音乐</value> </list> </property> <!-- 第五种,Map注入,entry --> <property name="card"> <map> <entry key="手机号" value="13477665544"></entry> <entry key="邮编" value="310000"></entry> </map> </property> <!--Set注入,value--> <property name="games"> <set> <value>CoC</value> <value>BoB</value> <value>LoL</value> </set> </property> <!--Null值注入--> <property name="wife"> <null/> </property> <!--Properties--> <property name="info"> <props> <!-- 值写在外面--> <prop key="birthday">2000-01-01</prop> </props> </property> </bean>
-
测试
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) applicationContext.getBean("student"); System.out.println(student.toString()); /* * Student{ name='张三', address=Address{address='北京'}, books=[西游记, 红楼梦, 三国演义], hobbies=[学习, 看书, 听音乐], card={ 手机号=13477665544, 邮编=310000 }, games=[CoC, BoB, LoL], wife='null', info={ birthday=2000-01-01 } * } * */ }
6.3、拓展方式注入
-
P命名空间 property,属性注入
在前面加上约束 xmlns:p="http://www.springframework.org/schema/p" …… <bean id ="user" class="com.pojo.User" p:name="张三" p:age="18"></bean>
-
C命名空间constructor,构造器注入
在前面加上约束 xmlns:c="http://www.springframework.org/schema/c" <bean id ="user02" class="com.pojo.User" c:name="张三" c:age="18"></bean>
测试
实体类:
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
get set toString……
}
applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id ="user01" class="com.pojo.User" p:name="张三" p:age="18"></bean>
<bean id ="user02" class="com.pojo.User" c:name="张三" c:age="18"></bean>
</beans>
测试类:
@Test
public void TestSchema(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("UserBean.xml");
User user01 = applicationContext.getBean("user01", User.class);
System.out.println(user01.toString());
User user02 = applicationContext.getBean("user02", User.class);
System.out.println(user02.toString());
}
}
控制台输出:
User{name='张三', age=18}
User{name='张三', age=18}
6.4、Bean作用域
Scope | Description |
---|---|
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
prototype | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext . |
session | Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext . |
application | Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext . |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket . Only valid in the context of a web-aware Spring ApplicationContext . |
-
单例模式(Spring默认机制)创建的对象是一样的
<bean id ="user01" class="com.pojo.User" p:name="张三" p:age="18" scope="singleton"></bean>
-
原型模式,每次从容器中get的时候,都会产生一个新对象
<bean id ="user01" class="com.pojo.User" p:name="张三" p:age="18" scope="prototype"></bean>
-
其余的request,session,application,这些只能在web开发中使用
7、Bean的自动装配
- 自动装配是Sprinp满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种自动装配的方式
- 在xml中显式配置
- 在java中显式配置
- 隐式的自动装配【重要】
7.1、测试
使用之前:
<bean id="cat" class="com.pojo.Cat"></bean>
<bean id="dog" class="com.pojo.Dog"></bean>
<bean id="people" class="com.pojo.Person">
<property name="name" value="张三"></property>
<property name="dog" ref="dog"></property>
<property name="cat" ref="cat"></property>
</bean>
测试:
@Test
public void test01(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person people = applicationContext.getBean("people", Person.class);
people.getDog().bark();
people.getCat().bark();
}
========================================
wang~
miao~
7.2、byName
<bean id="cat" class="com.pojo.Cat"></bean>
<bean id="dog" class="com.pojo.Dog"></bean>
<!--
byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的beanId
-->
<bean id="people" class="com.pojo.Person" autowire="byName">
<property name="name" value="张三"></property>
</bean>
7.3、byType
<bean id="cat" class="com.pojo.Cat"></bean>
<bean id="dog" class="com.pojo.Dog"></bean>
<!--
byType:会自动在容器上下文查找,和自己对象属性类型相同的beanId,注意全局唯一
-->
<bean id="people" class="com.pojo.Person" autowire="byType">
<property name="name" value="张三"></property>
</bean>
小结:
- byName的时候,需要保证所有的Bean的id的唯一,并且这个bean需要和自动注入的属性的set方法值一致
- byType的时候,需要所有的Bean的class的唯一,并且这个bean需要大的自动注入的属性的类型一致
7.4、使用注解实现自动装配
JDK1.5支持的注解,Spring就支持注解
要使用注解须知:
-
导入约束context约束
-
配置注解的支持context:annotation-config/
<?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/beans https://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/> </beans>
<!-- 指定要扫描的包,这个包下面的注解就会被指定生效--> <context:component-scan base-package="com.pojo"></context:component-scan>
@Autowired
直接在属性上使用即可!也可以在set方式上使用
使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字byType
public class Person {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
……
}
科普:
@Nullable 可以让有参构造 参数设置为空
如果使用@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value=“XXX”)去配合Autowired的使用,指定一个唯一的bean对象注入!
@Resource
@Required
注释适用于bean属性设置器方法,如以下示例所示:
JavaKotlin
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Required
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
小结:
@Resource和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byTpye的方式实现,当匹配到多个同类型的时候使用byName实现
- @Resource
8、使用注解开发
在Spring4之后,要使用注解开发,必须要保证AOP的包导入成功org.springframework:spring-aop:x.x.x
使用注解需要导入Context约束,增加注解的支持
<?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/beans
https://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/>
</beans>
注解说明:
-@Autowired
:自动装配通过类型。名字
如果Autowired不能唯一自动装配,则需要通过@Qualifier(value=“xxx”)
-@Nullable
:字段标记这个注解,说明这个字段可以为null
-@Resource
:自动装配名字,类型
-@Component
:组件,放在类上面,说明这个类被Spring管理了,就是bean
-
bean
-
属性如何注入
//等价于<bean id="user" class="com.dao.User"/> //@Component @Component public class User { // 相当于<property name="name" value="张三"/> @Value("张三") public String name ; }
-
衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层
- dao【@Repository】
- service【@Service】
- control【@Controller】
- 这四个注解功能都是一样的,都是将代表某个注册类注册到spring中,装配Bean
-
自动装配
-`@Autowired`:自动装配通过类型。名字 如果Autowired不能唯一自动装配,则需要通过@Qualifier(value=“xxx”) -`@Nullable`:字段标记这个注解,说明这个字段可以为null -`@Resource`:自动装配名字,类型 -`@Component`:组件,放在类上面,说明这个类被Spring管理了,就是bean
-
作用域
@Scope
:@Scope(“singleton”); -
小结
xml与注解:
- xml更加方便,适用于任何场合!维护简单方便
- 注解不是自己的类使用不了,维护相对复杂
xml与注解最佳实践
-
xml用来管理bean
-
注解用来注入属性
-
我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!-- 指定要扫描的包,这个包下面的注解就会被指定生效--> <context:component-scan base-package="com"></context:component-scan> <context:annotation-config/>
9、使用Java的方式配置Spring
我们现在要完全不使用spring的xml配置,全权交给java来做
JavaConfig是spring的一个子项目,在Spring4之后,它成为了一个核心功能
实体类:
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("张三")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
Configuration:
//这个也是Spring容器托管,注册到容器中,因为它本来就是一个@Component,@Configuration代表这就是一个配置类,就和我们之前看到了applicationContext.xml是一样的
@Configuration
//扫描pojo包
@ComponentScan("com.pojo")
//导入类似的configuration
@Import(Myconfig01.class)
public class Myconfig {
/*
* 注册一个bean,就像相当于我们之前写的一个bean标签
* 这个方法的名字,就相当于bean标签中的id属性
* 这个方法的返回值,就相当于bean标签中的class属性
* */
@Bean
public User getUser(){
//就是返回要注入的bean的对象
return new User();
}
}
测试类:
public class MyTest {
public static void main(String[] args) {
//如果完全是用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置累的class对象加载
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Myconfig.class);
User getUser = applicationContext.getBean("getUser", User.class);
System.out.println(getUser.getName());
}
}
10、代理模式
为什么要学习代理模式?因为这就是SpringAOP的底层!【SpringAOP和SpringMVC面试基本必问】
10.1、静态代理
通过中介租房子案例
用户-中介-房东
角色分析:
- 抽象角色:一般会使用接口或者抽象类来解决
- 正式角色:被代理的角色
- 代理角色:代理真实角色,我们一般会做一些附属操作
- 客户:访问代理对象的人
代码步骤
-
接口
//这是一个租房的接口 public interface Rent { public void rent(); }
-
真实角色
//房东 public class Hots implements Rent{ @Override public void rent() { System.out.println("房东要出租房子"); } }
-
代理角色
/中介 public class proxy implements Rent{ private Hots hots; public proxy() { } public proxy(Hots hots) { this.hots = hots; } @Override public void rent() { hots.rent(); seeHouse(); fare(); } //看房 public void seeHouse(){ System.out.println("中介带你去看房"); } //瘦中介费 public void fare(){ System.out.println("收中介费"); } }
-
客户端访问代理角色
public class Client { public static void main(String[] args) { //房东出租房子 Hots hots=new Hots(); //交给中介 proxy proxy = new proxy(hots); //中介带你去看了房子,签了合同、赚了中介费 //代理角色一般都会有一些附属操作 proxy.rent(); } } ========================================= 输出: 房东要出租房子 中介带你去看房 收中介费
代理模式的好处:
- 可以使真实角色操作更加纯粹!不用去关注一些公共业务
- 公共也就就交给觉得!实现了业务的分工
- 公共业务发生扩展的时候,方便管理
缺点:
- 一个真实角色就会产生一个代理角色;代码量就会翻倍
10.2、动态代理
- 动态代理和静态代理角色一样
- 动态代理的代理类是动态生成的,不是我们直接写好的
- 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
- 基于接口-JDK多的动态代理【我们在这里使用】
- 基于类的:cglib
- java字节码实现:javaslst
需要了解两个类:proxy(代理),InvocationHandler(调用处理程序)
动态代理的好处:
- 可以使真实角色操作更加纯粹!不用去关注一些公共业务
- 公共也就就交给觉得!实现了业务的分工
- 公共业务发生扩展的时候,方便管理
- 一个动态代理的是一个接口,一般就是对应的一类业务
- 一个动态代理可以代理多个类,只要是实现同一个接口就行
-
接口
//这是一个租房的接口 public interface Rent { public void rent(); }
-
真实角色
//房东 public class Hots implements Rent { @Override public void rent() { System.out.println("房东要出租房子"); } }
-
动态代理类
package com.demo03; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; //等会我们会用这个类,自动生成代理类 public class ProxyInvocationHandle implements InvocationHandler { //被代理的接口 private Rent rent; public void setRent(Rent rent){ this.rent=rent; } //生产得到代理类 public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this); } //处理代理实例,并返回结果 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { seeHouse(); //动态里代理的本质,就是使用反射机制实现 Object result = method.invoke(rent, args); fare(); return result; } public void seeHouse(){ System.out.println("带中介看房子"); } public void fare(){ System.out.println("收中介费"); } }
-
实现:
public class Client { public static void main(String[] args) { //真实角色 Hots hots = new Hots(); //代理角色:现在没有 ProxyInvocationHandle proxyInvocationHandle = new ProxyInvocationHandle(); //通过调用程序处理角色来处理我们要调用的接口对象 proxyInvocationHandle.setRent(hots); Rent proxy = (Rent) proxyInvocationHandle.getProxy(); proxy.rent(); } } ======================================================= 带中介看房子 房东要出租房子 收中介费
提取为公共实现
public class ProxyInvocationHandle implements InvocationHandler {
//被代理的接口
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//生产得到代理类
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
//处理代理实例,并返回结果
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
seeHouse();
//动态里代理的本质,就是使用反射机制实现
Object result = method.invoke(target, args);
log(method.getName());
return result;
}
public void seeHouse(){
System.out.println("动态代理附加方法01");
}
public void log(String meg){
System.out.println("动态代理附加方法02--->执行了"+meg+"方法");
}
}
测试:
public class Client {
public static void main(String[] args) {
//真实角色
UserService userService = new UserServiceImpl();
//代理角色
ProxyInvocationHandle proxyInvocationHandle = new ProxyInvocationHandle();
proxyInvocationHandle.setTarget(userService);//设置要代理的对象
//动态生成代理类
UserService proxy = (UserService) proxyInvocationHandle.getProxy();
proxy.add();
}
}
11、AOP
11.1、什么是AOP
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP技术利用一种称为“横切”的技术,剖解开封装对象的内部,将影响多个类的公共行为封装到一个可重用的模块中,并将其命名为Aspect
切面。所谓的切面,简单来说就是与业务无关,却为业务模块所共同调用的逻辑,将其封装起来便于减少系统的重复代码,降低模块的耦合度,有利用未来的可操作性和可维护性。
利用AOP可以对业务逻辑各个部分进行隔离,从而使业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高开发效率。
11.2、AOP咋Spring中的作用
提供声明式事物;允许用户自定义切面
- 横切关注点;跨越应用程序多个模块的方法或功能。即使与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事物等等
- 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法
- 目标(Target):被通知对象
- 代理(Proxy):向目标对象应用通知之后创建的对象
- 切入点(PointCut):切面通知执行的“地点”的定义
- 连接点(JointPoint):与切入点匹配的执行点
11.3、使用Spring实现AOP
【重点】使用AOP织入,需要导入一个依赖包
<!-- AOP织入包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9</version>
</dependency>
applicationContext引入:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
方式一:使用spring的API接口
-
UserService接口实现
public class UserServiceImpl implements UserService{ @Override public void insert() { System.out.println("增加了一个用户"); } @Override public void delete() { System.out.println("删除了一个用户"); } @Override public void update() { System.out.println("修改了一个用户"); } @Override public void select() { System.out.println("查询了一个用户"); }
-
Log与Logafter
public class Log implements MethodBeforeAdvice { //method:要执行的目标对象的方法 //aegs:参数 //target:,目标对象 @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"执行了"); } } ======================================== public class afterLog implements AfterReturningAdvice { //returnValue:返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行了"+method.getName()+"返回的结果为"+returnValue); } }
-
application.xml
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注册Bean--> <bean id="userService" class="com.service.UserServiceImpl"/> <bean id="log" class="com.log.Log"/> <bean id="afterLog" class="com.log.afterLog"/> <!-- 方式一:使用原始的Spring API接口--> <!-- 配置aop:需要导入aop的约束--> <aop:config> <!-- 切入点 :expression(要执行的位置) --> <aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/> <!-- 执行环绕增加!--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config> </beans>
-
测试类
public class MyTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //动态代理代理的是接口,注意点 UserService userService = (UserService) applicationContext.getBean("userService"); userService.insert(); } }
-
执行结果:
com.service.UserServiceImpl的insert执行了 增加了一个用户 执行了insert返回的结果为null
方式二:自定义类来实现
自定义类:
public class DiyPointcut {
public void before(){
System.out.println("=======方法执行前========");
}
public void after(){
System.out.println("=======方法执行后========");
}
}
applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 方式二:自定义类-->
<bean id="diy" class="com.diy.DiyPointcut"/>
<bean id="userService" class="com.service.UserServiceImpl"/>
<aop:config>
<!-- 自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!-- 切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试结果
=======方法执行前========
增加了一个用户
=======方法执行后========
方式三:用注解来实现
注解类:
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("======方法执行前======");
}
@After("execution(* com.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("======方法执行后======");
}
//在环绕增强中,我们可以给出一个参数,代表我们要获取处理切入的点
@Around("execution(* com.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("=====环绕增强开启======");
//执行方法
Object proceed = joinPoint.proceed();
//获得签名
Signature signature = joinPoint.getSignature();
System.out.println(signature);
}
}
applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 方式二:自定义类-->
<bean id="userService" class="com.service.UserServiceImpl"/>
<bean id="Anno" class="com.diy.AnnotationPointCut"/>
<!-- 开启注解支持-->
<!-- JDK(默认 proxy-target-class="false") cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy/>
</beans>
测试结果:
=====环绕增强开启======
======方法执行前======
增加了一个用户
======方法执行后======
void com.service.UserService.insert()
12、整合Mybatis
整合:
-
导入相关jar包
-
junit
-
mybatis
-
mysql数据库
-
spring相关的
-
aop织入器
-
mybatis-spring【新的知识点】
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.21</version> </dependency> <!-- 顺便多添加一个JDBC的包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.21</version> </dependency> <!-- AOP织入包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- JDBC--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <!-- 测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!-- spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.21</version> </dependency> <!-- mybatis-spring--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency>
记得添加滤过
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
-
-
编写配置文件
-
测试
12.1、回忆mybatis
CREATE DATABASE mybatis;
USE mybatis;
create table user(
id INt(20) NOT NULL PRIMARY KEY ,
name varchar(30) default NULL,
pwd VARCHAR(30) default NULL
)ENGINE=INNODB DEFAULT CHARSET =utf8;
INSERT INTO user (id,name,pwd) values
(1,'张三','123456'),
(2,'李四','123456'),
(3,'王五','123456');
-
编写实体类
package com.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
-
编写核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true &characterEncoding=utf8&useSSL=true&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root1@12"/> </dataSource> </environment> </environments> <mappers> <package name="com.mapper"/> </mappers> </configuration>
-
编写接口
public interface UserMapper { public List<User> selectUser(); }
-
编写mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mapper.UserMapper"> <select id="selectUser" resultType="user"> select * from mybatis.user </select> </mapper>
-
测试
import com.mapper.UserMapper; import com.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class MyTest { @Test public void test() throws IOException { String resource = "mybatis-config.xml"; InputStream resourceAsStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.selectUser(); System.out.println(users); } } ========================================= [User(id=1, name=张三, pwd=123456), User(id=2, name=李四, pwd=123456), User(id=3, name=王五, pwd=123456)]
12.2、Spring整合MyBatis
方式一:
-
编写数据源配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.pojo"/> </typeAliases> </configuration>
-
sqlSessionFactroy
-
sqlSeesionTemplate
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DataSource:使用Spring的数据源替换MyBatis的配置 c3p0 dbcp druid 我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true &characterEncoding=utf8&useSSL=true&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root1@12"/> </bean> <!-- sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/mapper/UserMapper.xml"></property> </bean> <!-- SqlsessionTemplate:就是我们使用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能使用构造器注入sqlSessionFactory,因为它没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
-
需要给接口加实现类
public class UserMapperImpl implements UserMapper{ //我们所有的操作,都使用sqlSession来执行,在原来,都在都使用SqlsessionTemplate; private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession){ this.sqlSession = sqlSession; } @Override public List<User> selectUser() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.selectUser(); } }
-
将自己写的实现类,注入到Spring中
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"></property> </bean> </beans>
-
测试
public class MyTest { @Test public void test() throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class); System.out.println(userMapper.selectUser()); } } ============================================= [User(id=1, name=张三, pwd=123456), User(id=2, name=李四, pwd=123456), User(id=3, name=王五, pwd=123456)]
方式二:
- 继承SqlSeesionDaoSupport 实现UserMapper
public class UserMapperImlp2 extends SqlSessionDaoSupport implements UserMapper { @Override public List<User> selectUser() { return getSqlSession().getMapper(UserMapper.class).selectUser(); } }
-
工具配置文件.xml,不用写SqlsessionTemplate
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DataSource:使用Spring的数据源替换MyBatis的配置 c3p0 dbcp druid 我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true &characterEncoding=utf8&useSSL=true&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root1@12"/> </bean> <!-- sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/mapper/UserMapper.xml"></property> </bean> </beans>
-
Spring配置
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <bean id="userMapper2" class="com.mapper.UserMapperImlp2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
13、声明式事务
13.1、回顾事务
- 把一组业务当初一个业务来做,要么都成功,要么都失败
- 事务在项目开发中,十分的重要,涉及数据的一致性问题,不能马虎
- 确保完整性和一致性
事务的ACID原则:
- 原子性
- 一致性
- 隔离性
- 多个业务可能操作同一个资源,防止数据损坏
- 持久性
- 事务一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久的写入到存储器中
13.2、spring中的事务管理
-
声明式事务:AOP
-
编程式事务:需要在代码中,进行事务的管理
-
Spring它对JDBC的隔离级别作出了补充和扩展,其提供了7种事务传播行为。
1、 PROPAGATION_REQUIRED:默认事务类型,如果没有,就新建一个事务;如果有,就加入当前事务。适合绝大多数情况。
2、PROPAGATION_REQUIRES_NEW:如果没有,就新建一个事务;如果有,就将当前事务挂起。
3、PROPAGATION_NESTED:如果没有,就新建一个事务;如果有,就在当前事务中嵌套其他事务。
4、PROPAGATION_SUPPORTS:如果没有,就以非事务方式执行;如果有,就使用当前事务。
5、PROPAGATION_NOT_SUPPORTED:如果没有,就以非事务方式执行;如果有,就将当前事务挂起。即无论如何不支持事务。
6、PROPAGATION_NEVER:如果没有,就以非事务方式执行;如果有,就抛出异常。
7、PROPAGATION_MANDATORY:如果没有,就抛出异常;如果有,就使用当前事务。
-
配置文件xml
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> </beans>
-
最终dao的配置xml
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!--DataSource:使用Spring的数据源替换MyBatis的配置 c3p0 dbcp druid 我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true &characterEncoding=utf8&useSSL=true&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root1@12"/> </bean> <!-- sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/mapper/UserMapper.xml"></property> </bean> <!-- 配置声明式事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg name="dataSource" ref="dataSource"/> </bean> <!-- 结合AOP实现事务的织入 --> <!-- 配置事务的通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 给哪些方法配置事务--> <!-- 配置事务的传播特性 new propagation--> <tx:attributes> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="delete"/> <tx:method name="update"/> <tx:method name="query" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务切入--> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* com.mapper.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> </beans>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!