笔记: 参考链接
参考视频:B站-狂神说Java
1.介绍Spring5
1.1 简介
目的:解决企业级应用开发的复杂性
范围:任何Java应用
- spring是一个轻量级,非入侵式的控制反转(IOC)和面向切面(AOP)的容器框架。
- IOC的核心是工厂模式,AOP的核心是代理模式
- 本身是个大杂烩,整合了现有的技术框架!容器
- SSH:Struct2+Spring+Hibernate
- SSM:SpringMvc+Spring+Mybatis
- 官网
- 官方下载地址
- github源码
- 中文文档
- maven
- spring-webmvc依赖:很全,会帮我们导入很多包。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.21</version>
</dependency>
1.2优点
- Spring是一个开源的免费的框架(容器)
- Spring是一 个轻量级的、非入侵式的 控制反转(IOC),面向切面编程(AOP) 框架
- 支持事务的处理,支持对框架的整合
1.3 七大模块
1.4 拓展
- Spring Boot
- 一个快速开发的脚手架。
- 基于SpringBoot可以快速的开发单个微服务。
- 约定大于配置
- Spring Cloud
- SpringCloud是基于SpringBoot实现的。
- spring和springMVC是学习->SpringBoot和SpringCloud的前提。
- 弊端:发展了太久之后,违背了原来的理念! 配置十分繁琐,人称:”配置地狱!”
2.IOC理论指导
●之前,程序是主动创建对象,控制权在程序员手上。
●使用了set注入后,程序不再具有主动性,而是变成了被动的接受对象!把创建对象的控制权交出。
● 这种思想,从本质上解决了问题,我们程序猿不用再去管理对象的创建了。系统的耦合性大大降低,可以更加专注的在业务的实现上!
- 控制反转loC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现loC的一种方法。没有IOC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,所谓的控制反转就是获得依赖对象的方式反转了。
- set的注入实现上下层的解耦:
- loC是Spring框架的核心内容,使用多种方式完美的实现了loC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IoC。
3.HelloSpring(小demo)
3.1 父项目
新建不用模板的空maven父项目,取名:spring-study
删掉里面的src文件夹
在pom.xml中配置spring依赖
3.2 子项目
新建不用模板的空maven子项目,取名:spring-02-hellospring
3.3 编写实体类
3.4 编写spring.xml
我们命名为beans.xml (对象new好了) set注入成员变量
<?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">
<!--使用Spring来创建对象,在Spring这些都称为Bean-->
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
3.5 测试
//固定,用xml加载必须写这句话ClassPathXmlApplicationContext
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象现在都在spring中管理了,我们要使用,直接去里面取出来就可以了
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
3.6 总结
控制反转:
控制: 谁来控制对象的创建, 传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的.
反转: 程序本身不创建对象,而变成被动的接收对象.
依赖注入: 就是利用set方法来进行注入的.
IOC是一种编程思想,由主动的编程变成被动的接收.
可以通过new ClassPathXmlApplicationContext去浏览一下底层源码 .
OK ,到了现在,我们彻底不用再程序中去改动了,要实现不同的操作,只需要在xmI配置文件中进行
修改(beans.xml:spring托管对象,注入依赖即可),所谓的IOC,一句话搞定:对象由Spring来创建,管理,装配
4. IOC创建对象的方式
4.1 默认实现方式:使用类的无参构造创建对象(无参构造方法+set注入依赖对象)
用set方法注入属性。(类似于上面的例子)
4.2 .如果我们使用有参构造创建对象,我们通过构造方法注入依赖(有参构造方法,构造对象的同时,注入依赖对象)
有好几种方式实现:推荐直接通过参数名
<bean id="user" class="com.hou.pojo.User">
<constructor-arg name="name" value="hou"></constructor-arg>
</bean>
注:在配置文件加载的时候,容器中管理的对象就已经初始化了。这就会导致多次调用同一名字的对象,实质上就是一个对象。
5.Spring配置
5.1 alias
<bean id="user" class="com.hou.pojo.User">
<constructor-arg name="name" value="hou"></constructor-arg>
</bean>
<!--可以使用别名获取到这个对象-->
<alias name="user" alias="user2aaa"/>
5.2 Bean的配置
id:bean的id标识符
class:bean对象所对应的类型
name:别名,更高级,可以同时取多个别名。
5.3 import
<import resource="spring-dao.xml"/>
一般用于团队开发,它可以将多个配置文件,导入合并为一个。
6. DI依赖注入
- 依赖: bean对象的创建依赖于spring容器
- 注入: bean对象中的所有属性,由容器来注入
6.1 构造器注入(在4已经说过了)
6.2 set方式注入 (重点)
不同数据类型的的注入方式不同:
1.写类
2.在beans.xml中配置
3.测试
6.3 p标签和c标签
1.写类
2.在user.xml中配置
3.编写测试
6.4 Bean的作用域
6.4 1单例模式(默认)
<bean id="use2" class="com.pojo.User" c:name="kun" c:age="19" scope="singleton"></bean>
6.4.2 多实例模式
每次从容器中get的时候,都产生一个新对象!
<bean id="use2" class="com.pojo.User" c:name="kun" c:age="19" scope="prototype"></bean>
其余的request、session、application这些只能在web开发中使用!
7. Bean的自动装配(属性) 【重要】
- Spring会在上下文自动寻找,并自动给bean装配属性
- 在Spring中有三种装配的方式
- 在xml中显示配置(如上面)
- 在java中显示配置(第9节内容)
- 隐式的自动装配bean 【重要】(本节内容)
环境搭建:一个人有两个宠物
public class Cat {
public void jiao(){
System.out.println("miao");
}
}
public class Dog {
public void jiao(){
System.out.println("wow");
}
}
package com.pojo;
public class People {
private Cat cat;
private Dog dog;
private String name;
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
7.1 配置autowire实现自动装配
<?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">
<bean id="cat11" class="com.pojo.Cat"/>
<bean id="dog" class="com.pojo.Dog"/>
<!--byname会在bean.xml中自动查找,和自己set方法后面的值对应的id的bean对象-->
<!--<bean id="people" class="com.pojo.People" autowire="byName">-->
<!--<property name="name" value="hou"></property>-->
<!--</bean>-->
<!--byType会在bean.xml中自动查找,和自己对象类型相同的bean对象-->
<bean id="people" class="com.pojo.People" autowire="byType">
<property name="name" value="hou"></property>
</bean>
</beans>
- byname自动装配:byname在bean.xml中自动查找,和自己set方法后面的名和的bean对象的id对应,此时需要保证所有的bean对象的id是唯一的。
- bytype自动装配:byType会自动查找,和自己对象的成员属性类型相同的bean,此时需要保证所有bean对象的class是唯一的。
7.2 使用注解实现自动装配
jdk1.5支持的注解,spring2.5支持的注解,此种方式比xml的方式更好。
7.2.1 在xml中配置注解支持
- 导入约束:
xmlns:context="http://www.springframework.org/schema/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>
3.beans对象
<bean id="cat" class="com.pojo.Cat"/>
<bean id="dog" class="com.pojo.Dog"/>
<bean id="people" class="com.pojo.People"/>
7.2.2 @Autowire(常用)
在属性上个使用,也可以在set上使用
我们可以不用编写set方法了
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
7.2.3 @Nullable
@Nullable 字段标志的注解,说明这个字段可以为null
7.2.4 @Qualifier(value = “dog”)
如果@Autowired自动装配环境比较复杂。自动装配无法通过一个注解【@Autowire】完成的时候
我们可以使用@Qualifier(value = “dog”)去配合使用,指定一个唯一的id对象
public class People {
@Autowired
private Cat cat;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
private String name;
}
7.1.5 @Resource(name=“dog”)
@Resource(name=“dog”),它是Java包下的注解
区别:
@autowire通过byType实现,而且必须要求这个对象存在
@resource默认通过byName实现,如果找不到,通过byType实现,找不到就报错
8.使用注解开发(对象创建和属性注入都使用注解来完成)
环境:保证spring4++,必须要保证AOP的包导入
1.使用注解需要导入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>
8.1 bean
@Component
8.2 属性如何注入
@Value(“dong”)
@Component
public class User {
@Value("dong")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
8.3 衍生的注解
@Component有几个衍生注解,会按照web开发中,mvc架构中分层。
- dao (@Repository)
- service(@Service)
- controller(@Controller)
这四个注解功能一样的,都是代表将某个类注册到容器中
8.4 自动装配
(第7点的内容)
8.5 作用域
@Scope(“prototype”) 多实例模式
@Component
@Scope("prototype")
public class User {
@Value("dong")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
8.6 小结 (xml和注解结合最佳方式)
- xml与注解:
- xml更加万能,维护简单
- 注解,不是自己的类,使用不了,维护复杂
- 最佳实践:
- xml用来管理bean
- 注解只用来完成属性的注入
8.7最后提醒
使用注解时:一定要有对应的注解依赖支持
<?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/>
<!--指定要扫描的包-->
<context:component-scan base-package="com.pojo"/>
</beans>
9.使用java方式配置spring
JavaConfig是Spring的一个子项目,在spring4之后,他成为了核心功能
1.类
// @Component是指:这个类被spring接管了,注册到了容器中
@Component
public class User {
@Value("dong")//属性注入值
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2.配置类
@Configuration //这个也会被spring容器托管,注册到容器中,因为他本来就是一个@Component
//@Configuration代表这是一个配置类,就和我们之前看到的beans.xml一样的
//ComponentScan:扫描
@ComponentScan("com.pojo")
@Import(Config2.class)//导入合并其他配置类
public class MyConfig {
//注册一个bean,就相当于我们之前写的一个bean标签
//这个方法的名字,就相当于bean标签中的id属性
//这个方法的返回值,就相当于bean标签中的class属性
@Bean
public User getUser(){
return new User();
}
}
3.测试
这种纯java配置方式,在springboot中,随处可见
《spring就两种:注解版的spring和配置版的spring》 java方式实质上也是注解版本。
补充:给容器中注册组件(bean)的几种方式
包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)
Bean的配置
@import 合并导入其他的配置类
10.代理模式
代理模式是SpringAOP的底层实现。
【面试必问:SpringAOP,SpringMVC】
- 代理模式分类:
- 静态代理
- 动态代理
代理模式:我和房东通过中介实现租房这个动作。
10.1 静态代理
-
角色分析:
- 抽象角色:一般会使用接口或者抽象类来解决-------(租房)
- 真实角色:被代理的角色-------(房东)
- 代理角色:代理真实角色,代理真实角色后,一般会做一些附属操作(收中介费,签合同等)------(中介)
- 客户:访问代理对象的人--------(客户)
-
代码步骤:
- 1.接口(抽象角色)
//抽象角色:实现房屋出租这件事
public interface Rent {
public void rent();
}
2.真实角色
//房东:租房
public class Host implements Rent{
@Override
public void rent() {
System.out.println("房东要出租房子");
}
}
3.代理角色
//代理角色 代理真实角色(成员属性)实现implements某些功能(租房)
public class Proxy implements Rent {
private Host host;
public Proxy() {
}
public Proxy(Host host) {
this.host = host;
}
@Override
public void rent() {
host.rent();
seeHouse();
contract();
fare();
}
//看房
public void seeHouse(){
System.out.println("中介带你看房");
}
//合同
public void contract(){
System.out.println("中介和你签合同");
}
//收费
public void fare(){
System.out.println("收中介费");
}
}
4.客户访问代理角色
//客户角色
public class Client {
public static void main(String[] args) {
//找代理
Host host = new Host();
Proxy proxy = new Proxy(host);
//找代理租房
proxy.rent();
}
}
代理模式的优点:不用改动原来代码,实现一些扩展功能
●可以使真实角色的操作更加纯粹!不用去关注一 些公共的业务
●公共也就就交给代理角色!实现了业务的分工!
●公共业务发生扩展的时候,方便集中管理!
缺点:
●一个抽象接口产生一个代理角色。
10.2静态代理再理解
10.3 动态代理
- 解决上述静态代理的缺点。
- 动态代理的代理类是动态生成的,不是我们直接写好的!(写模板,通过模板调用接口,自动生成)
- 动态代理分为两大类:基于接口的动态代理, 基于类的动态代理
- 基于接口---------- JDK动态代理 【使用】
- 基于类: cglib
- java字节码实现: javasist
- 需要了解两个类:InvocationHandler类的invoke方法-----调用处理程序并返回结果、Proxy.newProxyInstance静态方法----生成动态代理实例
- 静态代理和动态代理的区别
- 静态代理:一个代理只能服务于一个接口,一般就是对应的一类业务。
- 动态代理:一个代理类可以服务不同的接口,对应于所有的业务对象。
1.接口
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
2.真实对象
//真实对象
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("改变了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
3.动态代理类
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//会这个类,自动生成代理类
public class ProxyInvocation implements InvocationHandler {
//被代理的接口:代理谁
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//生成代理类
public Object getProxy(){
//实参(加载到类代码的位置,表示代理的接口,代表自己的InvocationHandler类)
return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces()
,this);
}
//处理代理实例,并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法执行前执行的方法
log(method.getName());
Object result = method.invoke(target, args);
return result;
}
public void log(String msg){
System.out.println("执行了"+msg+"方法");
}
}
4.客户
public class Client {
public static void main(String[] args) {
//真实角色
UserServiceImpl userService = new UserServiceImpl();
//代理角色,不存在
ProxyInvocation proxyInvocation = new ProxyInvocation();
proxyInvocation.setTarget(userService);//设置要代理的对象
//动态生成代理类对象
UserService proxy = (UserService)proxyInvocation.getProxy();
//调用接口方法
proxy.delete();
}
}
11.AOP
11.1 什么是AOP
AOP是一种横向编程的思想,在不影响原来业务类的情况下,实现动态的增强。
AOP (Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
11.2 AOP在Spring中的作用
- 提供声明式事务;允许用户自定义切面
- 了解名词:
●横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等
●切面(ASPECT) :横切关注点被模块化的特殊对象。即,它是一个类。
●通知(Advice) :切面必须要完成的工作。即,它是类中的一个方法。
●目标(Target) :被通知对象。
●代理(Proxy) :向目标对象应用通知之后创建的对象。
●切入点(PointCut) :切面通知执行的“地点”的定义。
●连接点(JointPoint) :与切入点匹配的执行点。
11.3 使用Spring实现AOP
导入依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
方式一:使用Spring的API接口
1.接口类
public interface UserService {
void add();
void delete();
void query();
void update();
}
2.UserServiceImp实现类
public class UserServiceImp implements UserService {
public void add() {
System.out.println("add");
}
public void delete() {
System.out.println("delete");
}
public void query() {
System.out.println("query");
}
public void update() {
System.out.println("update");
}
}
3.前置执行类
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+method.getName());
}
}
4.后置执行类
public class AfterLog implements AfterReturningAdvice {
//returnVaule: 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName()+returnValue);
}
}
5.在资源文件夹下新建ApplcationContext.xml,配置SpringAPI
<?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/beanss
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userservice" class="com.service.UserServiceImp"></bean>
<bean id="log" class="com.log.Log"/>
<bean id="afterlog" class="com.log.AfterLog"/>
<!--配置aop-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置)-->
<aop:pointcut id="point" expression="execution(* com.service.UserServiceImp.*(..))"/>
<!--执行环绕-->
<aop:advisor advice-ref="log" pointcut-ref="point"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="point"/>
</aop:config>
</beans>
6.测试类
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplcationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userservice");
//调用接口的方法
userService.add();
}
}
方式二:自定义来实现AOP【主要是切面定义】
1.自定义类
public class DiyPointcut {
public void before(){
System.out.println("before");
}
public void after(){
System.out.println("after");
}
}
2.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userservice" class="com.service.UserServiceImp"></bean>
<bean id="log" class="com.log.Log"/>
<bean id="afterlog" class="com.log.AfterLog"/>
<bean id="diy" class="com.diy.DiyPointcut">
</bean>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.service.UserServiceImp.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
3.测试代码不变
方法三:注解方式
1.编写注解类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//方式三:使用注解方式实现AOP
@Aspect //标注这个类是一个切面
public class Annotation {
@Before("execution(* com.service.UserServiceImp.*(..))")
public void before(){
System.out.println("before");
}
@After("execution(* com.service.UserServiceImp.*(..))")
public void after(){
System.out.println("after");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取切入的点
@Around("execution(* com.service.UserServiceImp.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("around");
Object proceed = joinPoint.proceed();
System.out.println("after around");
}
}
2.配置
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="ann" class="com.diy.Annotation"></bean>
<!--打开aop注解依赖-->
<aop:aspectj-autoproxy/>
<!--注册bean-->
<bean id="userservice" class="com.service.UserServiceImp"></bean>
</beans>
3.测试代码不变
12.整合Mybatis
中文文档:mybatis-spring
12.1 回顾mybatis的步骤
- 1.导入相关jar包
<dependencies>
<!--支持单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--mysql包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.21</version>
</dependency>
<!--Spring操作数据库,需要一个Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--面向切面编程AOP-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--mybatis-spring--->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
- 2.连接数据库
- 3.编写连接数据库配置文件mybatis-config.xml
- 4.编写实体类
- 5.编写接口(实现增删改查的接口)
- 6.编写UserMapper.xml实现类
- 7.测试类
注:
- 在mybatis-config.xml注册mapper
- 在pom.xml中加入build防止资源导出失败(未解决,还是导出不了,需要手动复制)
测试即可。
12.2 Mybatis-Spring
注:导入依赖包,需要在官网查看好对应版本号
12.2.1 过程
方式一:
1.专注做spring-mybatis之间的连接(spring-dao.xml)
- 编写数据源配置
- sqlSessionFactory
- 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--专注做spring-mybatis之间的连接-->
<!--DataSource:使用Spring的数据源替换Mybatis的配置
这里使用Spring提供分JDBC:org.springframework.jdbc.datasource
对象的成员变量
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="xxxx"/>
</bean>
<!--SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--之后只需要改这一部分即可-->
<!--绑定Mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/zhang/mapper/UserMapper.xml"/>
</bean>
<!--sqlSession-->
<!--SqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为该类没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
2.需要给接口加实现类
import com.zhang.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
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();
}
}
3.将自己写的实现类注入到spring中(在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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.zhang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
4.测试使用即可。
方式二:
有两处区别:
12.2.2 总结
整合过程:(把所有的new对象都交给spring去做)
注:需要一个实现类,实现UserMapper接口
总结过程:
有个实体类------------------------ 作用:(与数据库的对象属性对应)
有个业务接口 ----------------------- 作用:(做什么:增删改查)
有个业务实现xml---------------------作用:(具体如何做)
整合mybatis到spring中------------------------------spring-dao.xml
写一个业务的实现类,注入到spring中-------------------------作用:调用实现
通过spring来测试------------------作用:面向spring的编程
13.声明式事务
13.1回顾事务
- 事务保证了数据的一致性和完整性。
- 我现在要做的:在不改变原有的代码下实现事务
- spring中配置事务的重要性:
●如果不配置事务,可能存在数据提交不一致的情况
●如果我们不在spring中去配置声明式事务,我们就需要在代码中手动配置事务
13.2 Spring中的事务管理
- 声明式事务:AOP的应用
- 编程式事务
过程:
1.bean配置声明式事务
2.结合AOP实现事务置入
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-tx.aop">
<!--data source-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://111.230.212.103:3306/mybatis?userSSL=true&
userUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="xxxxx"/>
</bean>
<!--sqlsession-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<!--bound mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/mapper/*.xml"/>
</bean>
<!--sqlSession-->
<!--SqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为该类没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="datasource" />
</bean>
<!--结合aop实现事务置入-->
<!--配置事务的类-->
<!--确认包导入进去(spring-tx)-->
<tx:advice id="tx1" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性-->
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txpointxut" expression="execution(* com.mapper.*.*(..))"/>
<aop:advisor advice-ref="tx1" pointcut-ref="txpointxut"/>
</aop:config>
</beans>
注:这里的mapper包就是之后的dao包
详情见:https://www.cnblogs.com/tjlstudy/p/13038508.html
13.3 总结事务处理的方式
1.javaweb中(13.2小节,JDBC事务)
2.spring的声明式事务处理(上述方式)