回顾Spring5
1、Spring
1.1、简介
2002年,Rod Jahnson首次推出了Spring框架雏形interface21框架。
2004年3月24日,Spring框架以interface21框架为基础,经过重新设计,发布了1.0正式版。
很难想象Rod Johnson的学历 , 他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学。
Spring理念 : 使现有技术更加实用 . 本身就是一个大杂烩 , 整合现有的框架技术框架
官网 : http://spring.io/
官方下载地址 : https://repo.spring.io/libs-release-local/org/springframework/spring/
GitHub : https://github.com/spring-projects
https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/
目的:解决企业级应用开发的复杂性
SSH:Struct2+Spring+Heibernate
SSM:SpringMVC+Spring+Mybatis
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
1.2、优点
Spring是 一个开源的免费的框架(容器)!
Spring是一个轻量级的、非入侵式的框架!
控制反转(IOC),面向切面编程(AOP)
支持事务的处理,对框架整合的支持
总结:
Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!
1.3、Spring的组成
1.4、拓展
SpringBoot
一个快速开发的脚手架
基于SpringBoot可以快速的开发单个微服务
SpringCloud
SpringCloud基于SpringBoot实现的
大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要完全掌握Spring、SpringMVC
弊端:发展了多年以后,违背了原来的理念!配置十分繁琐,配置地狱
2、IOC理论推导
1、UserDao接口
2、UserDaoImpl实现类
3、UserService业务接口
4、UserServiceImpl业务实现类
在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改源代码,如果程序代码量十分大,修改一次的成本代价十分昂贵
我们使用一个Set接口实现
private UserDao userDao;
//利用set动态实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
之前,程序是主动创建对象!控制权在程序员手上
使用set注入后,程序不再具有主动性,而是变成了被动的接收对象
public class MyTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层他们不需要接触
UserService userService = new UserServiceImpl();
((UserServiceImpl)userService).setUserDao(new UserDaoMysqlImpl());
userService.getUser();
}
}

