欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring框架學(xué)習(xí)總結(jié)

 更新時間:2021年06月23日 08:55:17   作者:我實在是想不出什么好聽的昵稱了啊  
Spring是于2003 年興起的一個輕量級的Java 開發(fā)框架,由Rod Johnson創(chuàng)建。簡單來說,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源框架

Spring 框架概述

  • Spring 使創(chuàng)建 Java 企業(yè)應(yīng)用程序變得容易。它提供了在企業(yè)環(huán)境中使用 Java 語言所需的一切,并支持 Groovy 和 Kotlin 作為 JVM 上的替代語言,并且可以根據(jù)應(yīng)用程序的需求靈活地創(chuàng)建多種體系結(jié)構(gòu)。從 Spring Framework 5.0 開始,Spring 需要 JDK 8(Java SE 8),并且已經(jīng)為 JDK 9 提供了現(xiàn)成的支持。
  • Spring 是分層的 Java SE/EE full-stack 輕量級開源框架,以 IoC(Inverse of Control,控制反轉(zhuǎn))和 AOP(Aspect Oriented Programming,面向切面編程)為內(nèi)核,使用基本的 JavaBean 完成以前只可能由 EJB 完成的工作,取代了 EJB 臃腫和低效的開發(fā)模式。
  • Spring 是開源的。它擁有一個龐大而活躍的社區(qū),可以根據(jù)各種實際用例提供持續(xù)的反饋。這幫助 Spring 在很長一段時間內(nèi)成功地 Developing 了。

Spring優(yōu)點

  • 方便解耦,簡化開發(fā)
  • Spring 就是一個大工廠,可以將所有對象的創(chuàng)建和依賴關(guān)系的維護(hù)交給 Spring 管理。
  • 方便集成各大優(yōu)秀框架
  • Spring 不排斥各種優(yōu)秀的開源框架,其內(nèi)部提供了對各種優(yōu)秀框架(如 Struts2、Hibernate、MyBatis 等)的直接支持。
  • 方便程序的測試
  • Spring 支持 JUnit4,可以通過注解方便地測試 Spring 程序。
  • AOP 編程的支持
  • Spring 提供面向切面編程,可以方便地實現(xiàn)對程序進(jìn)行權(quán)限攔截和運行監(jiān)控等功能。
  • 聲明式事務(wù)的支持
  • 只需要通過配置就可以完成對事務(wù)的管理,而無須手動編程。

Spring體系結(jié)構(gòu)

Spring 框架采用分層架構(gòu),根據(jù)不同的功能被劃分成了多個模塊,這些模塊大體可分為 Data Access/Integration、Web、AOP、Aspects、Messaging、Instrumentation、Core Container 和 Test,具體如下圖所示:

在這里插入圖片描述

Data Access/Integration(數(shù)據(jù)訪問/集成)

數(shù)據(jù)訪問/集成層包括 JDBC、ORM、OXM、JMS 和 Transactions 模塊,具體介紹如下。

JDBC 模塊:提供了一個 JDBC 的抽象層,大幅度減少了在開發(fā)過程中對數(shù)據(jù)庫操作的編碼。 ORM 模塊:對流行的對象關(guān)系映射 API,包括 JPA、JDO、Hibernate 和 iBatis 提供了的集成層。 OXM 模塊:提供了一個支持對象/XML 映射的抽象層實現(xiàn),如 JAXB、Castor、XMLBeans、JiBX 和 XStream。 JMS 模塊:指 Java 消息服務(wù),包含的功能為生產(chǎn)和消費的信息。 Transactions 事務(wù)模塊:支持編程和聲明式事務(wù)管理實現(xiàn)特殊接口類,并為所有的 POJO。

Web 模塊

Spring 的 Web 層包括 Web、Servlet、Struts 和 Portlet 組件,具體介紹如下。

Web 模塊:提供了基本的 Web 開發(fā)集成特性,例如多文件上傳功能、使用的 Servlet 監(jiān)聽器的 IoC 容器初始化以及 Web 應(yīng)用上下文。 Servlet模塊:包括 Spring 模型—視圖—控制器(MVC)實現(xiàn) Web 應(yīng)用程序。 Struts 模塊:包含支持類內(nèi)的 Spring 應(yīng)用程序,集成了經(jīng)典的 Struts Web 層。 Portlet 模塊:提供了在 Portlet 環(huán)境中使用 MV C實現(xiàn),類似 Web-Servlet 模塊的功能。

Core Container(核心容器)

Spring 的核心容器是其他模塊建立的基礎(chǔ),由 Beans 模塊、Core 核心模塊、Context 上下文模塊和 Expression Language 表達(dá)式語言模塊組成,具體介紹如下。

Beans 模塊:提供了 BeanFactory,是工廠模式的經(jīng)典實現(xiàn),Spring 將管理對象稱為 Bean。 Core 核心模塊:提供了 Spring 框架的基本組成部分,包括 IoC 和 DI 功能。 Context 上下文模塊:建立在核心和 Beans 模塊的基礎(chǔ)之上,它是訪問定義和配置任何對象的媒介。ApplicationContext 接口是上下文模塊的焦點。 Expression Language 模塊:是運行時查詢和操作對象圖的強(qiáng)大的表達(dá)式語言。

其他模塊

Spring的其他模塊還有 AOP、Aspects、Instrumentation 以及 Test 模塊,具體介紹如下。

AOP 模塊:提供了面向切面編程實現(xiàn),允許定義方法攔截器和切入點,將代碼按照功能進(jìn)行分離,以降低耦合性。 Aspects 模塊:提供與 AspectJ 的集成,是一個功能強(qiáng)大且成熟的面向切面編程(AOP)框架。 Instrumentation 模塊:提供了類工具的支持和類加載器的實現(xiàn),可以在特定的應(yīng)用服務(wù)器中使用。 Test 模塊:支持 Spring 組件,使用 JUnit 或 TestNG 框架的測試。

Spring拓展

Spring Boot與Spring Cloud

  • Spring Boot 是 Spring 的一套快速配置腳手架,可以基于Spring Boot 快速開發(fā)單個微服務(wù)。
  • Spring Cloud是基于Spring Boot實現(xiàn)的。
  • Spring Boot專注于快速、方便集成的單個微服務(wù)個體,Spring Cloud關(guān)注全局的服務(wù)治理框架。
  • Spring Boot使用了約束優(yōu)于配置的理念,很多集成方案已經(jīng)幫你選擇好了,能不配置就不配置 , Spring Cloud很大的一部分是基于Spring Boot來實現(xiàn),Spring Boot可以離開Spring Cloud獨立使用開發(fā)項目,但是Spring Cloud離不開Spring Boot,屬于依賴的關(guān)系。
  • SpringBoot在SpringClound中起到了承上啟下的作用,如果你要學(xué)習(xí)SpringCloud必須要學(xué)習(xí)SpringBoot。

Spring IoC 容器 (IoC 也稱為依賴項注入(DI),或DI是實現(xiàn)IoC的一種方法)

IoC容器概述

  • 控制反轉(zhuǎn)是一種通過描述(XML或注解)并通過第三方去生產(chǎn)或獲取特定對象的方式。在Spring中實現(xiàn)控制反轉(zhuǎn)的是IoC容器,其實現(xiàn)方法是依賴注入。
  • Spring容器在初始化時先讀取配置文件,根據(jù)配置文件或元數(shù)據(jù)創(chuàng)建與組織對象存入容器中,程序使用時再從Ioc容器中取出需要的對象。

在這里插入圖片描述

  • Spring 提供了兩種 IoC 容器,分別為 BeanFactory 和 ApplicationContext。

1.BeanFactory

beanFactory是一個Factory,用于管理bean的,有了一個Spring的beanFactory,我們就可以從spring中獲取注冊到其中的bean來使用。

2.ApplicationContext

ApplicationContext 是 BeanFactory 的子接口,也被稱為應(yīng)用上下文。該接口的全路徑為:

org.springframework.context.ApplicationContext,它不僅提供了 BeanFactory 的所有功能,還添加了對 i18n(國際化)、資源訪問、事件傳播等方面的良好支持。

ApplicationContext 接口有兩個常用的實現(xiàn)類:ClassPathXmlApplicationContext和FileSystemXmlApplicationContext。 ClassPathXmlApplicationContext從類路徑 ClassPath 中尋找指定的 XML 配置文件,找到并裝載完成 ApplicationContext 的實例化工作,具體如下所示。ApplicationContext applicationContext = new ClassPathXmlApplicationContext(String configLocation);configLocation 參數(shù)用于指定 Spring 配置文件的名稱和位置,如 applicationContext.xml。

FileSystemXmlApplicationContext從指定的文件系統(tǒng)路徑中尋找指定的 XML 配置文件,找到并裝載完成 ApplicationContext 的實例化工作,具體如下所示。ApplicationContext applicationContext = new FileSystemXmlApplicationContext(String configLocation);它與 ClassPathXmlApplicationContext 的區(qū)別是:在讀取 Spring 的配置文件時,F(xiàn)ileSystemXmlApplicationContext 不再從類路徑中讀取配置文件,而是通過參數(shù)指定配置文件的位置,它可以獲取類路徑之外的資源,如“D:/workspaces/applicationContext.xml”。

