Spring学习

1.Spring

1.1简介

  • Spring:春天---->给软件行业带来了春天

  • 2002年,首次推出了Spring框架的雏形,inferface21框架

  • Spring以interface21框架为基础与2004年3月24日正式发布1.0版本

  • Spring理念:是现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架

  • SSH: truct2 + Spring + Hibernate!

  • SSM: SpringMvc + Spring + Mybatis!

官网:https://spring.io/

官方下载地址:https://repo.spring.io/release/org/springframework/spring/

GitHub: https://github.com/spring-projects/spring-framework

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

1.2 优点

  • 是一个开源的免费的框架
  • 是一个轻量级,非入侵式的框架
  • 控制反转(IOC),面向切面编程(AOP)
  • 支持事务的处理,对框架整合的支持

总结:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

1.3组成

image

1.4 拓展

在官网有这个介绍:现代化的java开发(基于Spring的开发)
image

  • Spring Boot
  • 一个快速开发的脚手架框架
  • 可以快速开发单个微服务
  • 约定大于配置!
  • Spring Cloud
  • 基于SpringBoot实现的

2.IOC理论指导

  • 之前,程序是主动创建对象,控制权在程序员手上
  • 使用set注入后,程序不再具有主动性,而是变成了被动的接受对象

这种思想,从本质上解决了问题,让我们程序员不再去管理对象的创建了。系统的耦合性大大降低了,可以更加专注在业务的实现上!这是IOC的原型

IOC的本质

控制反转loC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现lIoC的一种方法,也有人认为DI只是loC的另一种说法。没有loC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是loC容器,其实现方法是依赖注入(Dependency Injection,DI).

3创建一个Hello Spring程序

  • Hello 对象是谁创建的
    • 是有Spring创建的
  • Hello 对象的属性是怎么设置的
  • 由Spring容器设置的
    这个过程就叫控制反转:
    控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的
    反转:程序本身不创建对象,而变成被动的接受对象
    依赖注入:就是利用set方法来进行注入的。
    IOC是一种编程思想,有主动的编程变成被动的接受。

所谓的IOC,就是对象由Spring来创建,管理,装配!

4.IOCC创建对象的方式

1.使用无参构造,默认实现!

<!--使用Spring来创建对象,在Spring这些都成为Bean
    bean=对象 new Hello()
    id = 变量名
    class = new的对象
    property 相当于给对象中的属性设置一个值
    -->

    <bean id="user" class="top.yqqk.pojo.User">
        <!--value:具体的值,基本数据类型;ref:引用Spring容器中创建好的对象-->
        <property name="name" value="孟"/>
    </bean>

2.假设我们要使用有参构造创建对象。

  • 1下标赋值
<bean id="user" class="top.yqqk.pojo.User">
<!--index 有参构造参数所在的位置下标-->
	<constructor-arg index="0" value="孟"/>
</bean>

2.通过类型创建不建议使用

<bean id="user" class="top.yqqk.pojo.User">
        <constructor-arg type="java.lang.String" value="孟"/>
</bean>

3.参数名构造

<bean id="user" class="top.yqqk.pojo.User">
    <constructor-arg name="name" value="孟"/>
</bean>

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!

5.Spring配置(applicationContext.xml:官方推荐的命名)

5.1,别名

<!--别名 如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="userAlias"/>

5.2,Bean的配置

    <!-- id:bean的唯一标识符,也就是相当于变量名;class:bean对象所对应的权限定名(包名+类型);name:也是别名,可以同时取多个别名-->
    <bean id="user" class="top.yqqk.pojo.User" name="userBean">
    <!--value:具体的值,基本数据类型;ref:引用Spring容器中创建好的对象-->
        <constructor-arg name="name" value="孟"/>
    </bean>

5.3,import

import 一般用于团队开发,他可以将多个配置文件,导入合并为一个

6.依赖注入

6.1 构造器注入

  • 就是前面的参数名构造

6.2 Set方法注入【重点】

  • 依赖注入: Set注入!
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入
      【环境搭建】
      1.复杂类型
public class Address {
    private String address;

