Spring笔记

1、Spring
1.1、Spring简介
  • Spring:春天 给软件行业带来了春天

  • 2002:首次推出了Spring框架的雏形,interface21框架。

  • Spring框架以interface21框架为基础经过重新设计,并不断丰富其内涵,于2004年3月24日发布了1.0正式版。

  • Rod Johnson,Spring Framework创始人,悉尼大学音乐学博士。

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

  • SSH:Struct2 + Spring + Hibernate

  • SSM:SpringMVC + Spring + Mybatis

官网:https://spring.io/projects/spring-framework#overview

官方下载地址: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、优点
  • Spring是一个开源的免费框架(容器)
  • Spring是一个轻量级的,非入侵式的框架
  • 控制反转(IOC),面向切面编程(AOP)
  • 支持事物的处理,对框架整合的支持

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

1.3、组成

在这里插入图片描述

1.4、拓展

在这里插入图片描述

  • Spring Boot

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

    • SpringCloud是基于SpringBoot实现的

因为现在大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要完全掌握Spring及SpringMVC。承上启下。

Spring弊端:违背了原来的理念,配置十分繁琐,人称“配置地狱!”。

2、IoC理论推导
  1. UserDao接口
  2. UserDaoImpl实现类
  3. UserService业务接口
  4. UserServiceImpl实现类

在我们之前的业务中,用户的需求可能会影响我们原来的代码,需要根据用户的需求去修改源代码。如果程序代码量十分大,修改一次的成本十分昂贵!

我们使用一个Set接口实现,已经发生了革命性的变化!

 private UserDao userDao;

 //利用set进行动态实现值得注入
 public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
 }
  • 之前,程序是主动创建对象,控制权在程序员手中
    在这里插入图片描述

  • 使用了set注入后,程序不再具有主动性,而是变成了被动的接受对象。
    在这里插入图片描述

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

  • IOC本质

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

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

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

3、Hello Spring
        //解析beans.xml文件,生成管理Bean的对象context
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean:参数即为Bean.xml文件中的bean的id
        UserServiceImpl userService = (UserServiceImpl) context.getBean("userService");
        userService.getUser();
<?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 = 类名
    property相当于给类中属性设置一个值
    -->
    <bean id="hello" class="com.wang.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

思考问题?

  • Hello对象是谁创建的?

    hello对象是由Spring创建的。

  • Hello对象的属性是怎么设置的?

    hello对象的属性是由Spring容器设置的。

    这个过程就叫控制反转。

    控制:谁来控制对象的创建,传统应用程序中的对象是由程序本身控制创建的,使用Spring后,对象由Spring来创建。

    反转:程序本身不创建对象,而变成被动的接收对象。

    依赖注入:利用set方法来进行注入

    IOC是一种编程思想,由主动地编程变成被动的接收。

要实现不同的操作,只需要在xml配置文件中进行改动,所谓的IOC可以简单概括为对象由Spring来创建,管理和装配。

4、IOC创建对象的方式
  1. 使用无参构造创建对象,默认。

  2. 假设我们要使用有参构造创建对象,可以有以下的几种方式:

    1. 下表赋值
    <!--第一种,下标赋值-->
        <bean id="user" class="com.wang.pojo.User">
            <constructor-arg index="0" value="wangshu"/>
        </bean>
    
    1. 参数类型赋值
    <!--第二种,参数类型赋值,不建议使用-->
        <bean id="user" class="com.wang.pojo.User">
            <constructor-arg type="java.lang.String" value="wangshu2"/>
        </bean>
    
    1. 参数名
    <!--第三种,直接通过参数名-->
        <bean id="user" class="com.wang.pojo.User">
            <constructor-arg name="name" value="wangshu3"/>
        </bean>
    

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

5、Spring配置
5.1、别名
<!--第三种,直接通过参数名-->
    <bean id="user" class="com.wang.pojo.User">
        <constructor-arg name="name" value="wangshu3"/>
    </bean>
    
    <alias name="user" alias="userNew"/>
5.2、Bean配置

在这里插入图片描述

5.3、import

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