3.BeanFactory 和 ApplicationContext區(qū)別:

BeanFactory在初始化容器時,并未實例化Bean,直到第一次訪問某個Bean 時才實例目標(biāo)Bean;而ApplicationContext 則在初始化應(yīng)用上下文時就實例化所有單實例的Bean 。

在實際開發(fā)中,通常都選擇使用 ApplicationContext,而只有在系統(tǒng)資源較少時,才考慮使用 BeanFactory。(但是,它們都是通過 XML 配置文件加載 Bean 的。)

Spring入門程序

1.創(chuàng)建maven項目

2.在pom.xml導(dǎo)入jar包依賴

    <dependencies>
        <!--導(dǎo)入spring,maven依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <!--導(dǎo)入junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

3.編寫接口

package com.xxx.mapper;
/**
 * @author shkstart
 * @create 2021-06-11 15:50
 */
public interface UserMapper {
    public void hello();
}

4.編寫接口實現(xiàn)類

package com.xxx.mapper;/**
 * @author shkstart
 * @create 2021-06-11 15:50
 */
/**
 *@program: springTest
 *@description:
 *@author: XieXianXin
 *@create: 2021-06-11 15:50
 */
public class UserMapperImpl implements UserMapper{
    @Override
    public void hello() {
        System.out.println("Spring入門程序!");
    }
}

編寫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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    使用Spring來創(chuàng)建對象,在Spring這些都稱為Bean
    類型 變量名 = new 類型();
    Hello hello = new Hello();
    id = 變量名
    class = new 的對象
    -->
    <beans>
        <bean id="hello" class="com.xxx.mapper.UserMapperImpl">
        </bean>
    </beans>
</beans>

測試

package com.xxx.mapper;/**
 * @author shkstart
 * @create 2021-06-11 15:57
 */
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 *@program: springTest
 *@description:
 *@author: XieXianXin
 *@create: 2021-06-11 15:57
 */
public class helloTest {
    @Test
    public void helloTest1(){
        // 1. 初始化Spring容器,加載配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 通過容器獲取userMapper實例
        UserMapper hello = context.getBean("hello", UserMapper.class);
        // 3.調(diào)用實例中的hello()方法
        hello.hello();

    }
}

測試結(jié)果

在這里插入圖片描述

IoC創(chuàng)建對象的三種方式

通過無參構(gòu)造(要提供set方法)

編寫實體類User:

public class User {
    private String name;
    // set方法
    public void setName(String name) {
        this.name=name;
    }
    public User() {
        System.out.println("無參構(gòu)造方法執(zhí)行了!");
    }
    public void print(){
        System.out.println("學(xué)生名字為:"+name);
    }
}

編寫Spring核心配置文件:

 

 <!--有參構(gòu)造,但是要有g(shù)et方法-->
        <bean id="user" class="com.xxx.pojo.User">
            <constructor-arg value="小新2" index="0"/>
        </bean>

測試以及結(jié)果:

 @Test
    public void helloTest2(){
        // 1. 初始化Spring容器,加載配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 通過容器獲取userMapper實例
        User user = context.getBean("user", User.class);
        // 3.調(diào)用實例中的print()方法
        user.print();
    }

在這里插入圖片描述

通過有參構(gòu)造(要提供get方法)

編寫實體類User:

public class User {
    private String name;
    //get方法
    public String getName() {
        return name;
    }
    public User(String name) {
        System.out.println("有參構(gòu)造方法執(zhí)行了!");
        this.name = name;
    }
    public void print(){
        System.out.println("學(xué)生名字為:"+name);
    }
}

編寫Spring核心配置文件:

 <!--有參構(gòu)造,但是要有g(shù)et方法-->
        <bean id="user" class="com.xxx.pojo.User">
            <constructor-arg value="小新2" index="0"/>
        </bean>

測試以及結(jié)果:

  @Test
    public void helloTest2(){
        // 1. 初始化Spring容器,加載配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 通過容器獲取userMapper實例
        User user = context.getBean("user", User.class);
        // 3.調(diào)用實例中的print()方法
        user.print();
    }

在這里插入圖片描述

拓展:Spring核心配置文件有三種寫法:

<!--有參構(gòu)造,但是要有g(shù)et方法-->
        <bean id="user" class="com.xxx.pojo.User">
            <constructor-arg index="0" value="小新-index屬性(0開始,按順序)"/>
            <constructor-arg name="name" value="小新-name屬性"/>
            <constructor-arg type="java.lang.String" value="小新-參數(shù)類型"/>
        </bean>

結(jié)果展示:

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

通過工廠類

編寫工廠類:

public class Factory {
    //方法一,靜態(tài)方法
    public static User getStaticInstance(){
        return new User("小新2——靜態(tài)方法創(chuàng)建對象");
    }
    //方法二,實例方法
    public User getInstance(){
        return new User("小新3-實例方法創(chuàng)建對象");
    }
}

編寫Spring核心配置文件:

<!--工廠類創(chuàng)建對象-->
        <!--創(chuàng)建工廠-->
        <bean id="factory" class="com.xxx.mapper.Factory"/>
        <!--靜態(tài)方法對象-->
        <bean id="staticFactory-user" class="com.xxx.mapper.Factory" factory-method="getStaticInstance"/>
        <!--實例方法對象-->
        <bean id="factory-user" factory-bean="factory" factory-method="getInstance"/>

測試以及結(jié)果:靜態(tài)方法:

 @Test
    public void helloTest4(){
        // 1. 初始化Spring容器,加載配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 通過容器獲取userMapper實例
        User user = context.getBean("staticFactory-user", User.class);
        // 3.調(diào)用實例中的print()方法
        user.print();
    }

在這里插入圖片描述

實例方法:

@Test
    public void helloTest3(){
        // 1. 初始化Spring容器,加載配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 通過容器獲取userMapper實例
        User user = context.getBean("factory-user", User.class);
        // 3.調(diào)用實例中的print()方法
        user.print();
    }

在這里插入圖片描述

Spring依賴注入(DI)和Bean的作用域

什么是依賴注入:Spring 容器在創(chuàng)建被調(diào)用者的實例時,會自動將調(diào)用者需要的對象實例注入給調(diào)用者,這樣,調(diào)用者通過 Spring 容器獲得被調(diào)用者實例。

依賴注入主要有兩種實現(xiàn)方式,分別是屬性 setter 注入和構(gòu)造方法注入,其中setter注入要求重點掌握。

  • 屬性 setter 注入(重點展開講解)
  • 指 IoC 容器使用 setter 方法注入被依賴的實例。通過調(diào)用無參構(gòu)造器或無參 static 工廠方法實例化 bean 后,調(diào)用該 bean 的 setter 方法,即可實現(xiàn)基于 setter 的 DI。
  • 構(gòu)造方法注入
  • 指 IoC 容器使用構(gòu)造方法注入被依賴的實例。基于構(gòu)造器的 DI 通過調(diào)用帶參數(shù)的構(gòu)造方法實現(xiàn),每個參數(shù)代表一個依賴。

屬性 setter 注入講解:

環(huán)境搭建:(創(chuàng)建一個Student和Book類):

Student

package com.xxx.pojo;/**
 * @author shkstart
 * @create 2021-06-11 17:45
 */
import java.util.*;
/**
 *@program: Spring_study
 *@description:
 *@author: XieXianXin
 *@create: 2021-06-11 17:45
 */
public class Student {
    private String name;
    private Book book;
    private String[] course;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> fruit;
    private String marriage;
    private Properties info;
    public Student() {
    }
    public Student(String name, Book book, String[] course, List<String> hobbies, Map<String, String> card, Set<String> fruit, String marriage, Properties info) {
        this.name = name;
        this.book = book;
        this.course = course;
        this.hobbies = hobbies;
        this.card = card;
        this.fruit = fruit;
        this.marriage = marriage;
        this.info = info;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", book=" + book +
                ", course=" + Arrays.toString(course) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", fruit=" + fruit +
                ", marriage='" + marriage + '\'' +
                ", info=" + info +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public String[] getCourse() {
        return course;
    }
    public void setCourse(String[] course) {
        this.course = course;
    }
    public List<String> getHobbies() {
        return hobbies;
    }
    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }
    public Map<String, String> getCard() {
        return card;
    }
    public void setCard(Map<String, String> card) {
        this.card = card;
    }
    public Set<String> getFruit() {
        return fruit;
    }
    public void setFruit(Set<String> fruit) {
        this.fruit = fruit;
    }
    public String getMarriage() {
        return marriage;
    }
    public void setMarriage(String marriage) {
        this.marriage = marriage;
    }
    public Properties getInfo() {
        return info;
    }
    public void setInfo(Properties info) {
        this.info = info;
    }
}