    public Address() {
    }
    public Address(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

2.真实测试对象

public class Student {
    private String name;//姓名
    private Address address;//地址
    private String [] books;//书,课本
    private List<String> hobbys;//爱好
    private Map<String,String> card;//身份信息
    private Set<String> games;//游戏
    private Properties info;
    private String wife;//妻子的名字
}

3.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="top.yqqk.pojo.Address"></bean>
    <bean id="student" class="top.yqqk.pojo.Student">
        <!--第一种,普通值注入,values-->
        <property name="name" value="Spring学习"/>

        <!--第二种,Bean注入 ref-->
        <property name="address" ref="address"/>

        <!--第三种,数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <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="5209771314"/>
                <entry key="年级" value="333444"/>
            </map>
        </property>

        <!--第六种,Set注入-->
        <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>和平精英</value>
            </set>
        </property>

        <!--第七种,null注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--第八种,Properties-->
        <property name="info">
            <props>
                <prop key="身份证">1111112222221212</prop>
                <prop key="性别">男</prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>
</beans>

4.测试

@Test
    public void myTest01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }

6.3 拓展方式注入

1.P命名空间
引入约束: xmlns:p="http://www.springframework.org/schema/p"
image

2.C命名空间(通过构造器注入:constructor-arg)
引入约束: xmlns:c="http://www.springframework.org/schema/c"
image

6.4 Beans的作用域

image

1.单例模式(Spring默认机制)

<bean id="address" class="top.yqqk.pojo.Address" scope="singleton"></bean>

image
2.原型模式:每次从容器中get的时候,都会产生一个新的对象

<bean id="address" class="top.yqqk.pojo.Address" scope="prototype"></bean>

image

3.其余的request,session,application,websocket只在web开发中使用

7.Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会在上下文中自动查找,并自动给bean装入属性

在Spring中有三种装配方式:
1.在xml中显示配置
2.在java中显示配置
3.隐式的自动装配bean【重点】
- autowire:自动装配
- byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的bean!【需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法后面的值要一致】
- byType:会自动在容器上下文查找,和自己对象属性相同的bean!【需要保证所有bean的cless唯一,且这个bean需要和自动注入的属性的类型要一致】

7.1自动装配【autowire="byName"】

<?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="cat" class="top.yqqk.pojo.Cat"></bean>
    <bean id="dog" class="top.yqqk.pojo.Dog"></bean>
    <!--autowire:自动装配 byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的bean!-->
    <bean id="people" class="top.yqqk.pojo.People" autowire="byName">
        <property name="name" value="自己呀"/>
    </bean>
</beans>

7.2自动装配【autowire="byType"】

<?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 class="top.yqqk.pojo.Cat"></bean>
    <bean class="top.yqqk.pojo.Dog"></bean>
    <!--autowire:自动装配
    byType:会自动在容器上下文查找,和自己对象属性相同的bean!
    -->
    <bean id="people" class="top.yqqk.pojo.People" autowire="byType">
        <property name="name" value="自己呀"/>
    </bean>
</beans>

7.3使用注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解开发

要使用注解开发须知:
1.导入约束:
context约束:xmlns:context="http://www.springframework.org/schema/context"
2.配置注解的支持: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方式上使用!

使用@Autowired我们可以不用编写Set方法,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byname标准!

** @Autowired(required = false)**
定义了Autowired的required属性为false,说明这个对象可以为空

如果@Autowired自动装配的环境比较复杂,自动装备无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value="xxx")去配合@Autowired的使用,指定一个唯一的bean对象注入!

@Resource 也实现了自动装配

8 使用注解开发

在Spring之后,要使用注解开发,必须要保证aop的包导入了

  • @Component:组件,放在类上,说明这个类被Spring管理了 就是bean!
    1.bean
    2.属性如何注入
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component // 相当于 <bean id="user" class="top.yqqk.pojo.User"/>
public class User {
    @Value("孟子") // 相当于<property name="name" value="孟子"/>
    public String name ;
}

3.衍生的注解
@Component有几个衍生的注解,我们在web开发中,会按照mvc三层架构分层

  • dao 【@Repository】
  • service 【@Service】
  • controller 【@Controller】
  • pojo 【@Component】

这几个注解功能都一样,都代表将某个类注册到Spring中,装配bean

4.自动装配置

  • 见7.2自动装配

5.作用域
@Scope("XXXXX")

使用java的方式配置Spring

javaConfig是Spring的一个子项目,在Spring4之后,他成为了一个核心功能

1.实体类User类:

package top.yqqk.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    @Value("学习Spring")
    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.在配置类中BeanConfig:

package top.yqqk.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import top.yqqk.pojo.User;


//这个也会被Spring容器托管,注册到容器中,因为他本来就是一个@Component
//@Component,代表一个配置类
@Configuration
@ComponentScan("top.yqqk.pojo")
public class BeanConfig {
    //注册一个Bean, 相当于我们之前写的bean标签
    //这个方法的名字,相当于bean标签中id属性
    //这个方法的返回值,相当于bean标签中class属性
    @Bean
    public User getUser(){
        return new User(); //就是返回要注入到bean的对象!
    }
}

3.在测试类中MyTest中:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import top.yqqk.config.BeanConfig;
import top.yqqk.pojo.User;

public class MyTest {
    @Test
    public void myTest(){
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        User getUser =(User) context.getBean("getUser");
        System.out.println(getUser.getName());
    }
}

这种纯java的配置方式,在SpringBoot中随处可见

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.代理角色

public class Proxy implements Rent {

    private Host host;