假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以用import将所有人的beans.xml合并成一个总的.xml文件。
在这里插入图片描述

6、DI(依赖注入)
6.1、构造器注入

前面已经说过

6.2、Set方式注入【重点】
  • 依赖注入:Set注入!

    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入。

    【环境搭建】

    1. 复杂类型
    public class Address {
        private String addr;
    
        public String getAddr() {
            return addr;
        }
    
        public void setAddr(String addr) {
            this.addr = addr;
        }
    
        @Override
        public String toString() {
            return "Address{" +
                    "addr='" + addr + '\'' +
                    '}';
        }
    }
    
    1. 真实测试对象
    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;
    }
    
    1. Beans.xml
        <bean id="address" class="com.wang.pojo.Address">
            <property name="addr" value="安徽省安庆市"/>
        </bean>
    
        <bean id="student" class="com.wang.pojo.Student">
            <!--第一种,普通值注入-->
            <property name="name" value="汪澍"/>
            <!--第二种,引用类型注入,使用ref-->
            <property name="address" ref="address"/>
            <!--数组的注入-->
            <property name="books">
                <array>
                    <value>我的阿勒泰</value>
                    <value>冬牧场</value>
                    <value>走夜路时请放声歌唱</value>
                    <value>九篇雪</value>
                    <value>阿勒泰的角落</value>
                </array>
            </property>
            <!--集合List注入-->
            <property name="hobbies">
                <list>
                    <value>阅读</value>
                    <value>运动</value>
                    <value>看电影</value>
                </list>
            </property>
    
            <!--Map注入-->
            <property name="card">
                <map>
                    <entry key="身份证" value="0902"/>
                    <entry key="学生证" value="0316"/>
                </map>
            </property>
    
            <!--Set注入-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>CS</value>
                </set>
            </property>
    
            <!--Empty及Null注入-->
            <property name="wife">
                <null/>
            </property>
    
            <!--Propeties注入 Key=Value-->
            <property name="info">
                <props>
                    <prop key="driver">com.mysql.jdbc.Driver</prop>
                    <prop key="url">jdbc:mysql://localhost:3306/db14?characterEncoding=UTF-8</prop>
                    <prop key="uesrname">root</prop>
                    <prop key="password">123456</prop>
                </props>
            </property>
        </bean>
    
    1. 测试类
    System.out.println(student);
            /*
            Student{
                    name='汪澍', 
                    address=Address{addr='安徽省安庆市'}, 
                    books=[我的阿勒泰, 冬牧场, 走夜路时请放声歌唱, 九篇雪, 阿勒泰的角落], 
                    hobbies=[阅读, 运动, 看电影], 
                    card={
                            身份证=0902, 
                            学生证=0316
                    }, 
                    games=[LOL, CS], 
                    wife='null', 
                    info={
                            uesrname=root, 
                            password=123456, 
                            url=jdbc:mysql://localhost:3306/db14?characterEncoding=UTF-8, 
                            driver=com.mysql.jdbc.Driver
                    }
            }
             */
    
6.3、拓展方式注入

我们可以使用p命名空间和c命名空间进行注入。

  1. 官方解释
<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="someone@somewhere.com"/>
    </bean>

    <bean name="p-namespace" class="com.example.ExampleBean"
        p:email="someone@somewhere.com"/>
</beans>

在这里插入图片描述

  1. xml配置
<!--p命名空间注入 Properties直接注入属性的值-->
    <bean id="user" class="com.wang.pojo.User" p:age="18" p:name="xxx" p:address-ref="address"/>
    <!--C命名空间注入 Construct-Args通过构造器注入-->
    <bean id="user1" class="com.wang.pojo.User" c:age="19" c:name="xxx" c:adress-ref="address"/>

测试:

@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("user.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }

注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束!

6.4、bean的作用域

在这里插入图片描述

  1. 单例模式(Spring默认机制)
<bean id="address" class="com.wang.pojo.Address" scope="singleton">
    <property name="addr" value="安徽省安庆市"/>
</bean>
  1. 原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
  1. 其余的request、session、application这些只能在Web开发中使用到。
7、Bean的自动装配
  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会在上下文自动寻找,并自动给bean装配属性。

在Spring中有三种装配的方式

  1. 在xml中显示的配置
  2. 在java中显示配置
  3. 隐式的自动装配bean【重要】
7.1、测试
  1. 环境搭建:一个人有两个宠物
7.2、byName自动装配
<!--
    byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
    -->
    <bean id="person" class="com.wang.pojo.Person" autowire="byName">
        <property name="name" value="汪澍"/>
    </bean>
7.3、byType自动装配
<--byType:会自动在容器上下文中查找,和自己对象属性相同的bean!
    -->
    <bean id="person" class="com.wang.pojo.Person" autowire="byType">
        <property name="name" value="汪澍"/>
    </bean>

小结:

  • byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法后的值一致。
  • byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
7.4、 使用注解实现自动装配

jdk1.5支持注解,spring2.5支持注解

要使用注解须知:

  1. 导入约束: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。

科普:

@Nullable 字段标记了这个注解,说明这个字段可以为null
public @interface Autowired{
  boolean required() default true;
}

测试代码

public class Person{
  //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
  @Autowired(required = false)
  private Cat cat;
  @Autowired
  private Dog dog;
  private String name;
}

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

public class Person{
  //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
  @Autowired
  @Qualfier(value="cat01")
  private Cat cat;
  @Autowired
  private Dog dog;
  private String name;
}

@Resource注解

在这里插入图片描述

小结:@Resource和@Autowired的区别

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired通过byType的方式实现,并且必须要求这个对象存在!【常用】
  • @Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错。【常用】
  • 执行顺序不同:@Autowired通过byType的方式实现。
8、使用注解开发

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

在这里插入图片描述

使用注解需要导入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>
  1. bean

@Component

  1. 属性如何注入
//等价于<bean id="user" class="com.wang.dao.User"/>
//@Component组件
@Component
public class User {
    /*等价于<property name="name" value="汪澍"/>
    @Value("汪澍")*/
    public String name;

    //等价于<property name="name" value="汪澍"/>
    @Value("汪澍")
    public void setName(String name) {
        this.name = name;
    }
}
  1. 衍生的注解

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

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

    这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean。

  2. 自动装配

  • @Autowired:自动装配通过类型,名字
    • 如果Autowired不能唯一装配上属性,则需要通过@Qulifier(Value=“xxx”)来进行配合
  • @Nullable:字段标记了这个注解,说明这个字段可以为null
  • @Resource:自动装配通过名字,类型。
  1. 作用域

    @Scope(’’’’)

@Component
@Scope("prototype")
public class User {}
  1. 小结

    xml与注解:

    • xml更加万能,适用于任何场合!维护更加简单方便
    • 注解不是自己的类使用不了,维护相对复杂

    xml与注解最佳实践:

    • xml用来管理bean
    • 注解只负责完成属性的注入
    • 我们在使用过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持。
    <!--指定要扫描的包,这个包下的注解就会生效-->
        <context:component-scan base-package="com.wang"/>
        <context:annotation-config/>
    
9、使用Java的方式配置Spring

完全不使用Spring的xml配置了,全权交给Java来做!

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

  1. 准备
//如果使用了Spring配置类方式进行开发,就只能通过AnnotationConfig上下文获取容器,通过配置类的class对象加载!
        ApplicationContext context = new AnnotationConfigApplicationContext("MyConfig.class");
        User user = (User) context.getBean("getuser");
        System.out.println(user.getName());

在这里插入图片描述

  1. 使用

    1. 实体类

      //这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中。
      @Component
      public class User {
          //属性注入
          @Value("汪澍")
          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. 配置类

      /*
          这个也会被Spring容器托管,注册到容器中,因为它本来就是一个@Component
          @Configuration代表它是一个配置类,就和之前的xxx.xml一样
       */
      @Configuration
      @ComponentScan("com.wang")
      @Import(MyConfig2.class)//引入另一个配置类
      public class MyConfig {
          /*
              注册一个Bean,相当于之前在xml文件中的一个bean标签;
              这个方法的名称就相当于之前xml文件中bean标签的id属性
              这个方法返回值,就相当于bean标签中的class属性。
           */
          @Bean
          public User getUser(){
              return new User();//这就是返回要注入到bean的对象!
          }
      }
      
    3. 测试类

      public class MyTest {
          public static void main(String[] args) {
              //如果使用了Spring配置类方式进行开发,就只能通过AnnotationConfig上下文获取容器,通过配置类的class对象加载!
              ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
              User user = (User) context.getBean("getUser");
              System.out.println(user.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() {
            seeHouse();
            host.rent();
        }
    
        public void seeHouse(){
            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

需要了解两个类:

Proxy类;InvocationHandler接口

动态代理的好处:

  • 可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
  • 公共业务就就交给代理角色,实现业务的分工
  • 公共业务发生扩展的时候,方便集中管理
  • 一个动态代理类代理的是一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要是实现了同一个接口即可。
11、AOP
11.1 什么是AOP?

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

在这里插入图片描述

11.2、Aop在Spring中的作用

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

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

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

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

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

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

  • 切入点(PointCut):切面通知执行的“地点”的定义

  • 连接点(JointPoint):与切入点匹配的执行点。
    在这里插入图片描述

在这里插入图片描述

11.3、使用Spring实现Aop

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

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

方式一:使用Spring的API接口【使用Spring接口实现】

<!--方式一:使用原生Spring API接口-->
    <!--配置AOP:需要导入AOP的约束-->
    <aop:config>
        <!--切入点:expression:表达式 execution(要执行的位置!* * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

方式二:自定义类实现AOP【主要是切面定义】

<!--方式二:使用自定义类实现AOP-->
    <bean id="diyPointCut" class="com.wang.diy.DiyPointCut"/>

    <aop:config>
        <!--自定义切面,ref要引用的类-->
        <aop:aspect ref="diyPointCut">
            <!--切入点:expression:表达式 execution(要执行的位置!* * * * *)-->
            <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

方式三:使用注解实现

//使用注解方式实现AOP
@Aspect //标注这个类为切面
public class AnnotationPointCut {
    @Before("execution(* com.wang.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("==方法执行前==");
    }
    @After("execution(* com.wang.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("==方法执行后==");
    }

    @Around("execution(* com.wang.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        Signature signature = jp.getSignature();
        System.out.println(signature);
        System.out.println("环绕前");
        Object proceed = jp.proceed();
        System.out.println("环绕后");
    }
}

【注意】

<!--方式三:注解实现AOP-->
    <bean id="annotationPointCut" class="com.wang.diy.AnnotationPointCut"/>
    <!--开启注解支持 默认JDk实现(proxy-target-class="false")也可以使用cglib来实现(proxy-target-class="true")-->
    <aop:aspectj-autoproxy proxy-target-class="false"/>
12、整合Mybatis

步骤

  1. 导入相关jar包
    • junit
    • mybatis
    • mysql数据库
    • Spring相关
    • aop织入
    • mybatis-spring
  2. 编写配置文件
  3. 测试
12.1、回忆Mybatis
  1. 编写实体类
  2. 编写核心配置文件
  3. 编写接口
  4. 编写Mapper.xml
  5. 测试
12.2 Mybatis-Spring
  1. 编写数据源配置
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 需要给接口增加实现类
  5. 将自己写的实现类,注入到Spring中。
  6. 测试使用即可。
13、声明式事务
1、回顾事务
  • 要么都成功,要么都失败
  • 事务在项目开发中,十分重要,涉及到数据的一致性问题。
  • 确保完整性和一致性。

事务ACID原则:

  • 原子性
  • 一致性
  • 隔离性
    • 多个业务操作同一个资源,防止数据损坏
  • 持久性
    • 事物一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久化的写到存储器中。
2、Spring中的事务管理
  • 声明式事务:AOP
  • 编程式事务:需要在代码中,进行事务的管理。

思考:

为什么需要事务:

  • 如果不配置事务,可能存在数据提交不一致的情况
  • 如果不在Spring中配置声明式事务,就需要在代码中手动配置事务。
  • 事务在项目的开发中十分重要,涉及到数据的一致性和完整性问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值