Book

package com.xxx.pojo;/**
 * @author shkstart
 * @create 2021-06-11 17:45
 */
/**
 *@program: Spring_study
 *@description:
 *@author: XieXianXin
 *@create: 2021-06-11 17:45
 */
public class Book {
    private String name;
    private int id;
    public Book() {
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Book(String name, int id) {
        this.name = name;
        this.id = id;
    }
}

常量注入:

<bean class="com.xxx.pojo.Student" id="student">
            <!--常量注入-->
            <property name="name" value="小新"/>
</bean>

Bean注入:

<bean class="com.xxx.pojo.Book" id="book">
           <property name="name" value="Java放棄"/>
           <property name="id" value="100"/>
       </bean>
       <bean class="com.xxx.pojo.Student" id="student">
       	 <!--Bean注入-->
          <property name="book" ref="book"/>
   	 </bean>

數(shù)組注入:

<property name="course">
              <array>
                  <value>高數(shù)</value>
                  <value>計算機(jī)網(wǎng)絡(luò)</value>
                  <value>數(shù)據(jù)庫</value>
              </array>
           </property>

List注入:

<property name="hobbies">
               <list>
                   <value>唱</value>
                   <value>跳</value>
                   <value>Rap</value>
               </list>
           </property>

Map注入:

<property name="card">
               <map>
                   <entry key="銀行卡:" value="2501314"/>
                   <entry key="身份證:" value="1314520"/>
               </map>
           </property>

Set注入:

 <property name="fruit">
               <set>
                   <value>香蕉</value>
                   <value>蘋果</value>
                   <value>雪梨</value>
               </set>
           </property>

Null注入:

<property name="marriage">
               <null/>
           </property>

Properties注入:

<property name="info">
               <props>
                   <prop key="username">小新</prop>
                   <prop key="password">520</prop>
               </props>
           </property>

測試及結(jié)果展示:

public class BeanTest {
    @Test
    public void beanTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
    }
}

在這里插入圖片描述

Student{name=‘小新', book=Book{name=‘Java放棄', id=100}, course=[高數(shù), 計算機(jī)網(wǎng)絡(luò), 數(shù)據(jù)庫], hobbies=[唱, 跳, Rap], card={銀行卡:=2501314, 身份證:=1314520}, fruit=[香蕉, 蘋果, 雪梨], marriage=‘null', info={password=520, username=小新}}

Process finished with exit code 0

p命名空間(以Book類舉例)導(dǎo)入約束 xmlns:p=“http://www.springframework.org/schema/p”

<bean id="pBook" class="com.xxx.pojo.Book" p:name="Java懵懂" p:id="250"/>

測試及結(jié)果:

 @Test
    public void cpTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book pBook = context.getBean("pBook", Book.class);
        System.out.println(pBook);
    }

在這里插入圖片描述

c命名空間導(dǎo)入約束 xmlns:c=“http://www.springframework.org/schema/c”

<bean id="cBook" class="com.xxx.pojo.Book" c:id="520" c:name="Java入坑"/>

測試及結(jié)果:

  @Test
    public void cpTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book cBook = context.getBean("cBook", Book.class);
        System.out.println(cBook);
    }

在這里插入圖片描述

作用域種類

singleton(以Book舉例)單例模式,使用 singleton 定義的 Bean 在 Spring 容器中只有一個實例,這也是 Bean 默認(rèn)的作用域。

<bean class="com.xxx.pojo.Book" id="scopeBook" scope="singleton">
            <property name="id" value="1"/>
        </bean>
@Test
    public void scopeTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book1 = context.getBean("scopeBook", Book.class);
        Book book2 = context.getBean("scopeBook", Book.class);
        System.out.println(book1.hashCode());
        System.out.println(book2.hashCode());
        System.out.println(book1==book2);
    }
}

在這里插入圖片描述

prototype 原型模式,每次通過 Spring 容器獲取 prototype 定義的 Bean 時,容器都將創(chuàng)建一個新的 Bean 實例,即每次調(diào)用getBean()時,相當(dāng)于執(zhí)行了一次new XxxBean()。

<bean class="com.xxx.pojo.Book" id="scopeBook" scope="prototype">
            <property name="id" value="1"/>
        </bean>
@Test
    public void scopeTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book1 = context.getBean("scopeBook", Book.class);
        Book book2 = context.getBean("scopeBook", Book.class);
        System.out.println(book1.hashCode());
        System.out.println(book2.hashCode());
        System.out.println(book1==book2);
    }
}

在這里插入圖片描述

  • request

在一次 HTTP 請求中,容器會返回該 Bean 的同一個實例。而對不同的 HTTP 請求,會返回不同的實例,該作用域僅在當(dāng)前 HTTP Request 內(nèi)有效。

  • session

在同一個 HTTP Session 中,容器會返回該 Bean 的同一個實例。而對不同的 HTTP 請求,會返回不同的實例,該作用域僅在當(dāng)前 HTTP Session 內(nèi)有效。

  • global Session

在一個全局的 HTTP Session 中,容器會返回該 Bean 的同一個實例。該作用域僅在使用 portlet context 時有效。

Spring 常用配置及屬性

在這里插入圖片描述

Spring自動裝配

  • Bean 的裝配可以理解為依賴關(guān)系注入,Bean 的裝配方式也就是 Bean 的依賴注入方式。Spring 容器支持多種形式的 Bean的裝配方式,如基于 XML 的 Bean 裝配、基于Annotation 的 Bean 裝配和自動裝配等。之前的舉例是通過XML的Bean裝配的。接下來講解自動裝配。
  • 自動裝配就是指 Spring 容器可以自動裝配(autowire)相互協(xié)作的 Bean 之間的關(guān)聯(lián)關(guān)系,將一個 Bean 注入其他 Bean 的 Property 中。
  • Spring的自動裝配需要從兩個角度來實現(xiàn):

1.組件掃描(component scanning):spring會自動發(fā)現(xiàn)應(yīng)用上下文中所創(chuàng)建的bean;

2.自動裝配(autowiring):spring自動滿足bean之間的依賴,也就是我們說的IoC/DI;

autowire 的屬性和作用

在這里插入圖片描述

  • 環(huán)境搭建:(分別創(chuàng)建一個Student和Student2類,再創(chuàng)建一個Teacher類)
public class Student {
   public void study(){
       System.out.println("Student類的方法study執(zhí)行了");
   }
}
public class Student2 {
    public void study(){
        System.out.println("Student2類的方法study執(zhí)行了");
    }
}
public class Teacher {
    private Student student;
    private Student2 student2;
    private String teach;
    public Teacher() {
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "student=" + student +
                ", student2=" + student2 +
                ", teach='" + teach + '\'' +
                '}';
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public Student2 getStudent2() {
        return student2;
    }
    public void setStudent2(Student2 student2) {
        this.student2 = student2;
    }
    public String getTeach() {
        return teach;
    }
    public void setTeach(String teach) {
        this.teach = teach;
    }
    public Teacher(Student student, Student2 student2, String teach) {
        this.student = student;
        this.student2 = student2;
        this.teach = teach;
    }
}

配置Spring核心配置文件

使用autowire=“byName”:

   <bean class="com.xxx.pojo.Student" id="student"/>
 <bean class="com.xxx.pojo.Student" id="student"/>
    <bean class="com.xxx.pojo.Student2" id="student2"/>
    <bean class="com.xxx.pojo.Teacher" id="teacher" autowire="byName">
        <property name="teach" value="Java"/>
    </bean>

測試及結(jié)果:

public class BeanTest {
    @Test
    public void beanTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Teacher teacher = context.getBean("teacher", Teacher.class);
        teacher.getStudent().study();
        teacher.getStudent2().study();
    }
}

在這里插入圖片描述

若修改Student的bean id值不為student,如:<bean class="com.xxx.pojo.Student" id="s"/>則會報空指針異常java.lang.NullPointerException at BeanTest.beanTest(BeanTest.java:24)。因為按byName規(guī)則找不對應(yīng)set方法,真正的setStudent就沒執(zhí)行,對象就沒有初始化,所以調(diào)用時就會報空指針錯誤。

當(dāng)一個bean節(jié)點帶有 autowire byName的屬性時:

1.將查找其類中所有的set方法名,例如setStudent,獲得將set去掉并且首字母小寫的字符串,即student。

2.去spring容器中尋找是否有此字符串名稱id的對象,如果有,就取出注入;如果沒有,就報空指針異常。

Spring注解開發(fā)

環(huán)境搭建

1.在spring配置文件中引入context文件頭

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

開啟屬性注解支持!

<context:annotation-config/>

編寫一個 Student類

public class Student {
    private String name;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student() {
    }
    public Student(String name) {
        this.name = name;
    }
}

編寫Spring核心配置文件:

<bean class="com.xxx.pojo.Student" id="student">
       <property name="name" value="小新"/>
   </bean>

測試及結(jié)果:

@Test
    public void beanTest(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student",Student.class);
        System.out.println(student);

在這里插入圖片描述

使用@Configuration和@Bean給容器中注冊組件 編寫一個配置類

/**
 *@program: springTest
 *@description: 在類上添加@Configuration注解使得該類成為Spring配置類,通過@Bean注解將該類注入到IoC容器,此時配置類==配置文件
 *@author: XieXianXin
 *@create: 2021-06-12 23:06
 */
// 這個配置類也是一個組件
@Configuration// 告訴Spring這是一個配置類
public class AnnotationStudent {
    @Bean// @Bean注解是給IOC容器中注冊一個bean,id默認(rèn)是用方法名作為id
    public Student student(){
        return new Student("小新");
    }
}

測試及結(jié)果:

    @Test
    public void beanTest(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationStudent.class);
        Student bean = context.getBean(Student.class);
        //返回Student類在IoC容器中的id值
        String[] namesForType = context.getBeanNamesForType(Student.class);
        for (String s : namesForType) {
            System.out.println(s);
        }
        System.out.println(bean);
    }
}

在這里插入圖片描述

若在配置類中給@Bean設(shè)置一個value值,如@Bean("stu")則測試結(jié)果為:

在這里插入圖片描述

則我們在使用注解方式向Spring的IOC容器中注入JavaBean時,如果沒有在@Bean注解中明確指定bean的名稱,那么就會使用當(dāng)前方法的名稱來作為bean的名稱;如果在@Bean注解中明確指定了bean的名稱,那么就會使用@Bean注解中指定的名稱來作為bean的名稱。

使用@ComponentScan自動掃描組件并指定掃描規(guī)則

開啟注解掃描,并刪除之前配置文件中的bean

<context:component-scan base-package="com.xxx"/>

在原有環(huán)境下創(chuàng)建一個com.xxx.service包,并創(chuàng)建一個Teacher類,并在類上添加一個@Service注解,同時,之前的Student類上也添加一個@Component注解

@Service
public class Teacher {
   private Student student;
   public void teach(){
       System.out.println("教授的學(xué)生是"+student);
   }
    @Override
    public String toString() {
        return "Teacher{" +
                "student=" + student +
                '}';
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public Teacher(Student student) {
        this.student = student;
    }
    public Teacher() {
    }
}

測試及結(jié)果:

public class BeanTest {
        @Test
        public void beanTest() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            String[] beanDefinitionNames = context.getBeanDefinitionNames();
            for (String definitionName : beanDefinitionNames) {
                System.out.println(definitionName);
            }
        }
}

在這里插入圖片描述

以上可以看到:在配置注解掃描后,只要在com.xxx包下的所有子包中,加上了@Repository(Dao)、@Service(service)、@Controller、(web)@Component注解的類都會被掃描到,并自動注入到Spring容器中。(其實上面四個功能,目前為止是一樣的)

  • 使用注解配置XML包掃描

我們可以在配置類中(前面的AnnotationStudent)使用@ComponentScan注解配置包掃描,由此代替xml中的<context:component-scan base-package="com.xxx"/>。先注釋掉之前的xml方式的注解掃描,接著

@Configuration// 告訴Spring這是一個配置類@ComponentScan(value = "com.xxx")public class AnnotationStudent {    @Bean// @Bean注解是給IOC容器中注冊一個bean,id默認(rèn)是用方法名作為id    public Student student(){        return new Student("小新");    }}

測試結(jié)果跟之前一樣。因此,推薦以后都使用注解掃描就好了,Spring還是盡量用注解開發(fā),MyBatis中還是用xml配置文件。

  • ComponentScan方法使用

excludeFilters()不包含哪些包、includeFilters()包含哪些包,使用includeFilters時,需要在XML配置文件中先配置use-default-filters="false",即禁用默認(rèn)的掃描所有包過濾規(guī)則才能生效。另外,ComponentScan還是一個可重復(fù)注解的注解,因此可以在一個類上重復(fù)使用這個注解。

使用@Scope注解設(shè)置組件的作用域

通過在類中添加注解@scope注解設(shè)置作用域,如:

// 這個配置類也是一個組件
@Configuration// 告訴Spring這是一個配置類
public class AnnotationStudent {
    @Scope("prototype")
    @Bean// @Bean注解是給IOC容器中注冊一個bean,id默認(rèn)是用方法名作為id
    public Student student(){
        return new Student("小新");
    }
}
  • 結(jié)果:

如果為false。

  • @Scope注解中的取值如下所示:

在這里插入圖片描述

注解自動裝配組件(@Resource是JDK自帶的)

  • @Autowired

@Autowired注解可以對類成員變量、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作。@Autowired注解可以放在類、接口以及方法上。等價于<property name="屬性名" value=" 屬性值"/>@Autowired注解默認(rèn)是優(yōu)先按照類型去容器中找對應(yīng)的組件,即:context.getBean(類名.class);,如果找到多個相同類型的組件,那么是將屬性名稱作為組件的id,到IOC容器中進(jìn)行查找,即:context.getBean("組件的id");

  • @Qualifier

@Autowired是根據(jù)類型自動裝配的,加上@Qualifier則可以根據(jù)byName的方式自動裝配,且Qualifier不能單獨使用。

  • @Resource

是JDK自帶的注解 可以按名稱注入也可以按類型注入,默認(rèn)是按名稱注入,沒有顯式指定名稱時,在spring容器中匹配與需要注入的bean屬性名相同的bean,如果還不同,@Resource會找到一個主類型匹配而不是一個特定的命名bean。

懶加載@Lazy

懶加載就是Spring容器啟動的時候,先不創(chuàng)建對象,在第一次使用(獲?。゜ean的時候Xxx xxx = context.getBean(Xxx.class);再來創(chuàng)建對象,并進(jìn)行一些初始化。使用時,只需要在配置類的方法上加上@Lazy注解即可。

public class AnnotationStudent {
    @Lazy
    @Bean// @Bean注解是給IOC容器中注冊一個bean,id默認(rèn)是用方法名作為id
    public Student student(){
        System.out.println("在容器中添加對象!");
        return new Student("小新");
    }
}
public class BeanTest {
        @Test
        public void beanTest() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            System.out.println("容器創(chuàng)建完成!");
            Student student = context.getBean(Student.class);
            Student student1 = context.getBean(Student.class);
            System.out.println(student==student1);
        }
}

在這里插入圖片描述

  • 非懶加載模式(默認(rèn)情況):bean在Spring容器啟動的時候ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");就會被創(chuàng)建,并且還加載到Spring容器中去了。
@Configuration// 告訴Spring這是一個配置類
public class AnnotationStudent {
    @Bean// @Bean注解是給IOC容器中注冊一個bean,id默認(rèn)是用方法名作為id
    public Student student(){
        System.out.println("在容器中添加對象!");
        return new Student("小新");
    }
}
public class BeanTest {
        @Test
        public void beanTest() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            System.out.println("容器創(chuàng)建完成!");
        }
}

在這里插入圖片描述

使用@Import注解給容器中快速導(dǎo)入一個組件

注冊bean的方式通常有以下幾種:

1.包掃描+給組件標(biāo)注注解(@Controller、@Servcie、@Repository、@Component

2.@Bean注解

3.@Import注解(只作用在類上,可以在實際開發(fā)項目中導(dǎo)入別人的類并注冊到容器中,這是兩外兩種無法做到的)例如在AnnotationStudent配置類上導(dǎo)入Teacher類對應(yīng)的bean實例(id默認(rèn)是組件的全類名)

4.使用FactoryBean接口(支持泛式)向Spring容器中注冊bean

// 這個配置類也是一個組件
    @Configuration// 告訴Spring這是一個配置類
    @Import(Teacher.class)
    public class AnnotationStudent {
        @Bean// @Bean注解是給IOC容器中注冊一個bean,id默認(rèn)是用方法名作為id
        public Student student(){
            return new Student("小新");
        }
    }
public class BeanTest {
        @Test
        public void beanTest() {
           /* ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            */
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationStudent.class);
            String[] beanNamesForType = applicationContext.getBeanDefinitionNames();
            for (String s : beanNamesForType) {
                System.out.println(s);
            }
        }
}

在這里插入圖片描述

當(dāng)去除@Import后,輸出結(jié)果為:

在這里插入圖片描述

Bean生命周期

常意義上講的bean的生命周期,指的是bean從創(chuàng)建到初始化,經(jīng)過一系列的流程,最終銷毀的過程,如下圖所示。在Spring中,我們可以自己來指定bean的初始化和銷毀的方法@Bean(initMethod = "自定義的初始化方法名",destroyMethod = "自定義的銷毀方法名")。當(dāng)容器在bean進(jìn)行到當(dāng)前生命周期的階段時,會自動調(diào)用我們自定義的初始化和銷毀方法。

在這里插入圖片描述

自定義一個Life類:

public class Life {
    public Life(){
        System.out.println("Life構(gòu)造方法執(zhí)行了!");
    }
    public void init(){
        System.out.println("Life初始化方法執(zhí)行了!");
    }
    public void destroy(){
        System.out.println("Life銷毀方法執(zhí)行了!");
    }
}

配置類中注冊bean:

@Configuration// 告訴Spring這是一個配置類
    public class AnnotationStudent {
        @Bean(initMethod = "init",destroyMethod = "destroy")
        public Life life(){
            return new Life();
        }
    }

測試及結(jié)果:

public class BeanTest {
        @Test
        public void beanTest() {
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationStudent.class);
            System.out.println("容器創(chuàng)建完成!");
            Life bean = applicationContext.getBean(Life.class);
        }
}

在這里插入圖片描述

可以看到,對于單實例對象,先執(zhí)行構(gòu)造方法,再到初始化方法,而銷毀方法執(zhí)行需要顯式關(guān)閉容器時候才執(zhí)行applicationContext.close();

在這里插入圖片描述

因此,我們可以自定義初始化方法和銷毀方法處理配置數(shù)據(jù)源問題,在初始化的時候,會對很多的數(shù)據(jù)源的屬性進(jìn)行賦值操作;在銷毀的時候,我們需要對數(shù)據(jù)源的連接等信息進(jìn)行關(guān)閉和清理。

@Value注解為屬性賦值

在Student類中的name屬性上加上@Value注解,等價于配置文件中的<bean id="student" class="com.xxx.pojo.Student"> <property name="name" value="xiaoxin"/> </bean>里的<property name="name" value="xiaoxin"/>,外面的bean是@Component注解作用。

@Component
public class Student {
    @Value("xiaoxin")
    private String name;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student() {
    }
    public Student(String name) {
        this.name = name;
    }
}

測試及結(jié)果:

public class BeanTest {
        @Test
        public void beanTest() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Student student = context.getBean("student", Student.class);
            System.out.println(student);
        }
}

在這里插入圖片描述

使用@PropertySource加載配置文件

  • 原始xml方式:
  • 在resources包下創(chuàng)建一個applicationContext.properties配置文件,內(nèi)容為鍵值對形式:name=xiaoxin password=888888
  • 編寫一個Property類,用于測試:
public class Property {
    private String username;
    private Integer password;
    @Override
    public String toString() {
        return "Property{" +
                "username='" + username + '\'' +
                ", password=" + password +
                '}';
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getPassword() {
        return password;
    }
    public void setPassword(Integer password) {
        this.password = password;
    }
    public Property(String username, Integer password) {
        this.username = username;
        this.password = password;
    }
    public Property() {
    }
}

Spring核心配置文件內(nèi)容為:

    <context:annotation-config />
    <context:component-scan base-package="com.xxx"/>
    <context:property-placeholder location="applicationContext.properties"/>
    <bean class="com.xxx.pojo.Property" id="property">
        <property name="username" value="${name}"/>
        <property name="password" value="${password}"/>
    </bean>

測試及結(jié)果:

public class BeanTest {
        @Test
        public void beanTest() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Property property = context.getBean("property", Property.class);
            System.out.println(property.toString());
        }
}

在這里插入圖片描述

注解方式:

  • 保留原applicationContext.properties配置文件
  • 將Spring核心配置文件內(nèi)容刪除:只保留開啟注解:<context:annotation-config />
  • Property類完全使用注解代替:
@Configuration//表示該類是配置類,等價于核心配置文件
@ComponentScan(value = "com.xxx")//等價于<context:component-scan base-package="com.xxx"/>
@Component//注冊bean,默認(rèn)id為類名(首字母小寫),等價于<bean class="com.xxx.pojo.Property" id="property"></bean>
@PropertySource("classpath:applicationContext.properties")//等價于<context:property-placeholder location="applicationContext.properties"/>
public class Property {
    @Value("${name}")//等價于<property name="username" value="${name}"/>
    private String username;
    @Value("${password}")//等價于<property name="password" value="${password}"/>
    private Integer password;
    @Override
    public String toString() {
        return "Property{" +
                "username='" + username + '\'' +
                ", password=" + password +
                '}';
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getPassword() {
        return password;
    }
    public void setPassword(Integer password) {
        this.password = password;
    }
    public Property(String username, Integer password) {
        this.username = username;
        this.password = password;
    }
    public Property() {
    }
}

測試及結(jié)果:

public class BeanTest {
        @Test
        public void beanTest() {
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Property.class);
            Property bean = applicationContext.getBean(Property.class);
            System.out.println(bean.toString());
        }
}

在這里插入圖片描述

代理模式

代理模式:為其他對象提供一種代理以控制對這個對象的訪問。

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

靜態(tài)代理

案例:男孩相親,想找女孩結(jié)婚,于是男孩找媒婆進(jìn)行代理,媒婆代理介紹女孩同時,還要收取一定的介紹費。

接口類

/**
 *@program: springTest
 *@description: 相親接口
 *@author: XieXianXin
 *@create: 2021-06-13 20:36
 */
public interface Marry {
    //相親
    void marry();
}

女孩(目標(biāo)對象)

/**
 *@program: springTest
 *@description: 目標(biāo)對象
 *@author: XieXianXin
 *@create: 2021-06-13 20:32
 */
public class Girl {
    private String name;
    @Override
    public String toString() {
        return "Girl{" +
                "name='" + name + '\'' +
                '}';
    }
    public Girl(String name) {
        this.name = name;
    }
    public Girl() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

男孩(被代理對象)

/**
 *@program: springTest
 *@description: 被代理對象
 *@author: XieXianXin
 *@create: 2021-06-13 20:33
 */
public class Boy implements Marry {
    private Girl girl;
    public Boy(Girl girl) {
        this.girl = girl;
    }
    @Override
    public void marry() {
        System.out.println("想跟"+girl.getName()+"認(rèn)識!");
    }
}

媒婆(代理對象)

/**
 *@program: springTest
 *@description: 代理類
 *@author: XieXianXin
 *@create: 2021-06-13 20:33
 */
public class Proxy implements Marry {
    private Boy boy;
    public Proxy(Girl girl){
         boy = new Boy(girl);
    }
    @Override
    public void marry() {
        boy.marry();
    }
    public void earn(){
        System.out.println("媒婆收取介紹費");
    }
}

測試及結(jié)果

public class ProxyTest {
    @Test
    public void proxyTest(){
        Girl girl = new Girl();
        girl.setName("美女!");
        Proxy proxy = new Proxy(girl);
        proxy.marry();
        proxy.earn();
    }
}

在這里插入圖片描述

  • 靜態(tài)代理的好處:

可以使得我們的真實角色更加純粹 . 不再去關(guān)注一些公共的事情。

公共的業(yè)務(wù)由代理來完成 . 實現(xiàn)了業(yè)務(wù)的分工。

公共業(yè)務(wù)發(fā)生擴(kuò)展時變得更加集中和方便。

  • 靜態(tài)代理缺點:

冗余,由于代理對象要實現(xiàn)與目標(biāo)對象一致的接口,會產(chǎn)生過多的代理類。

不易維護(hù),一旦接口增加方法,目標(biāo)對象與代理對象都要進(jìn)行修改。

Spring AOP AOP

AOP

(Aspect Oriented Programming)意為:面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。

在這里插入圖片描述

總的來說,AOP是指在程序的運行期間動態(tài)地將某段代碼切入到指定方法、指定位置進(jìn)行運行的編程方式。AOP的底層是使用動態(tài)代理實現(xiàn)的。

AOP中相關(guān)概念

橫切關(guān)注點:跨越應(yīng)用程序多個模塊的方法或功能。即是,與我們業(yè)務(wù)邏輯無關(guān)的,但是我們需要關(guān)注的部分,就是橫切關(guān)注點。如日志 , 安全 , 緩存 , 事務(wù)等等 …

切面(ASPECT):橫切關(guān)注點 被模塊化 的特殊對象。即,它是一個類。

通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。

目標(biāo)(Target):被通知對象。

代理(Proxy):向目標(biāo)對象應(yīng)用通知之后創(chuàng)建的對象。

切入點(PointCut):切面通知 執(zhí)行的 “地點”的定義。

連接點(JointPoint):與切入點匹配的執(zhí)行點。

SpringAOP中支持5種類型的Advice

在這里插入圖片描述

Spring AOP的實現(xiàn)(3種)

  • 導(dǎo)入依賴

在原有的maven的pom.xml文件中加上AOP織入依賴包

<!--使用Spring實現(xiàn)Aop,使用AOP織入,需要導(dǎo)入一個依賴包!-->
      <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.4</version>
      </dependency>

通過 Spring API 實現(xiàn)

編寫業(yè)務(wù)接口及其實現(xiàn)類

/**
 * @author shkstart 第一種,有接口方式,通過 Spring API 實現(xiàn),要實現(xiàn)Uservice接口,具體看advice包
 *                  第二種,通過自定義類實現(xiàn),運用的是AOP定義,不需要實現(xiàn)接口,具體看diy包
 *                  第三種,使用注解實現(xiàn),具體看annotation包
 * @create 2021-06-04 16:12
 */
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
/**
 *@program: Spring_study
 *@description:
 *@author: XieXianXin
 *@create: 2021-06-04 16:14
 */
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 select() {
        System.out.println("查詢用戶");
    }
}