    public Proxy() {
    }

    public Proxy(Host host) {
        this.host = host;
    }

    //租房
    @Override
    public void rent() {
        seeHost();
        host.rent();
        heTong();
        fare();

    }
    //看房
    public void seeHost(){
        System.out.println("中介带你看房");
    }

    //收中介费
    public void fare(){
        System.out.println("中介收费了");
    }
    //签合同
    public void heTong(){
        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 动态代理

  • 动态代理和静态代理角色一样
  • 动态代理的代理类是动态生成的,不是我们直接写好的
  • 动态代理分为两类:基于接口的动态代理,基于类的动态代理
    • 基于接口:JDK动态代理
    • 基于类:cglib
    • java字节码:javassist

需要了解两个类:Proxy(代理),InvocationHandler(调用处理程序)

动态代理的好处:

  • 继承了静态代理类的好处
  • 一个动态代理类的就是一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要是实现了同一个接口即可!

11.AOP

11.1 什么是AOP

AOP (Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

image

11.2 AOP在Spring中的作用

提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等....

  • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。

  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。

  • 目标(Target):被通知对象。

  • 代理(Proxy):向目标对象应用通知之后创建的对象。

  • 切入点(Pointcut):切面通知执行的“地点"的定义。

  • 连接点(JointPoint):与切入点匹配的执行点。

在SpringAOP中,通过Advice定义横切逻辑,在Spring中支持5中类型的Advice:

image

即Aop在不改变原有代码的情况下,去增加新的功能

11.3 使用Spring实现

【重点】使用AOP织入,需要导入一个依赖包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>

方式一:使用Spring的API接口
小练习结构图:
image

AfterLog类:

package top.yqqk.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    //o:返回值
    //method:要执行的目标对象的方法
    //objects:参数
    //o1:目标对象
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"返回结果为:"+o);
    }
}

Log类:

package top.yqqk.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

    //method:要执行的目标对象的方法
    //objects:参数
    //o:目标对象
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");

    }
}

UserService接口:

package top.yqqk.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

UserServiceImpl类:

package top.yqqk.demo04;

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("查询了一个用户");
    }
}

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">

    <bean id="userService" class="top.yqqk.service.UserServiceImpl" />
    <bean id="log" class="top.yqqk.log.Log"/>
    <bean id="afterLog" class="top.yqqk.log.AfterLog"/>

    <!--配置AOP-->
    <aop:config>
        <!--切入点: expression:表达式,execution(要执行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* top.yqqk.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        <!---->
    </aop:config>
</beans>

测试类MyTest

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.yqqk.service.UserService;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

方式二:自定义来实现AOP

新建自定义类DiyPointCut:

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
        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="userService" class="top.yqqk.service.UserServiceImpl" />
    <!--配置AOP-->
    <bean id="diy" class="top.yqqk.diy.DiyPointCut"></bean>
    <aop:config>
        <!--自定义切面-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="pointcut" expression="execution(* top.yqqk.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

测试类MyTest:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.yqqk.service.UserService;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

3.使用注解实现AOP
切面类AnnotationPointCut:

package top.yqqk.diy;

//使用注解的方式实现AOP

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect  //标记这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* top.yqqk.service.UserServiceImpl.*(..))")
    public void before(){
        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
        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="userService" class="top.yqqk.service.UserServiceImpl" />
    <bean id="annotationPointCut" class="top.yqqk.diy.AnnotationPointCut"/>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
</beans>

测试类MyTest同上

12 整合Mybatis

步骤:

1.导入相关的jar包

  • junt
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring 【new】

2.编写配置文件
3.测试

12.1 回顾Mybatis

1.编写实体类
2.编写核心配置文件
3.编写接口
4.编写Mapper
5.测试

12.2 Mybatis-spring

网站:http://mybatis.org/spring/zh/index.html

1.编写数据源配置【DataSource】
2.SqlSessionFactory
3.SqlSessionTemplate
4.需要给接口加实现类
5.将实现类注入到bean中
6测试使用即可

13 spring声明式事务

1.回顾事务

  • 要么都成功,要么都失败
  • 事务在项目开发中,十分重要,涉及到数据的一致性问题!
  • 确保完整性和一致性

事务ACID原则:

  • 原子性
    • 要么都成功,要么都失败
  • 一致性
    • 要么都成功,要么都失败
  • 隔离性
    -防止数据损坏
  • 持久性
    • 事务一旦提交,无论系统发生问题,结果都不会被影响,被持久化的写到存储器中

1.spring中的事务管理

  • 声明式事务:【AOP应用】
  • 编程式事务:需要在代码中,进行事务的管理

为什么需要事务:

  • 如果不配置事务,可能存在数据提交不一致的情况下;
posted @ 2021-04-11 20:57  晚雾  阅读(51)  评论(0)    收藏  举报