这种思想,从本质上解决了问题,我们程序员再也不用去管理对象的创建了
系统的耦合性大大降低,更加专注的在业务的实现上,这是IOC的原型!
IOC本质
控制反转(Inversion of Control)是一种设计思想,DI(依赖注入)实现IOC的一种方式也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)
3、HelloSpring
3.1、编写Hello实体类
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+ name );
}
}
3.2、编写我们的spring文件 , 这里我们命名为beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是java对象 , 由Spring创建和管理-->
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
</beans>
3.3、测试
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
读Spring源码:这里可以点进去看一下ClassPathXmlApplicationContext到ApplicationContext中间经历了很多层
总结:
Hello 对象是谁创建的 ? 【hello 对象是由Spring创建的
Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的
这个过程就叫控制反转 :
控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
依赖注入 : 就是利用set方法来进行注入的.
IOC是一种编程思想,由主动的编程变成被动的接收
4、IOC创建对象的方式
1、默认使用无参构造创建对象!
package com.kiki.pojo;
/**
* @author kiki
* @version 1.0
* @create 2020/7/6 20:51
*/
public class User {
private String name;
public User(){
System.out.println("user的无参构造");
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
<bean id="user" class="com.kiki.pojo.User">
<property name="name" value="kikikikiki"/>
</bean>
2、假设我们要使用有参构造
2.1、下标赋值
public User(String name){
this.name = name;
}
<bean id="user" class="com.kiki.pojo.User">
<!--第一种下标赋值-->
<constructor-arg index="0" value="下标为0"/>
</bean>
2.2第二种方式通过类型创建
不建议使用
<bean id="user" class="com.kiki.pojo.User">
<constructor-arg type="java.lang.String" value="kiki通过类型"/>
</bean>
2.3直接通过参数名来设置
<bean id="user" class="com.kiki.pojo.User">
<constructor-arg name="name" value="kiki通过参数名"/>
</bean>
Spring容器,配置文件中的bean,无论用不用就直接实例化了
在配置文件加载的时候,容器中管理的对象就已经初始化了
5、Spring配置
5.1、别名
<!--别名,如果添加了别名,也可以通过别名获取这个对象-->
<alias name="user" alias="userNew"/>
5.2、Bean的配置
<!--
id:bean的唯一标识符,相当于对象名
class:bean对象所对应的全限定名:包名+类名
name:也是别名,而且name可以同时取多个别名
-->
<bean id="user" class="com.kiki.pojo.User" name="user2,u2">
</bean>
5.3、import
这个import,一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="beans.xml"/>
</beans>
6、DI依赖注入
6.1、构造器注入
6.2、Set方式注入【重点】
依赖注入:本质Set注入
依赖:bean对象的创建依赖于容器
注入:bean对象的所有属性,由容器来注入
[环境搭建]
package com.kiki.pojo;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* @author kiki
* @version 1.0
* @create 2020/7/6 21:47
*/
@Data
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> game;
private String wife;
private Properties info;
}
package com.kiki.pojo;
/**
* @author kiki
* @version 1.0
* @create 2020/7/6 21:47
*/
public class Address {
private String address;
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
}
测试类
import com.kiki.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author kiki
* @version 1.0
* @create 2020/7/6 21:58
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.kiki.pojo.Student">
<!--第一种普通值注入value-->
<property name="name" value="kiki"/>
</bean>
</beans>
<?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="address" class="com.kiki.pojo.Address">
</bean>
<bean id="student" class="com.kiki.pojo.Student">
<!--第一种普通值注入value-->
<property name="name" value="kiki"/>
<!--第二种bean注入使用ref注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>写代码</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="12345678946465"></entry>
<entry key="银行卡" value="76876865464564"></entry>
<entry key="学生卡" value="98789764546323"></entry>
</map>
</property>
<!--Set-->
<property name="game">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--null值注入-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">123456789</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
</beans>
6.3、其他方式注入
p命名空间注入
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kiki.pojo.User" p:name="kiki" p:age="18"/>
2、c 命名空间注入 : 需要在头文件中加入约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<!--c命名空间注入,通过构造器注入:construct-args -->
<bean id="user2" class="com.kiki.pojo.User" c:name="嘻嘻" c:age="20"/>
官方文档有讲
6.4、Bean的作用域(默认单例)
单例模式(singleton):
原型模式(prototype):每次从容器中get的时候,都会产生一个新对象
其余的request、session、application这些只能在web中开发中使用到
7、Bean的自动装配
自动装配:是Spring满足Bean依赖的一种方式!
Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配的方式:
1、在xml中显示的装配
2、在java中显示配置
3、隐式的自动装配bean 【重要】
7.1、ByName自动装配
<bean id="cat" class="com.kiki.pojo.Dog"/>
<bean id="dog" class="com.kiki.pojo.Cat"/>
<bean id="people" class="com.kiki.pojo.People" autowire="byName">
<property name="name" value="kiki"/>
</bean>
7.2、ByType自动装配
<bean id="cat" class="com.kiki.pojo.Dog"/>
<bean id="dog" class="com.kiki.pojo.Cat"/>
<bean id="people" class="com.kiki.pojo.People" autowire="byType">
<property name="name" value="kiki"/>
</bean>
小结:
byName的时候,保证所有的bean的id唯一,并且这个bean需要和自动注入属性的set方法的值一样!
byType保证所有的bean的id唯一,并且这个bean需要和自动注入属性的属性的类型一致!
7.3使用注解实现装配
jdk1.5开始支持注解,Spring2.5就支持注解了
要使用注解首先在applicationContext.xml配置文件中配置
参考官网:https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-annotation-config
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>
@Autowired
使用注解后,在类中就不需要set方法了,底层使用反射实现,前提是你这个自动装配的属性IOC(Spring)容器中存在,且符合名字byType
如果显式的@Autowired(required=false),说明这个对象可以为null,否则不允许为null
@Nullable
表示该字段可以为null
@Autowired
@Qualifier(value = “xxx”)
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired完成时,我们可以使用@Qualifier(value = “xxx”)去配置,指定一个唯一的bean对象
@Resource(name=“xxx”) 是java注解的注入,默认通过byName,先通过id再通过类型
总结:
参考博客(https://blog.csdn.net/weixin_40423597/article/details/80643990)
@Resource和Autowired区别:
都是来实现自动装配的,都可以放在属性字段上
8、使用注解开发
在Spring4以后,要使用注解开发,必须要保证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: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.kiki.pojo"/>
</beans>
package com.kiki.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 8:29
*/
//等价于 <bean id="user" class="com.kiki.pojo.User"/>
@Component
public class User {
//相当于在bean中<property name="name" value="kiki"/>
@Value("kiki")
public String name;
public Integer age;
}
1、bean
2、属性注入
3、衍生注解
@Component有几个衍生的注解,会按照mvc三层架构分层
dao [@Repository]
service [@Service]
controller[@Controller]
这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配bean
4、自动装配
5、作用域
6、小结
xml与注解:
xml更加万能,适用于任何场合!维护简单方便
注解不是自己的类使用不了。维护相对复杂
xml与注解最佳实践:
xml用来管理bean
注解只负责完成属的注入
我们在使用的过程中必须要开启注解 支持
9、使用java的方式配置Spring
JavaConfig是Spring的一个子项目,在Spring4之后
package com.kiki.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 8:58
*/
public class User {
private String name;
public String getName() {
return name;
}
@Value("kiki")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
package com.kiki.config;
import com.kiki.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 9:20
*/
//类似于核心配置文件,代表这个是一个配置类,和beans.xml一样
@Configuration//这个也会被Spring容器托管,注册到容器中
@ComponentScan("com.kiki.pojo")
public class KikiConfig {
//注册一个bean就相当于我们之前写的一个bean
//这个方法的名字就相当于bean标签中的id属性
//这个方法的返回值就相当于bean标签中的class属性
@Bean
public User getUser(){
return new User();//就是返回要注入bean的对象
}
}
import com.kiki.config.KikiConfig;
import com.kiki.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 9:23
*/
public class MyTest {
public static void main(String[] args) {
//如果完全使用配置类来做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(KikiConfig.class);
User user = (User) context.getBean("getUser");
System.out.println(user.getName());
}
}
10、代理模式
为什么要学习代理模式?因为这就是springAOP的底层
代理模式分类:
静态代理:
动态代理:
10.1、静态代理
角色分析:
抽象角色:一般会使用接口或者抽象类来解决
真实角色:被代理的角色
代理角色:代理真实的角色,代理真实的角色后,我们一般做一些附属操作
客户:访问代理对象的人
1、接口
package com.kiki.demo01;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 10:15
*/
//租房
public interface Rent {
public void rent();
}
2、真实角色
package com.kiki.demo01;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 10:15
*/
//房东
public class Host implements Rent{
public void rent() {
System.out.println("房东要出租房子");
}
}
3、代理角色
package com.kiki.demo01;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 10:18
*/
//代理角色
public class Proxy implements Rent{
private Host host;
public Proxy() {
}
public Proxy(Host host) {
this.host = host;
}
public void rent() {
seeHouse();
host.rent();
hetong();
fare();
}
//看房
public void seeHouse(){
System.out.println("中介带你看房子");
}
//签合同
public void hetong(){
System.out.println("签署租赁合同");
}
//收中介费
public void fare(){
System.out.println("收中介费");
}
}
4、客户端访问代理角色
package com.kiki.demo01;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 10:16
*/
public class Client {
public static void main(String[] args) {
//房租要租房子
Host host = new Host();
//代理,中介带房东租房子但是呢,代理角色一般有一些附属操作
Proxy proxy = new Proxy(host);
//你不用面对房东直接找中介租房即可
proxy.rent();
}
}
代理的好处:
可以使真实角色的操作更加纯粹,不用去关注一些 公共非的业务
公共也就是交给代理角色!实现业务的分工!
公共业务发生扩展的时候,方便集中管理!
缺点:
一个真实角色会产生一个代理对象,代码量会翻倍,开发效率会降低
10.2、 动态代理
动态代理和静态代理角色一样
动态代理类是动态生成的不是直接写好的
动态代理分为两大类:
基于接口的动态代理:JDK动态代理
基于类的动态代理:cglib
java字节码实现:JAVAssist
需要 了解两个类:Proxy代理,InvocationHandler调用处理程序
proxy生成代理对象的
package com.kiki.demo04;
import com.kiki.demo03.Rent;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 19:07
*/
//用这个类自动生成代理类
public class ProxyInvocationHandler 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);
}
//处理代理实例,并返回结果,核心
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("[Debug]执行了"+msg+"方法");
}
}
动态代理的好处:
- 静态代理有的它都有,静态代理没有的,它也有!
- 可以使得我们的真实角色更加纯粹 . 不再去关注一些公共的事情 .
- 公共的业务由代理来完成 . 实现了业务的分工 ,
- 公共业务发生扩展时变得更加集中和方便 .
- 一个动态代理 , 一般代理某一类业务
- 一个动态代理可以代理多个类,代理的是接口!
11、AOP
什么是AOP
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP在Spring中的作用
提供声明式事务;允许用户自定义切面
以下名词需要了解下:
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
方式一、使用Spring的API接口
UserService
package com.kiki.service;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 22:13
*/
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
Log
package com.kiki.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 22:16
*/
public class Log implements MethodBeforeAdvice {
/**
*
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了!");
}
}
AfterLog
package com.kiki.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
* @author kiki
* @version 1.0
* @create 2020/7/10 22:46
*/
public class AfterLog implements AfterReturningAdvice {
/**
*
* @param returnValue 返回值
* @param method
* @param args
* @param target
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了:"+method.getName()+"返回结果为"+returnValue);
}
}
applicationContex.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.kiki.service.UserServiceImpl"/>
<bean id="log" class="com.kiki.log.Log"/>
<bean id="afterLog" class="com.kiki.log.AfterLog"/>
<!--配置AOP-->
<aop:config>
<!--切入点:我们需要在哪里执行Spring方法
execution(要执行的位置)
两个点代表任意参数
-->
<aop:pointcut id="pointcut" expression="execution(* com.kiki.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强
哪个类 切入到哪里
-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
MyTest
import com.kiki.service.UserService;
import com.kiki.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author kiki
* @version 1.0
* @create 2020/7/11 3:36
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
方式二、自定义类来实现AOP
自定义类DiyPotintCut
package com.kiki.diy;
/**
* @author kiki
* @version 1.0
* @create 2020/7/11 3:46
*/
public class DiyPotintCut {
public void before(){
System.out.println("[Debug]==========方法执行前========");
}
public void after(){
System.out.println("[Debug]==========方法执行后========");
}
}
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.kiki.service.UserServiceImpl"/>
<bean id="log" class="com.kiki.log.Log"/>
<bean id="afterLog" class="com.kiki.log.AfterLog"/>
<!--方式二-->
<bean id="diy" class="com.kiki.diy.DiyPotintCut"/>
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.kiki.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
方式三、使用注解来实现AOP
AnnocationPointCut
package com.kiki.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* @author kiki
* @version 1.0
* @create 2020/7/11 4:01
*/
//使用注解方式实现AOP
//使用注解将其标注为一个切面
@Aspect
public class AnnocationPointCut {
@Before("execution(* com.kiki.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("[Debug]==========方法执行前");
}
@After("execution(* com.kiki.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("[Debug]==========方法执行后");
}
//在环绕增强中我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.kiki.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("[Debug]==========环绕前");
// Signature signature = jp.getSignature();
// System.out.println("signature"+signature);
//执行方法
Object proceed = jp.proceed();
System.out.println("[Debug]==========环绕后");
}
}
applicationContext.xml
<!--方式三-->
<bean id="annocationPointCut" class="com.kiki.diy.AnnocationPointCut"/>
<!--开启注解支持
JDK(默认)
proxy-target-class="false"默认就是false就是JDk
cglib
proxy-target-class="true"则是cglib
-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
12、整合Mybatis
1、导入相关jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2、编写配置文件
3、测试
12.1、回忆mybatis
1.编写实体类
2、核心配置文件
<?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>
<!--引入外部配置文件-->
<properties resource="db.properties">
</properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/><!--标准的日志工厂实现-->
<!--<setting name="logImpl" value="LOG4J"/>-->
<!--开启驼峰命名转换-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!--可以给实体类起别名 ,这样在mapper.xml文件中就可以直接使用别名,简便-->
<!--<typeAlias type="com.kiki.pojo.User" alias="User"/>-->
<!--也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
每一个在包 domain.blog 中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。
-->
<package name="com.kiki.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" 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="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个mapper.xml需要在mybatis核心配置文件中注册-->
<mappers>
<!--<mapper resource="com/kiki/dao/*.xml"></mapper>-->
<package name="com.kiki.dao"/>
</mappers>
</configuration>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
3、
import com.kiki.mapper.UserMapper;
import com.kiki.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;
/**
* @author kiki
* @version 1.0
* @create 2020/7/11 9:11
*/
public class MyTest {
@Test
public void test() throws IOException {
String resources = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resources);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.selectUser();
for (User user : userList) {
System.out.println(user);
}
}
}
12.2、Mybatis-Spring
1、编写数据源
spring-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: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">
<!--DataSource:使用Spring数据源替换mybatis的配置-->
<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="123456"/>
</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/kiki/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate就是我们使用的SqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能通过构造器注入,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="com.kiki.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
<?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>
<!--引入外部配置文件-->
<properties resource="db.properties">
</properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/><!--标准的日志工厂实现-->
<!--<setting name="logImpl" value="LOG4J"/>-->
<!--开启驼峰命名转换-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!--可以给实体类起别名 ,这样在mapper.xml文件中就可以直接使用别名,简便-->
<!--<typeAlias type="com.kiki.pojo.User" alias="User"/>-->
<!--也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
每一个在包 domain.blog 中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。
-->
<package name="com.kiki.pojo"/>
</typeAliases>
<!--<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!–每一个mapper.xml需要在mybatis核心配置文件中注册–>
<mappers>
<!–<mapper resource="com/kiki/dao/*.xml"></mapper>–>
<package name="com.kiki.mapper"/>
</mappers>-->
</configuration>
package com.kiki.mapper;
import com.kiki.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
/**
* @author kiki
* @version 1.0
* @create 2020/7/11 9:51
*/
public class UserMapperImpl implements UserMapper{
//我们的所有的操作在原来都是用sqlSession来执行,现在都使用sqlSessionTemplate;
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
List<User> userList = userMapper.selectUser();
for (User user : userList) {
System.out.println(user);
}
}
2、sqlSessionFactory
3、sqlSessionTemplate
4、需要给接口加实现类
5、测试
13、声明式事务
1、回顾事务
把一组业务当成一个业务来做,要么都成功,要么都失败
事务ACID原则:
原子性
一致性
隔离性:多个业务可能操作同一个资源,防止数据损坏
持久性:事务一旦提交,无论系统发生什么问题,结果都不会被影响,被持久化的写入到储存器中!
2、Spring中的事务管理
问:
为什么需要事务?
如果不配置事务,可能存在数据提交不一致的情况
浙公网安备 33010602011771号