編寫增強(qiáng)類(分別有前置通知、后置通知和環(huán)繞通知)

/**
 *@program: Spring_study
 *@description: 前置通知,在方法前增強(qiáng),實現(xiàn)MethodBeforeAdvice接口
 *@author: XieXianXin
 *@create: 2021-06-04 16:21
 */
public class BeforeAdvice implements MethodBeforeAdvice {
    //method : 要執(zhí)行的目標(biāo)對象的方法
    //args : 被調(diào)用的方法的參數(shù)
    //target : 目標(biāo)對象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("前置通知的"+target.getClass().getName()+"的"+method.getName()+"方法被執(zhí)行了");
    }
}
/**
 *@program: Spring_study
 *@description: 后置通知,在方法后執(zhí)行,實現(xiàn)AfterReturningAdvice接口
 *@author: XieXianXin
 *@create: 2021-06-04 17:00
 */
public class AfterAdvice implements AfterReturningAdvice {
    //returnValue 返回值
    //method被調(diào)用的方法
    //args 被調(diào)用的方法的對象的參數(shù)
    //target 被調(diào)用的目標(biāo)對象
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置通知的"+target.getClass().getName()+"的"+method.getName()+"執(zhí)行了,返回值為:"+returnValue);
    }
}
/**
 *@program: Spring_study
 *@description: 環(huán)繞通知,在方法前后執(zhí)行,實現(xiàn)MethodInterceptor接口
 *@author: XieXianXin
 *@create: 2021-06-04 17:07
 */
public class InterceptAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        try {
            System.out.println("環(huán)繞通知"+invocation.getMethod().getName()+"——方法前執(zhí)行的");
            Method invocationMethod = (Method) invocation.proceed();
            System.out.println("環(huán)繞通知"+invocation.getMethod().getName()+"——方法后執(zhí)行的");
            return invocationMethod;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return invocation;
    }
}

配置Spring核心配置文件,實現(xiàn)AOP切入

    <!--第一種方式,通過接口實現(xiàn)-->
    <!--1.注冊bean-->

    <bean id="userService" class="com.xxx.service.UserServiceImpl"/>

    <bean id="beforeAdvice" class="com.xxx.advice.BeforeAdvice"/>
    <bean id="afterAdvice" class="com.xxx.advice.AfterAdvice"/>
    <bean id="interceptAdvice" class="com.xxx.advice.InterceptAdvice"/>
    <bean id="throwAdvice" class="com.xxx.advice.ThrowAdvice"/>

    <!--2.aop的配置-->
    <aop:config>
        <!--切入點 expression:表達(dá)式匹配要執(zhí)行的方法-->
        <aop:pointcut id="pointCut" expression="execution(* com.xxx.service.UserServiceImpl.*(..))"/>
        <!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法 . pointcut-ref切入點-->
        <!--前置通知-->
        <aop:advisor advice-ref="beforeAdvice" pointcut-ref="pointCut"/>
        <!--后置通知-->
        <aop:advisor advice-ref="afterAdvice" pointcut-ref="pointCut"/>
        <!--環(huán)繞通知-->
        <aop:advisor advice-ref="interceptAdvice" pointcut-ref="pointCut"/>
        <!--異常拋出通知-->
        <aop:advisor advice-ref="throwAdvice" pointcut-ref="pointCut"/>
    </aop:config>

測試及結(jié)果

public class UserServiceImplTest {
    @Test
    public void myTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //動態(tài)代理的是接口,不是實體類,因此不是UserServiceImpl.class
        UserService userService = context.getBean("userService", UserService.class);
        userService.delete();
        System.out.println("==============================");
        UserService userService1 = context.getBean("userService", UserService.class);
        userService1.add();
        System.out.println("==============================");
        UserService userService2 = context.getBean("userService", UserService.class);
        userService2.select();
        System.out.println("==============================");
        UserService userService3 = context.getBean("userService", UserService.class);
        userService3.update();
    }
}

在這里插入圖片描述

通過自定義類來實現(xiàn) 保留之前的業(yè)務(wù)類UserServiceImpl編寫自定義類DiyPointcut

/**
 *@program: Spring_study
 *@description: 自定義類實現(xiàn)AOP,一個類相當(dāng)于一個切面,類的方法相當(dāng)于通知
 *@author: XieXianXin
 *@create: 2021-06-04 21:16
 */
public class DiyPointcut {
    public void beforeAdvice(){
        System.out.println("前置通知");
    }
    public void afterAdvice(){
        System.out.println("后置通知");
    }
    public void interceptAdvice(ProceedingJoinPoint joinPoint){//環(huán)繞通知要有ProceedingJoinPoint joinPoint參數(shù)
        System.out.println("方法"+joinPoint.getSignature().getName()+"環(huán)繞通知前執(zhí)行的語句");
        Object[] args = joinPoint.getArgs();
        try {
            Object proceed = joinPoint.proceed(args);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("方法"+joinPoint.getSignature().getName()+"環(huán)繞通知后執(zhí)行的語句");
    }
}

配置Spring核心配置文件

    <!--第二種方式,通過自定義類實現(xiàn)-->
    <!--1.注冊bean-->
    <bean id="userService" class="com.xxx.service.UserServiceImpl"/>

    <bean id="diyPointcut" class="com.xxx.diy.DiyPointcut"/>

    <aop:config>
        <!--2.使用AOP標(biāo)簽-->
        <aop:aspect ref="diyPointcut">
            <!--3.切入點-->
            <aop:pointcut id="pointcut" expression="execution(* com.xxx.service.UserServiceImpl.*(..))"/>
            <!--4.通知-->
            <!--前置通知-->
            <aop:before method="beforeAdvice" pointcut-ref="pointcut"/>
            <!--后置通知-->
            <aop:after method="afterAdvice" pointcut-ref="pointcut"/>
            <!--環(huán)繞通知-->
            <aop:around method="interceptAdvice" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

測試及結(jié)果

public class UserServiceImplTest {
    @Test
    public void myTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //動態(tài)代理的是接口,不是實體類,因此不是UserServiceImpl.class
        UserService userService = context.getBean("userService", UserService.class);
        userService.delete();
        System.out.println("==============================");
        UserService userService1 = context.getBean("userService", UserService.class);
        userService1.add();
        System.out.println("==============================");
        UserService userService2 = context.getBean("userService", UserService.class);
        userService2.select();
        System.out.println("==============================");
        UserService userService3 = context.getBean("userService", UserService.class);
        userService3.update();
    }
}

在這里插入圖片描述

通過自定義類來實現(xiàn) 編寫注解實現(xiàn)的增強(qiáng)類AnnotationAdvice

/**
 *@program: Spring_study
 *@description: 使用注解進(jìn)行AOP設(shè)計
 *@author: XieXianXin
 *@create: 2021-06-04 22:18
 */
@Aspect
public class AnnotationAdvice {
    @Before("execution(* com.xxx.service.UserServiceImpl.*(..))")//表達(dá)式中寫要被增強(qiáng)的類
    public void before(){
        System.out.println("前置通知");
    }
    @After("execution(* com.xxx.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("后置通知");
    }
    @Around("execution(* com.xxx.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("環(huán)繞通知執(zhí)行前");
        System.out.println("簽名:"+joinPoint.getSignature());
        //執(zhí)行目標(biāo)方法proceed
        Object proceed = joinPoint.proceed();
        System.out.println("環(huán)繞通知執(zhí)行后");
        System.out.println(proceed);
    }
}

開啟注解掃描和注冊bean

<!--指定要掃描的包,這個包下的注解就會生效-->
    <context:component-scan base-package="com.xxx.service"/>
    <context:annotation-config/>
 <aop:aspectj-autoproxy proxy-target-class="false"/>

    <!--2.注冊bean,只需要注冊增強(qiáng)的那個類-->
    <bean class="com.xxx.service.UserServiceImpl" id="userService"/>
    <bean id="annotationAdvice" class="com.xxx.annotation.AnnotationAdvice"/>

測試及結(jié)果

public class UserServiceImplTest {
    @Test
    public void myTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //動態(tài)代理的是接口,不是實體類,因此不是UserServiceImpl.class
        UserService userService = context.getBean("userService", UserService.class);
        userService.delete();
        System.out.println("==============================");
        UserService userService1 = context.getBean("userService", UserService.class);
        userService1.add();
        System.out.println("==============================");
        UserService userService2 = context.getBean("userService", UserService.class);
        userService2.select();
        System.out.println("==============================");
        UserService userService3 = context.getBean("userService", UserService.class);
        userService3.update();
    }
}

在這里插入圖片描述

Spring事務(wù)管理及Spring整合MyBatis代碼示例

Spring事務(wù)管理

  • 什么是事務(wù):事務(wù)就是把一系列的動作當(dāng)成一個獨立的工作單元,這些動作要么都執(zhí)行,要么都不執(zhí)行。
  • 事務(wù)四個特性-ACID:

原子性(atomicity)

事務(wù)是原子性操作,由一系列動作組成,事務(wù)的原子性確保動作要么全部完成,要么完全不起作用

一致性(consistency)

一旦所有事務(wù)動作完成,事務(wù)就要被提交。數(shù)據(jù)和資源處于一種滿足業(yè)務(wù)規(guī)則的一致性狀態(tài)中

隔離性(isolation)

可能多個事務(wù)會同時處理相同的數(shù)據(jù),因此每個事務(wù)都應(yīng)該與其他事務(wù)隔離開來,防止數(shù)據(jù)損壞

持久性(durability)

事務(wù)一旦完成,無論系統(tǒng)發(fā)生什么錯誤,結(jié)果都不會受到影響。通常情況下,事務(wù)的結(jié)果被寫到持久化存儲器中Spring支持編程

  • 式事務(wù)管理和聲明式的事務(wù)管理:

聲明式事務(wù)管理

聲明式事務(wù)管理建立在AOP之上,其本質(zhì)是對方法前后進(jìn)行攔截,然后在目標(biāo)方法開始之前創(chuàng)建或者加入一個事務(wù),執(zhí)行完目標(biāo)方法之后根據(jù)執(zhí)行的情況提交或者回滾。

編程式事務(wù)每次實現(xiàn)都要單獨實現(xiàn),但業(yè)務(wù)量大功能復(fù)雜時,使用編程式事務(wù)無疑是痛苦的,而聲明式事務(wù)不同,聲明式事務(wù)屬于無侵入式,不會影響業(yè)務(wù)邏輯的實現(xiàn),只需要在配置文件中做相關(guān)的事務(wù)規(guī)則聲明或者通過注解的方式,便可以將事務(wù)規(guī)則應(yīng)用到業(yè)務(wù)邏輯中。

顯然聲明式事務(wù)管理要優(yōu)于編程式事務(wù)管理,這正是Spring倡導(dǎo)的非侵入式的編程方式。唯一不足的地方就是聲明式事務(wù)管理的粒度是方法級別,而編程式事務(wù)管理是可以到代碼塊的,但是可以通過提取方法的方式完成聲明式事務(wù)管理的配置。

使用Spring管理事務(wù),注意頭文件的約束導(dǎo)入:

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

聲明式事務(wù)配置拓展:

JDBC事務(wù)

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource" />
</bean>

自動代理的配置

!-- Spring事務(wù)管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事務(wù)的傳播特性 -->
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true" >
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributes">
    <props>
      <prop key="add*">PROPAGATION_REQUIRED</prop>
      <prop key="edit*">PROPAGATION_REQUIRED</prop>
      <prop key="remove*">PROPAGATION_REQUIRED</prop>
      <prop key="insert*">PROPAGATION_REQUIRED</prop>
      <prop key="update*">PROPAGATION_REQUIRED</prop>
      <prop key="del*">PROPAGATION_REQUIRED</prop>
      <prop key="*">readOnly</prop>
    </props>
  </property>
</bean>

基于 命名空間的聲明式事務(wù)管理

<beans......>
  ......
  <bean id="bankService" 
  class="footmark.spring.core.tx.declare.namespace.BankServiceImpl">
    <property name="bankDao" ref="bankDao"/>
  </bean>
  <tx:advice id="bankAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="transfer" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>
  <aop:config>
    <aop:pointcut id="bankPointcut" expression="execution(* *.transfer(..))"/>
    <aop:advisor advice-ref="bankAdvice" pointcut-ref="bankPointcut"/>
  </aop:config>
  ......
</beans>
  • @Transactional 的聲明式事務(wù)管理

啟用tx的annotation:

<tx:annotation-driven transaction-manager="transactionManager"/>

@Transactional 可以作用于接口、接口方法、類以及類方法上。當(dāng)作用于類上時,該類的所有 public 方法將都具有該類型的事務(wù)屬性,同時,我們也可以在方法級別使用該標(biāo)注來覆蓋類級別的定義。

編程式事務(wù)管理

編程式事務(wù)管理是侵入性事務(wù)管理,使用TransactionTemplate或者直接使用PlatformTransactionManager,對于編程式事務(wù)管理,Spring推薦使用TransactionTemplate。

  • Spring事務(wù)的傳播行為:

事務(wù)的第一個方面是傳播行為(propagation behavior)。當(dāng)事務(wù)方法被另一個事務(wù)方法調(diào)用時,必須指定事務(wù)應(yīng)該如何傳播。例如:方法可能繼續(xù)在現(xiàn)有事務(wù)中運行,也可能開啟一個新事務(wù),并在自己的事務(wù)中運行。Spring定義了七種傳播行為:

在這里插入圖片描述

  • 事務(wù)的隔離級別:

事務(wù)的第二個維度就是隔離級別(isolation level)。

臟讀(Dirty reads)——臟讀發(fā)生在一個事務(wù)讀取了另一個事務(wù)改寫但尚未提交的數(shù)據(jù)時。如果改寫在稍后被回滾了,那么第一個事務(wù)獲取的數(shù)據(jù)就是無效的。 不可重復(fù)讀(Nonrepeatable read)——不可重復(fù)讀發(fā)生在一個事務(wù)執(zhí)行相同的查詢兩次或兩次以上,但是每次都得到不同的數(shù)據(jù)時。這通常是因為另一個并發(fā)事務(wù)在兩次查詢期間進(jìn)行了更新。 幻讀(Phantom read)——幻讀與不可重復(fù)讀類似。它發(fā)生在一個事務(wù)(T1)讀取了幾行數(shù)據(jù),接著另一個并發(fā)事務(wù)(T2)插入了一些數(shù)據(jù)時。在隨后的查詢中,第一個事務(wù)(T1)就會發(fā)現(xiàn)多了一些原本不存在的記錄。

Spring結(jié)合事務(wù)整合MyBatis示例

1.導(dǎo)入相關(guān)Jar包

<!--Spring整合Mybatis需要如下包,都是放在dependencies內(nèi)-->
    <dependencies>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--mysql驅(qū)動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--導(dǎo)入spring,maven依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <!--使用Spring實現(xiàn)Aop,使用AOP織入-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <!--spring操作數(shù)據(jù)庫也需要一個spring-jdbc包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.7</version>
        </dependency>
        <!--整合必要的一個包,mybatis-spring,使用2.0以上版本-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>

        <!--LOG4J-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <!--需要解決的亂碼以及maven靜態(tài)資源過濾問題等在build內(nèi)完成-->
    <!--解決單元測試中文亂碼-->
    <build>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <argLine>
                        -Dfile.encoding=UTF-8
                    </argLine>
                </configuration>
            </plugin>
        </plugins>

        <!--可能出現(xiàn)問題說明:Maven靜態(tài)資源過濾(導(dǎo)出)問題
         Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
         Cause: java.io.IOException: Could not find resource com/xxx/dao/UserMapper.xml
         原因是idea默認(rèn)不編譯src目錄下的xml文件,所以加載不到
         解決辦法在pom文件中的build標(biāo)簽內(nèi)加入如下配置,則可以找到j(luò)ava和resources下的所有properties和xml文件了
     -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>

2.編寫配置文件及加入日志

mybatis-config.xml

<configuration>

    <!--
    configuration" 里的標(biāo)簽順序如下:(否則報錯如下信息)
     "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?
     objectWrapperFactory?,reflectorFactory?,plugins?,environments?,
     databaseIdProvider?,mappers?)".
    -->

    <!--標(biāo)準(zhǔn)的日志工廠實現(xiàn)(常用:STDOUT_LOGGING,LOG4J),下面的value值建議去mybaits文檔復(fù)制
        日志就是記錄程序的運行軌跡,方便查找關(guān)鍵信息,也方便快速定位解決問題。
    -->
    <settings>
        <!--下劃線駝峰自動轉(zhuǎn)換-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <!--給這個包下的類起別名-->
    <typeAliases>
        <package name="com.xxx.pojo"/>
    </typeAliases>

    <mappers>
        <mapper resource="com/xxx/mapper/UserMapper.xml"/>
    </mappers>

    
</configuration>

spring-mybatis.xml

<!--
spring整合mybatis,根據(jù)mybatis-spring文檔可以,需要一個數(shù)據(jù)源獲取SqlSessionFactory 和至少一個數(shù)據(jù)映射器類
具體查看文檔:http://mybatis.org/spring/zh/getting-started.html
-->

    <!--
    DataSource:使用Spring的數(shù)據(jù)源替換Mybatis的配置:druid c3p0,dbcp
    這里使用Speing提供的JDBC:org.springframework.jdbc.datasource.DriverManagerDataSource
    前提是要導(dǎo)入:spring-jdbc 包
    -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybaits?serverTimezone=UTC&amp;allowPublicKeyRetrieval=true&amp;useSSL=false&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--MyBatis-Spring 中,可使用 SqlSessionFactoryBean來創(chuàng)建 SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    <!--跟在mybatis中學(xué)習(xí)一樣,需要在mybatis核心配置文件綁定xxxmapper.xml文件
        這里也需要綁定mybatis核心配置文件,綁定后,mybatis核心配置文件可以完成的這里也都可以完成,則mybatis-config文件可以不要也行
    -->
        <!--綁定mybatis-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注冊映射器,等價于mybatis核心配置文件中的:
            <mappers>
            <mapper resource="com/xxx/mapper/UserMapper.xml"/>
            </mappers>
        -->
        <!--<property name="mapperLocations" value="classpath:com/xxx/mapper/*.xml"/>-->
    </bean>

    <!--注冊SqlSessionTemplate,相當(dāng)于我們使用的sqlSession,因此可將id命名為此好記-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--因為sqlSessionTemplate只有構(gòu)造方法而無set方法,只能使用構(gòu)造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <!--配置聲明式事務(wù)(AOP原理,不改變源代碼條件下增加事務(wù)),而編程式事務(wù)要在源代碼上自動try catch
        具體可查看文檔:http://mybatis.org/spring/zh/transactions.html
    -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--結(jié)合AOP實現(xiàn)事務(wù)的織入-->
    <!--配置事務(wù)的通知-->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <!--給具體方法配置事務(wù)和傳播新特性(propagation=REQUIRED是默認(rèn)的,即會自動創(chuàng)建事務(wù))
             具體查看:https://blog.csdn.net/edward0830ly/article/details/7569954
             name="*"表示給所有方法配置事務(wù),也可給具體方法,給出方法名即可
             -->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
   <!--配置事務(wù)切入-->
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.xxx.mapper.*.*(..))"/>
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>

applicationContext.xml

 <import resource="spring-mybatis.xml"/>
    <bean id="userMapperImpl_2" class="com.xxx.mapper.UserMapperImpl_2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

log4j.properties

#將等級為DEBUG的日志信息輸出到console和file這兩個目的地,console和file的定義在下面的代碼
log4j.rootLogger=DEBUG,console,file
#控制臺輸出的相關(guān)設(shè)置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件輸出的相關(guān)設(shè)置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/xxx.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志輸出級別
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

3.編寫接口及其實現(xiàn)類和配置對應(yīng)的mapper.xml文件

UserMapper接口

public interface UserMapper {
    //查詢所有用戶
    public List<User> queryUser();
    //添加一個用戶
    int addUser(User user);
    //根據(jù)id刪除用戶
    int deleteUser(int id);
}

UserMapperImpl_2實現(xiàn)類

/**
 *@program: Spring_study
 *@description: spring-mybatis整合方式二:繼承SqlSessionDaoSupport實現(xiàn)接口
 *@author: XieXianXin
 *@create: 2021-06-05 22:08
 */
public class UserMapperImpl_2 extends SqlSessionDaoSupport implements UserMapper {
    @Override
    public List<User> queryUser() {
        return getSqlSession().getMapper(UserMapper.class).queryUser();
    }
    @Override
    public int addUser(User user) {
        return getSqlSession().getMapper(UserMapper.class).addUser(user);
    }
    @Override
    public int deleteUser(int id) {
        return getSqlSession().getMapper(UserMapper.class).deleteUser(id);
    }
}

UserMapper.xml

<!--namespace==綁定一個對應(yīng)的Dao/Mapper接口,以后Mapper.xml文件都放在resourse下,
                                            但是要建立一個跟Mapper接口相對應(yīng)得包
    注意??!這里有一個坑,當(dāng)在resources下建立包時候,不要寫為:com.xxx.dao
                                                      應(yīng)該為:com/xxx/dao
                                           -->
<!--詭異事件,在學(xué)習(xí)mabatis適合,寫UTF-8沒錯,但是整合這里的所有XML卻報錯:1 字節(jié)的 UTF-8 序列的字節(jié) 1 無效。
    解決方法:將所有的XML文件UTF-8改為UTF8即可-->
<mapper namespace="com.xxx.mapper.UserMapper">
    <!--
    last_name已經(jīng)進(jìn)行自動駝峰轉(zhuǎn)換,則這里不用resultMap進(jìn)行不同名的映射
    resultType中也起了別名,不用再寫com.xxx.pojo了
    -->
    <select id="queryUser" resultType="User">
        select * from user
    </select>
    <insert id="addUser" parameterType="User">
        insert into user (id,last_name,email) values (#{id},#{lastName},#{email})
    </insert>
    <delete id="deleteUser" parameterType="_int">
        delete from user where id = #{id}
    </delete>
</mapper>

測試及結(jié)果

手動設(shè)置錯誤,如在插入語句上寫錯insert為inserts

  <insert id="addUser" parameterType="User">
        inserts into user (id,last_name,email) values (#{id},#{lastName},#{email})
    </insert>
public class UserMapperTest {
    static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
   @Test
    public void userMapperImpl_2(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapperImpl_2 = context.getBean("userMapperImpl_2", UserMapper.class);
        userMapperImpl_2.addUser(new User(12,"xiaoxin","com@xiaoxin"));
        userMapperImpl_2.deleteUser(8);
        for (User user : userMapperImpl_2.queryUser()) {
            System.out.println(user);
        }
    }
}

如果為插入語句錯誤,則項目不能正常插入,事務(wù)會回滾。

在這里插入圖片描述

查看并刷新數(shù)據(jù)庫表,沒有變化。

在這里插入圖片描述

接著將錯誤改正后,再次測試結(jié)果為:

在這里插入圖片描述

成功添加和刪除,事務(wù)保證了數(shù)據(jù)的一致性。查看數(shù)據(jù)庫表為:

在這里插入圖片描述

總結(jié)

本篇文章的內(nèi)容就到這了,希望大家可以喜歡,也希望大家可以多多關(guān)注腳本之家的其他精彩內(nèi)容!

相關(guān)文章

  • Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏

    Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏

    這篇文章主要介紹了Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • java Thread 多線程

    java Thread 多線程

    本篇文章小編為大家介紹,java Thread 多線程。需要的朋友參考下
    2013-04-04
  • 如何在 Java 中實現(xiàn)不可變類

    如何在 Java 中實現(xiàn)不可變類

    這篇文章主要介紹了如何在 Java 中實現(xiàn)不可變類,不變性是函數(shù)式編程的關(guān)鍵,因為它與盡量減少變化部分的這一目標(biāo)相一致,這使得對這些部分的推斷更為容易一些。,需要的朋友可以參考下
    2019-06-06
  • Java使用遞歸解決算法問題的實例講解

    Java使用遞歸解決算法問題的實例講解

    遞歸算法的實質(zhì)是把問題分解成規(guī)模縮小的同類問題的子問題,然后遞歸調(diào)用方法來表示問題的解,這里我們就來看幾個Java使用遞歸解決算法問題的實例講解
    2016-06-06
  • SpringBoot實現(xiàn)Excel讀取的實例教程

    SpringBoot實現(xiàn)Excel讀取的實例教程

    這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)Excel讀取的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 使用Spring Data JPA的坑點記錄總結(jié)

    使用Spring Data JPA的坑點記錄總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于使用Spring Data JPA的一些坑點,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • springcloud?gateway無法路由問題的解決

    springcloud?gateway無法路由問題的解決

    gateway網(wǎng)關(guān)的重要作用之一便是進(jìn)行路由轉(zhuǎn)發(fā)工作,下面這篇文章主要給大家介紹了關(guān)于springcloud?gateway無法路由問題的解決方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 詳解Java Selenium中的鼠標(biāo)控制操作

    詳解Java Selenium中的鼠標(biāo)控制操作

    本文主要講解如何用java Selenium 控制鼠標(biāo)在瀏覽器上的操作方法。主要列舉的代碼示例,大家可以自己上代碼執(zhí)行操作看效果,希望對大家有所幫助
    2023-01-01
  • MyBatis是如何實現(xiàn)日志模塊的詳解

    MyBatis是如何實現(xiàn)日志模塊的詳解

    這篇文章主要給大家介紹了關(guān)于MyBatis是如何實現(xiàn)日志模塊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用MyBatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Mybatis下的SQL注入漏洞原理及防護(hù)方法解析

    Mybatis下的SQL注入漏洞原理及防護(hù)方法解析

    SQL 注入是發(fā)生在 Web 程序中數(shù)據(jù)庫層的安全漏洞,是網(wǎng)站存在最多也是最簡單的漏洞,在實際項目中,即使使用了 Mybatis 框架,但仍然有可能因為編碼人員安全意識不足而導(dǎo)致 SQL 注入問題,這篇文章主要介紹了Mybatis下的SQL注入漏洞原理及防護(hù)方法?,需要的朋友可以參考下
    2022-11-11

最新評論