日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

狂神说Spring学习笔记————(一发入魂)

發(fā)布時(shí)間:2024/1/8 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 狂神说Spring学习笔记————(一发入魂) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 1、Spring
    • 1.1、簡(jiǎn)介
    • 1.2、優(yōu)點(diǎn)
    • 1.3、組成
  • 2、IOC理論推導(dǎo)
    • 2.1、IOC本質(zhì)
  • 3 Hello Spring
  • 4、IOC創(chuàng)建對(duì)象的方式
    • 使用無(wú)參構(gòu)造創(chuàng)建對(duì)象,默認(rèn)!
    • 假設(shè)我們要使用有參構(gòu)造創(chuàng)建對(duì)象。
  • 5、Spring配置
    • 5.1、 別名
    • 5.2 、Bean的配置
    • 5.3 、import
  • 6、DI依賴注入
    • 6.1、構(gòu)造器注入
    • 6.2 、set方式注入【重點(diǎn)】
    • 6.3、 擴(kuò)展方式注入
      • P命令
      • C命令
    • 6.4 、bean的作用域
  • 7、bean的自動(dòng)裝配
    • 7.1 、測(cè)試
    • 7.2、ByName自動(dòng)裝配
    • 7.3、ByTyte自動(dòng)裝配
    • 7.4 、使用注解實(shí)現(xiàn)自動(dòng)裝配
      • 7.4.1、 @Autowired與@Resource
  • 8、使用注解開(kāi)發(fā)
  • 9、使用Java的方式配置Spring
  • 10、代理模式
    • 10.1、靜態(tài)代理
    • 10.2 、加深理解
    • 10.3、動(dòng)態(tài)代理
  • 11、AOP
    • 11.1 什么是AOP
    • 11.2 AOP在Spring中的作用
    • 11.3 使用Spring實(shí)現(xiàn)Aop
  • 12、整合Mybatis
    • 12.1、回憶mybatis
    • 12.2、Mybatis-Spring
  • 13、聲明式事物
    • 13.1、回顧事務(wù)
    • 13.2、Spring中的事物管理

1、Spring

1.1、簡(jiǎn)介

  • Spring:春天----->給軟件行業(yè)帶來(lái)春天

  • 2002,首次推出Spring框架的出行:interface21的框架!

  • Spring框架級(jí)以interface21為基礎(chǔ),經(jīng)過(guò)重新設(shè)計(jì),并不斷豐富其內(nèi)涵,于2004年3月24日,發(fā)布了1.0正式版

  • Rod Johnson

    Spring Framework創(chuàng)始人,著名作者。 Rod在悉尼大學(xué)不僅獲得了計(jì)算機(jī)學(xué)位,同時(shí)還獲得了音樂(lè)學(xué)位。更令人吃驚的是在回到軟件開(kāi)發(fā)領(lǐng)域之前,他還獲得了音樂(lè)學(xué)的博士學(xué)位。

  • Spring理念:使現(xiàn)有的技術(shù)更加容易使用,本身是一個(gè)大雜燴,整合了現(xiàn)有的技術(shù)框架!

  • SSH:Struct2+Spring+hibernate

  • SSM:SpringMVC + Spring + Mybatis!

官網(wǎng):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、優(yōu)點(diǎn)

  • Spring是一個(gè)開(kāi)源的免費(fèi)框架(容器)!
  • Spring是一個(gè)輕量級(jí)的,非入侵式的框架!
  • 控制反轉(zhuǎn)(IOC),面向切面編程(AOP)!
  • 支持事務(wù)的處理,對(duì)框架整合的支持!

總結(jié)一句話:Spring就是一個(gè)輕量級(jí)的控制反轉(zhuǎn)(IOC)和面向切面編程(AOP)的框架!

1.3、組成

現(xiàn)代化的Java開(kāi)發(fā)!說(shuō)白就是基于Spring的開(kāi)發(fā)!

  • Spring Boot
    • 一個(gè)快速開(kāi)發(fā)的腳手架。
    • 基于SpringBoot可以快速的開(kāi)發(fā)單個(gè)微服務(wù)。
    • 約定大于配置。
  • Spring Cloud
    • SpringCloud是基于SpringBoot實(shí)現(xiàn)的。

因?yàn)楝F(xiàn)在大多數(shù)公司都在使用SpringBoot進(jìn)行快速開(kāi)發(fā),學(xué)習(xí)SpringBoot的前提,需要完全掌握Spring及SpringMVC!承上啟下的作用!

弊端:發(fā)展了太久之后,違背了原來(lái)的理念!配置十分繁瑣,人稱“配置地獄”!

2、IOC理論推導(dǎo)

  • UserDao 接口

  • UserDaoImpl 實(shí)現(xiàn)類

  • UserService 業(yè)務(wù)接口

  • UserServiceImpl 業(yè)務(wù)實(shí)現(xiàn)類

  • 在我們之前的業(yè)務(wù)中,用戶的需求可能會(huì)影響我們?cè)瓉?lái)的代碼,我們需要根據(jù)用戶的需求去修改原代碼!如果程序代碼量十分大,修改一次的成本代價(jià)十分昂貴!

    private UserDao userDao;//利用set進(jìn)行動(dòng)態(tài)實(shí)現(xiàn)值的注入!public void setUserDao(UserDao userDao) {this.userDao = userDao;}
    • 之前,程序是主動(dòng)創(chuàng)建對(duì)象!控制權(quán)在程序猿手上!
    • 使用了set注入后,程序不再具有主動(dòng)性,而是變成了被動(dòng)的接收對(duì)象!

    這種思想,從本質(zhì)上解決了問(wèn)題,我們程序猿不用再去管理對(duì)象的創(chuàng)建了。系統(tǒng)的耦合性大大降低~,可以更加專注的在業(yè)務(wù)的實(shí)現(xiàn)上!這是IOC的原型!

    2.1、IOC本質(zhì)

    控制反轉(zhuǎn)IoC(Inversion of Control),是一種設(shè)計(jì)思想,DI(依賴注入)是實(shí)現(xiàn)IoC的一種方法,也有人認(rèn)為DI只是IoC的另一種說(shuō)法。沒(méi)有IoC的程序中,我們使用面向?qū)ο缶幊?#xff0c;對(duì)象的創(chuàng)建與對(duì)象間的依賴關(guān)系完全硬編碼在程序中,對(duì)象的創(chuàng)建由程序自己控制,控制反轉(zhuǎn)后將對(duì)象的創(chuàng)建轉(zhuǎn)移給第三方,個(gè)人認(rèn)為所謂控制反轉(zhuǎn)就是:獲得依賴對(duì)象的方式反轉(zhuǎn)了。

    采用XML方式配置Bean的時(shí)候,Bean的定義信息是和實(shí)現(xiàn)分離的,而采用注解的方式可以把兩者合為一體,Bean的定義信息直接以注解的形式定義在實(shí)現(xiàn)類中,從而達(dá)到了零配置的目的。

    控制反轉(zhuǎn)是一種通過(guò)描述(XML或注解)并通過(guò)第三方去生產(chǎn)或獲取特定對(duì)象的方式。在Spring中實(shí)現(xiàn)控制反轉(zhuǎn)的是IoC容器,其實(shí)現(xiàn)方法是依賴注入(Dependency Injection,DI)。

    3 Hello Spring

  • 新建一個(gè)maven項(xiàng)目,編寫實(shí)體類
  • public class Hello {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String toString() {return "Hello{" +"str='" + str + '\'' +'}';} }
  • 編寫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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--使用Spring來(lái)創(chuàng)建對(duì)象,在Spring這些都稱為Bean類型 變量名=new 類型();Hello hello=new hello();id=變量名;class=new 的對(duì)象;property 相當(dāng)于給對(duì)象中的屬性設(shè)置一個(gè)值--><bean id="hello" class="com.kuang.pojo.Hello"><property name="str" value="Spring"/></bean> </beans>
  • 測(cè)試
  • public static void main(String[] args) {//獲取Spring的上下文對(duì)象!ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//我們的對(duì)象現(xiàn)在都在Spring中的管理了,我們需要使用,直接去里面取出來(lái)就可以!Hello hello = (Hello) context.getBean("hello");System.out.println(hello.toString());}

    思考問(wèn)題?

    • Hello對(duì)象是誰(shuí)創(chuàng)建的?
      Hello對(duì)象是由Spring創(chuàng)建的。
    • Hello對(duì)象的屬性是怎么設(shè)置的?
      Hello對(duì)象的屬性是由Spring容器設(shè)置的。

    這個(gè)過(guò)程就叫做控制反轉(zhuǎn):

    控制:誰(shuí)來(lái)控制對(duì)象的創(chuàng)建,傳統(tǒng)應(yīng)用程序的對(duì)象是由程序本身控制創(chuàng)建的,使用Spring后,對(duì)象是由Spring來(lái)創(chuàng)建的。

    反轉(zhuǎn):程序本身不創(chuàng)建對(duì)象,而變成被動(dòng)的接收對(duì)象。

    依賴注入:就是利用set方法來(lái)進(jìn)行注入的。

    IOC是一種編程思想,由主動(dòng)的編程變成被動(dòng)的接收。

    可以通過(guò)new ClassPathXmlApplicationContext去瀏覽一下底層源碼。

    針對(duì)之前的文檔進(jìn)行的Spring配置文件的練習(xí)

  • 添加beans.xml文件
  • <bean id="mysqlImpl" class="com.kuang.dao.UserDaoMysqlImpl"/><bean id="oraleImpl" class="com.kuang.dao.UserDaoOraleImpl"/><bean id="SqlServerImpl" class="com.kuang.dao.UserDaoSqlServerImpl"/><bean id="UserServiceImpl" class="com.kuang.service.UserServiceImpl"><property name="userDao" ref="mysqlImpl"/></bean> </beans><!--ref:引用Spring容器中創(chuàng)建好的對(duì)象value:具體的值,基本數(shù)據(jù)類型:-->
  • 修改test文件
  • //獲取ApplicationContext:拿到Spring的容器ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");//容器在手,天下我有,需要什么,直接get什么UserServiceImpl userServiceImpl=(UserServiceImpl) context.getBean("UserServiceImpl");userServiceImpl.getUser();

    4、IOC創(chuàng)建對(duì)象的方式

    • 使用無(wú)參構(gòu)造創(chuàng)建對(duì)象,默認(rèn)!

  • 創(chuàng)建user類
  • package com.kuang.pojo2;public class User {private String name;public User(){System.out.println("User的無(wú)參構(gòu)造");}public String getName() {return name;}public void setName(String name) {this.name = name;}public void show(){System.out.println("name"+name);} }

    2.創(chuàng)建beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.kuang.pojo2.User"><property name="name" value="秦將"/></bean> </beans>

    3.創(chuàng)建測(cè)試類

    public class MyTest {public static void main(String[] args) {ApplicationContext Context= new ClassPathXmlApplicationContext("beans.xml");User user=(User) Context.getBean("user");user.show();} }
    • 假設(shè)我們要使用有參構(gòu)造創(chuàng)建對(duì)象。

  • 下標(biāo)賦值
  • <!--第一種 下標(biāo)賦值--> <bean id="user" class="com.kuang.pojo2.User"><constructor-arg index="0" value="狂神說(shuō)Java"/> </bean>
  • 類型,通過(guò)類型創(chuàng)建有參構(gòu)造,不建議使用
  • <!--第二種 類型,通過(guò)類型創(chuàng)建有參構(gòu)造--> <bean id="user" class="com.kuang.pojo2.User"><constructor-arg type="java.lang.String" value="秦將"/> </bean>
  • 第三種 直接通過(guò)參數(shù)名進(jìn)行設(shè)置
  • <!-- 第三種 直接通過(guò)參數(shù)名進(jìn)行設(shè)置--> <bean id="user" class="com.kuang.pojo2.User"><constructor-arg name="name" value="qinjiang"/> </bean>

    總結(jié):在配置文件加載的時(shí)候,容器中管理的對(duì)象就已經(jīng)初始化了!

    5、Spring配置

    5.1、 別名

    <!--別名,如果添加了別名,我們也可以使用別名獲取到這個(gè)對(duì)象--> <alias name="user" alias="userNew"/>

    5.2 、Bean的配置

    <!-- id:bean的唯一標(biāo)識(shí)符,也就是相當(dāng)于我們學(xué)的對(duì)象名 class:bean對(duì)象所對(duì)應(yīng)的全限定名:包名+類名 name:也是別名,而且name可以同時(shí)取多個(gè)別名--> <bean id="userT" class="com.kuang.pojo.UserT" name="user2 u2,u3;u4"><property name="name" value="黑心白蓮"/> </bean>

    5.3 、import

    這個(gè)import。一般用于團(tuán)隊(duì)開(kāi)發(fā)使用,它可以將多個(gè)配置文件,導(dǎo)入合并為一個(gè)。
    假設(shè),現(xiàn)在項(xiàng)目中有多個(gè)人開(kāi)發(fā),這三個(gè)人負(fù)責(zé)不同的類開(kāi)發(fā),不同的類需要注冊(cè)在不同的bean中,我們可以利用import將所有人的beans.xml合并為一個(gè)總的!

    • 張三
    • 李四
    • 王五
    • applicationContext.xml
    <import resource="bean.xml"/> <import resource="bean2.xml"/> <import resource="bean3.xml"/>

    使用的時(shí)候,直接使用總的applicationContext.xml就可以了。

    6、DI依賴注入

    6.1、構(gòu)造器注入

    (有參,無(wú)參構(gòu)造器注入,詳細(xì)見(jiàn)4、IOC創(chuàng)建對(duì)象的方式)

    6.2 、set方式注入【重點(diǎn)】

    • 依賴注入:本質(zhì)是set注入
      • 依賴:bean對(duì)象的創(chuàng)建依賴與容器!
      • 注入:bean對(duì)象中的所有屬性,有容器注入!

    【環(huán)境搭建】

  • 復(fù)雜類型

    • 搭建其中類Address和Student,并且設(shè)置get和setter
    package com.kuang.pojo;public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;} } package com.kuang.pojo;import com.sun.xml.internal.ws.wsdl.writer.document.http.Address;import java.lang.ref.SoftReference; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties;public class Student {private String name;private Address address;private String[] books;private List<String> hobbies;private Map<String,String> card;private String wife;private Properties info;//配置文件所使用的類} }
    • 配置beans.xml
    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="student" class="com.kuang.pojo.Student"><property name="name" value="秦將"/></bean></beans>
    • 測(cè)試類
    public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); }}
    • 完善注入信息
    <bean id="address" class="com.kuang.pojo.Address"><property name="address" value="西安"/></bean><bean id="student" class="com.kuang.pojo.Student"><!--第一種:普通值注入,value --><property name="name" value="qinijiang"/><!--第二種,bean注入(引用注入) ,ref--><property name="address" ref="address"/><!--數(shù)組 --><property name="books"><array><value>紅樓夢(mèng)</value><value>西游記</value><value>水滸傳</value><value>三國(guó)演義</value></array></property><!--List --><property name="hobbies"><list><value>打籃球</value><value>看電影</value><value>敲代碼</value></list></property><!--Map --><property name="card"><map><entry key="身份證" value="123456789987456321"/><entry key="銀行卡" value="359419496419481649"/></map></property><!--Set --><property name="games"><set><value>LOL</value><value>COC</value><value>BOB</value></set></property><!--NULL --><property name="wife"><null/></property><!--Properties --><property name="info"><props><prop key="driver">20211029</prop><prop key="url">102.0913.524.4585</prop><prop key="password">123452456</prop></props></property></bean>
  • 6.3、 擴(kuò)展方式注入

    我們可以使用p命名空間和c命名空間進(jìn)行注入
    官方解釋:

    P命令相當(dāng)于是set注入,C命令相當(dāng)于構(gòu)造器注入(constructor)

    P命令

    使用步驟:

    1.配置userbeans.xml

    xmlns:p="http://www.springframework.org/schema/p" <!--p命令空間注入,可以直接注入屬性的值:property--> <bean id="user" class="com.kuang.pojo.User" p:name="秦將" p:age="18"/>

    2.修改測(cè)試類

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

    注意:需要導(dǎo)入junit的包

    <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> </dependency>

    C命令

    1.修改userbears.xml

    注意不可以直接注入:需要添加有參和無(wú)參構(gòu)造器

    xmlns:c="http://www.springframework.org/schema/c" <!--p命令空間注入,不可以直接注入屬性的值:constructor--> <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="17"/>

    2.修改測(cè)試類

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

    注意點(diǎn):p命名和c命名空間不能直接使用,需要導(dǎo)入xml約束!

    xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"

    6.4 、bean的作用域

    scope為作用域

    注意:singleton表示單例,全局唯一,而prototype表示原型模式,每次從容器中g(shù)et的時(shí)候,都會(huì)產(chǎn)生一個(gè)新對(duì)象!

    而request.session等是webmvc中使用

    1.單例模式(Spring模式模式)

    <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="17" scope="singleton"/>

    注意:singleton模式下的是一樣的,單線程一般使用單例模式,多線程使用原型模式

    測(cè)試:

    ApplicationContext context= new ClassPathXmlApplicationContext("userbeans.xml"); User user =context.getBean("user2", User.class); User user2 =context.getBean("user2", User.class); System.out.println(user.hashCode()); System.out.println(user2.hashCode()); System.out.println(user==user2);

    輸出結(jié)果:

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-JKJanU9J-1638191586706)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211009091310180.png)]

    2.原型模式:每次從容器中g(shù)et的時(shí)候,都會(huì)產(chǎn)生一個(gè)新對(duì)象

    <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>

    測(cè)試:(可以按照單例模式進(jìn)行測(cè)試)

    輸出結(jié)果:

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-34qipxJI-1638191586715)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211010113127074.png)]

    7、bean的自動(dòng)裝配

    • 自動(dòng)裝配是Spring滿足bean依賴一種方式
    • Spring會(huì)在上下文中自動(dòng)尋找,并自動(dòng)給bean裝配屬性

    在Spring中有三種裝配方式:

  • 在xml顯示的配置(ID依賴注入)
  • 在java中顯示配置(java config中配置)
  • 隱式的自動(dòng)裝配bean【重要】
  • 7.1 、測(cè)試

    環(huán)境搭建:創(chuàng)建項(xiàng)目,一個(gè)人有兩個(gè)寵物

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.People"> <property name="name" value="小章"/> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> </bean></beans>

    7.2、ByName自動(dòng)裝配

    <bean id="cat" class="com.yang.entity.Cat"/> <bean id="dog" class="com.yang.entity.Dog"/><!-- byName:會(huì)在容器上下文中查找,和自己對(duì)象set方法后面的值相對(duì)應(yīng)的beanid! --> <bean id="people" class="com.kuang.pojo.People" autowire="byName"> <property name="name" value="小章"/> </bean>

    7.3、ByTyte自動(dòng)裝配

    <!--byType:會(huì)自動(dòng)在容器上下文中查找,和自己對(duì)象屬性類型相同的bean--> <bean class="com.kuang.pojo.Cat"/> <bean class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.People" autowire="byType"> <property name="name" value="小章"/> </bean>

    小結(jié):

    • ByName的時(shí)候,需要保證所有bean的id唯一,并且這個(gè)bean需要和自動(dòng)注入的屬性的set方法的值一致!
    • ByType的時(shí)候,需要保證所有bean的class唯一,并且這個(gè)bean需要和自動(dòng)注入的屬性的類型一致!

    7.4 、使用注解實(shí)現(xiàn)自動(dòng)裝配

    jdk1.5支持的注解,Spring2.5就支持注解了!

    The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML

    要使用注解須知:

    1.導(dǎo)入約束。context約束

    2.配置注解【注意:context:annotation-config 開(kāi)啟注解的支持】

    <?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"> <!--開(kāi)啟注解的支持 --> <context:annotation-config/></beans> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.People" />

    @Autowired

    直接在屬性上使用即可!也可以在set方法上使用!

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-DEK8SBTn-1638191586720)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211010094952302.png)]

    使用Autowired我們就可以不用編寫set方法了,前提是你這個(gè)自動(dòng)配置的屬性在IOC(Spring)容器中存在,且符合名字ByName!

    7.4.1、 @Autowired與@Resource

    @Autowired
    直接在屬性上使用即可!也可以在set方式上使用
    使用Autowired我們可以不用編寫set方法了,前提是你這個(gè)自動(dòng)裝配的屬性在IOC(Spring)容器中存在,且符合名字byName

    科普:

    @Nullable 字段標(biāo)記了這個(gè)注解,說(shuō)明這個(gè)字段可以為null; public @interface Autowired { boolean required() default true;}

    測(cè)試代碼:

    public class People { //如果顯式定義了Autowired的required屬性為false,說(shuō)明這個(gè)對(duì)象可以為null,否則不允許為空 @Autowired(required = false) private Cat cat; @Autowired private Dog dog; private String name;}

    如果@Autowired自動(dòng)裝配的環(huán)境比較復(fù)雜,自動(dòng)裝配無(wú)法通過(guò)一個(gè)注解【@Autowired】完成的時(shí)候,我們可以使用@Qualifier(value = “xxx”)去配置@Autowired的使用,指定一個(gè)唯一的bean對(duì)象注入!

    public class People { @Autowired @Qualifier(value = "cat111") private Cat cat; @Autowired @Qualifier(value = "dog222") private Dog dog; private String name;}

    @Resource注解,不指定name值,先去判斷byName和byType,有一個(gè)能注入即成功

    public class People { @Resource(name = "xxxx") private Cat cat;

    小結(jié):@Resource和@Autowired的區(qū)別

    • 都是用來(lái)自動(dòng)裝配的,都可以放在屬性字段上
    • @Autowired通過(guò)Bytype的方式實(shí)現(xiàn),而且必須要求這個(gè)對(duì)象存在!【常用】
    • @Resource默認(rèn)通過(guò)ByName的方式實(shí)現(xiàn),如果找不到名字,則可以通過(guò)Bytype實(shí)現(xiàn),功能較為強(qiáng)大,如果兩個(gè)都找不到的情況下,就會(huì)報(bào)錯(cuò)!【常用】
    • 執(zhí)行順序的不同:@Autowired通過(guò)ByType的方式實(shí)現(xiàn)。@Resource默認(rèn)通過(guò)byName的方式實(shí)現(xiàn)。

    8、使用注解開(kāi)發(fā)

    在Spring4之后,要使用注解開(kāi)發(fā),必須要保證aop的包導(dǎo)入了

    使用注解需要導(dǎo)入約束,配置注解的支持!

    <?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--開(kāi)啟注解的支持 --><context:annotation-config/></beans>

    1. bean注入使用@Componet注解

    //@Component 等價(jià)于<bean id="user" class="com.kuang.pojo"/> @Component public class User {public String name="qian"; }
    • 或者使用value注入
    //相當(dāng)于 <property name="name" value="qianli"/> @Component public class User {@Value("qianli")public String name="qian"; }
    • 或者使用set方法進(jìn)行注入
    @Value("qianli2")public void setName(String name) {this.name = name;}

    2.測(cè)試類

    public class Mytest { public static void main(String[] args) { ApplicationContext context= new ClassPathXmlApplicationContext("appicationContext.xml"); User user=(User) context.getBean("user"); System.out.println(user.name); }}

    3.衍生的注解
    @Component有幾個(gè)衍生注解,我們?cè)趙eb開(kāi)發(fā)中,會(huì)按照mvc三層架構(gòu)分層!衍生注解都是和Component一樣的

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

    這四個(gè)注解功能都是一樣的,都是代表將某個(gè)類注冊(cè)到Spring中,裝配Bean

    4.自動(dòng)裝配

    - @Autowired:自動(dòng)裝配通過(guò)類型,名字。如果Autowired不能唯一自動(dòng)裝配上屬性,則需要通過(guò)@Qualifier(value = "xxx")去配置。- @Nullable 字段標(biāo)記了了這個(gè)注解,說(shuō)明這個(gè)字段可以為null;- @Resource:自動(dòng)裝配通過(guò)名字,類型。

    5.作用域

    @Scope(“singleton”)單例模式

    @Scope(“prototype”)原型模式

    @Component@Scope("singleton")public class User { public String name="qian"; @Value("qianli2") public void setName(String name) { this.name = name; }}

    6.小結(jié)

    xml與注解:

    • xml更加萬(wàn)能,適用于任何場(chǎng)合!維護(hù)簡(jiǎn)單房間
    • 注解不是自己的類使用不了,維護(hù)相對(duì)負(fù)責(zé)

    xml與注解最佳實(shí)踐:

    • xml用來(lái)管理bean;
    • 注解只負(fù)責(zé)完成屬性的注入;
    • 我們?cè)谑褂眠^(guò)程中,只需要注意個(gè)問(wèn)題:必須讓注解生效,就需要開(kāi)啟注解的支持
    <!--開(kāi)啟注解支持--> <context:annotation-config/> <!--指定要掃描的包,這個(gè)包下的注解就會(huì)生效--> <context:component-scan base-package="com.kuang"/>

    9、使用Java的方式配置Spring

    我們現(xiàn)在要完全不使用Spring的xml配置了,全權(quán)交給Java來(lái)做!
    JavaConfig是Spring的一個(gè)子項(xiàng)目,在Spring4之后,它成為了一個(gè)核心功能![外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-4VKZXOKt-1638191586727)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211010140328630.png)]

    • 實(shí)體類

    • //這里這個(gè)是注解的意思,就是說(shuō)明這個(gè)類被Spring接管了,注冊(cè)到了容器中@Componentpublic class User { private String name; public String getName() { return name; } @Value("qinjiang")//屬性注入值 public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; }}
    • 配置文件

    //這個(gè)也會(huì)spring容器托管,因?yàn)樗緛?lái)就是一個(gè)@component,@Configuration代表這個(gè)是一個(gè)配置類,就和我們之前看的beans.xml @Configuration @ComponentScan("com.kuang.java") public class KuangConfig { //注冊(cè)已被bean,即相當(dāng)于我們之前寫的一個(gè)bean標(biāo)簽 //這個(gè)方法的名字,就相當(dāng)于bean標(biāo)簽中的id屬性 //這個(gè)方法的返回值,就相當(dāng)于bean標(biāo)簽中的class屬性 @Bean public User getUser(){ return new User();//就是返回要注入到bean的對(duì)象! } }

    測(cè)試類

    public class Mytest { public static void main(String[] args) { //如果完全使用了配置類方式去做,我們就只能通過(guò)AnnotationConfig上下文來(lái)獲取容器,通過(guò)配置類的class對(duì)象加載! ApplicationContext context= new AnnotationConfigApplicationContext(KuangConfig.class); User getuser=(User) context.getBean("getUser"); System.out.println(getuser.getName()); }}

    這種純Java的配置方式,在SpringBoot中隨處可見(jiàn)!

    10、代理模式

    為什么要學(xué)習(xí)代理模式?因?yàn)檫@就是SpringAOP的底層!【SpringAOP和SpringMVC】

    代理模式的分類:

    • 靜態(tài)代理
    • 動(dòng)態(tài)代理

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-aWEdHVRl-1638191586735)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211010222814373.png)]

    10.1、靜態(tài)代理

    角色分析:

    • 抽象角色:一般會(huì)使用接口或者抽象類來(lái)解決
    • 真實(shí)角色:被代理的角色
    • 代理角色:代理真實(shí)角色,代理真實(shí)角色后,我們一般會(huì)做一些附屬操作
    • 客戶:訪問(wèn)代理對(duì)象的人

    代碼步驟:

  • 接口
  • //接口 public interface Rent {public void rent(); }
  • 真實(shí)角色
  • //房東 public class Hosts implements Rent {public void rent(){System.out.println("房東要出租房子!");} }
  • 代理角色
  • public class Porxy implements Rent {private Hosts hosts;public Porxy() {}public Porxy(Hosts hosts) {this.hosts = hosts;}public void rent() {hosts.rent();seehouses();sign();fee();}//看房public void seehouses(){System.out.println("中介帶著我要看房子!");}//簽合同public void sign(){System.out.println("和中介簽署租賃合同!");}//收費(fèi)用public void fee(){System.out.println("中介收取費(fèi)用!");} }
  • 客戶端訪問(wèn)代理角色
  • public class Client { public static void main(String[] args) { //房東要出租房子 Hosts hosts=new Hosts(); //hosts.rent(); //代理,中介幫房東出租房子,并且代理角色一般會(huì)有一些復(fù)數(shù)操作! Porxy porxy=new Porxy(hosts); //不用直接面對(duì)房東,直接找中介租房即可 porxy.rent(); }}

    代理模式的好處:

    • 可以使真實(shí)角色的操作更加純粹!不用去關(guān)注一些公共的業(yè)務(wù)
    • 公共的業(yè)務(wù)也就交給代理角色!實(shí)現(xiàn)了業(yè)務(wù)的分工
    • 公共業(yè)務(wù)發(fā)生擴(kuò)展的時(shí)候,方便集中管理!

    缺點(diǎn):

    • 一個(gè)真實(shí)角色就會(huì)產(chǎn)生一個(gè)代理角色:代碼量會(huì)翻倍,開(kāi)發(fā)效率較低

    10.2 、加深理解

    代碼步驟:

  • 接口
  • public interface UserService {public void add();public void delete();public void update();public void query(); }
  • 真實(shí)角色
  • //真實(shí)角色 public class UserServiceImpl implements UserService {public void add() {System.out.println("增加了一個(gè)用戶");}public void delete() {System.out.println("刪除了一個(gè)用戶");}public void update() {System.out.println("添加了一個(gè)用戶");}public void query() {System.out.println("查看了一個(gè)用戶");} }
  • 代理角色
  • ublic class UserServicePorxy implements UserService{private UserServiceImpl userService;public void setUserService(UserServiceImpl userService) {this.userService = userService;}public void add() {log("add");userService.add();}public void delete() {log("delete");userService.delete();}public void update() {log("update");userService.update();}public void query() {log("query");userService.query();}//日志方法public void log(String msg){System.out.println("[debug]使用了"+msg+"方法");} }
  • 客戶端訪問(wèn)代理角色
  • public class Client { public static void main(String[] args) { UserServiceImpl userService=new UserServiceImpl(); UserServicePorxy proxy=new UserServicePorxy(); proxy.setUserService(userService); proxy.add(); }}

    問(wèn)題:

    • 為什么使用代理模式?

      符合面向?qū)ο蟮钠叽笤瓌t,因?yàn)樵诠局行薷乃藭鴮懙脑创a可以直接導(dǎo)致代碼崩盤!

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-h8mPuCQF-1638191586739)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211011000528194.png)]

    10.3、動(dòng)態(tài)代理

    • 動(dòng)態(tài)代理和靜態(tài)代理一樣
    • 動(dòng)態(tài)代理的代理類是動(dòng)態(tài)生成的,不是我們直接寫好的!
    • 動(dòng)態(tài)代理分為兩大類:基于接口的動(dòng)態(tài)dialing,基于類的動(dòng)態(tài)代理
      • 基于接口——JDK動(dòng)態(tài)代理【我們?cè)谶@里使用】
      • 基于類:cglib
      • Java字節(jié)碼實(shí)現(xiàn):javasist

    需要了解兩個(gè)類:Proxy(代理).InvocationHandler(調(diào)用處理程序)

    代碼實(shí)現(xiàn)步驟:

  • 接口
  • public interface Rent { public void rent();}
  • 真實(shí)角色
  • public class Host implements Rent{ public void rent() { System.out.println("房東要出租房子!"); }}
  • ProxyInvocationHandler類
  • //我們會(huì)用這個(gè)類,自動(dòng)生成代理類!public class ProxyInvocationHandler implements InvocationHandler { //被代理的接口 private Rent rent; public void setRent(Rent rent) { this.rent = rent; } //生成得到代理類 public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(),this); } //處理代理實(shí)例,并返回結(jié)果 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //動(dòng)態(tài)代理的本質(zhì),就是使用反射機(jī)制實(shí)現(xiàn)! Object result = method.invoke(rent, args); seeHose(); fee(); return result; } public void seeHose(){ System.out.println("中介帶著看房子!"); } public void fee(){ System.out.println("中介收取費(fèi)用!"); }}
  • 測(cè)試
  • public class Client { public static void main(String[] args) { //真實(shí)角色 Host host = new Host(); //代理角色:現(xiàn)在沒(méi)有 ProxyInvocationHandler pih = new ProxyInvocationHandler(); //通過(guò)調(diào)用程序處理角色來(lái)處理我們要調(diào)用的接口對(duì)象! pih.setRent(host); Rent proxy = (Rent) pih.getProxy(); //這里的proxy就是動(dòng)態(tài)生成的,我們并沒(méi)有寫 proxy.rent(); }}
    • 注意:上述方法比較麻煩,我們可以采用較為簡(jiǎn)單的方法,進(jìn)行自動(dòng)生成代理類

      //等我們會(huì)用這個(gè)類,自動(dòng)生成代理類public class ProxyInvocationHandler implements InvocationHandler { //被代理的接口 private Object target; public void setTarget(Object target) { this.target = target; } //生成得到代理類 public Object getProxy(){ return Proxy.newProxyInstance( this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } //處理代理實(shí)例,并返回結(jié)果 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log(method.getName()); Object result=method.invoke(target,args); return result; } public void log(String msg){ System.out.println("[Debug] 使用了一個(gè)"+msg+"方法"); }}
      • 測(cè)試
    public class Client { public static void main(String[] args) { //真實(shí)角色 UserServiceImpl userService=new UserServiceImpl(); //代理角色,不存在 ProxyInvocationHandler pih=new ProxyInvocationHandler(); pih.setTarget(userService);//要設(shè)置成代理的對(duì)象 //動(dòng)態(tài)生成代理類 UserService proxy=(UserService) pih.getProxy(); proxy.query(); }}

    動(dòng)態(tài)代理的好處:

    • 可以是真實(shí)角色操作更加純粹!不用去關(guān)注一些公共的業(yè)務(wù)
    • 公共角色就交給代理角色!實(shí)現(xiàn)了業(yè)務(wù)的分工
    • 公共業(yè)務(wù)發(fā)生擴(kuò)展的時(shí)候,方便集中管理
    • 一個(gè)動(dòng)態(tài)代理類代理的是一個(gè)接口,一般對(duì)應(yīng)的一類業(yè)務(wù)
    • 一個(gè)動(dòng)態(tài)代理類可以代理多個(gè)類,只要是實(shí)現(xiàn)了同一個(gè)接口!

    11、AOP

    11.1 什么是AOP

    AOP(Aspect Oriented Programming)意為:面向切面編程,通過(guò)預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開(kāi)發(fā)中的一個(gè)熱點(diǎn),也是Spring框架中的一個(gè)重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開(kāi)發(fā)的效率。
    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-EZlwg5bF-1638191586741)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211012231104045.png)]

    11.2 AOP在Spring中的作用

    提供聲明式事務(wù);允許用戶自定義切面

    • 橫切關(guān)注點(diǎn):跨越應(yīng)用程序多個(gè)模塊的方法或功能。即是,與我們業(yè)務(wù)邏輯無(wú)關(guān)的,但是我們需要關(guān)注的部分,就是橫切關(guān)注點(diǎn)。如日志,安全,緩存,事務(wù)等等…
    • 切面(ASPECT):橫切關(guān)注點(diǎn)被模塊化的特殊對(duì)象。即,它是一個(gè)類。
    • 通知(Advice):切面必須要完成的工作。即,它是類中的一個(gè)方法。
    • 目標(biāo)(Target):被通知對(duì)象。
    • 代理(Proxy):向目標(biāo)對(duì)象應(yīng)用通知之后創(chuàng)建的對(duì)象。
    • 切入點(diǎn)(PointCut):切面通知執(zhí)行的“地點(diǎn)”的定義。
    • 連接點(diǎn)(JointPoint):與切入點(diǎn)匹配的執(zhí)行點(diǎn)
      [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-VIbNrIHL-1638191586745)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211012231307452.png)]

    SpringAOP中,通過(guò)Advice定義橫切邏輯,Spring中支持5種類型的Advice:

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-XolcMDHW-1638191586748)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211012231359232.png)]

    即AOP在不改變?cè)写a的情況下,去增加新的功能。

    11.3 使用Spring實(shí)現(xiàn)Aop

    【重點(diǎn)】使用AOP織入,需要依賴包

    <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version></dependency>

    方式一: 使用Spring的API接口【主要是SpringAPI接口實(shí)現(xiàn)】

  • 在service包下,定義UserService業(yè)務(wù)接口和UserServiceImpl實(shí)現(xiàn)類
  • public interface UserService { public void add(); public void delete(); public void update(); public void select();} public class UserServiceImpl implements UserService { public void add() { System.out.println("增加了一個(gè)用戶!"); } public void delete() { System.out.println("刪除了一個(gè)用戶!"); } public void update() { System.out.println("更新了一個(gè)用戶!"); } public void select() { System.out.println("查詢了一個(gè)用戶!"); }}
  • 在log包下,定義我們的增強(qiáng)類,一個(gè)Log前置增強(qiáng)和一個(gè)AfterLog后置增強(qiáng)類
  • public class Log implements MethodBeforeAdvice { //method: 要執(zhí)行的目標(biāo)對(duì)象的方法 //args:參數(shù) //target:目標(biāo)對(duì)象 public void before(Method method, Object[] agrs, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"被執(zhí)行了"); }} public class AfterLog implements AfterReturningAdvice { //returnValue: 返回值 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("執(zhí)行了"+method.getName()+"方法,返回結(jié)果為:"+returnValue); }}
  • 最后去spring的文件中注冊(cè) , 并實(shí)現(xiàn)aop切入實(shí)現(xiàn) , 注意導(dǎo)入約束,配置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"> <!--注冊(cè)bean--> <bean id="userService" class="com.kuang.service.UserServiceImpl"/> <bean id="log" class="com.kuang.log.Log"/> <bean id="afterLog" class="com.kuang.log.AfterLog"/> <!--方式一:使用原生Spring API接口--> <!--配置aop:需要導(dǎo)入aop的約束--> <aop:config> <!--切入點(diǎn):expression:表達(dá)式,execution(要執(zhí)行的位置!* * * * *)--> <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/> <!--執(zhí)行環(huán)繞增加!--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config></beans>
  • 測(cè)試
  • public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //動(dòng)態(tài)代理代理的是接口:注意點(diǎn) UserService userService = (UserService) context.getBean("userService"); userService.add();// userService.select(); }}

    方式二: 自定義類來(lái)實(shí)現(xiàn)AOP【主要是切面定義】

  • 在diy包下定義自己的DiyPointCut切入類
  • public class DiyPointCut { public void before(){ System.out.println("=======方法執(zhí)行前========"); } public void after(){ System.out.println("=======方法執(zhí)行后========"); }}
  • 去spring中配置文件
  • <!--方式二:自定義類--> <bean id="diy" class="com.kuang.diy.DiyPointCut"/> <aop:config> <!--自定義切面,ref 要引用的類--> <aop:aspect ref="diy"> <!--切入點(diǎn)--> <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/> <!--通知--> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config>
  • 測(cè)試
    • 與方式一測(cè)試一樣
  • 方式三: 使用注解實(shí)現(xiàn)!

    在diy包下定義注解實(shí)現(xiàn)的AnnotationPointCut增強(qiáng)類

    //聲明式事務(wù)! @Aspect //標(biāo)注這個(gè)類是一個(gè)切面 public class AnnotationPointCut {@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")public void before(){System.out.println("====方法執(zhí)行前====");}@After("execution(* com.kuang.service.UserServiceImpl.*(..))")public void after(){System.out.println("====方法執(zhí)行后====");}//在環(huán)繞增強(qiáng)中,我們可以給定一個(gè)參數(shù),代表我們要獲取處理切入的點(diǎn);@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")public void around(ProceedingJoinPoint jp) throws Throwable{System.out.println("環(huán)繞前");Signature signature = jp.getSignature();// 獲得簽名System.out.println("signature:"+signature);Object proceed = jp.proceed(); //執(zhí)行方法System.out.println("環(huán)繞后");System.out.println(proceed);}}
  • 在Spring配置文件中,注冊(cè)bean,并增加支持注解的配置。
  • <!--方式三:使用注解--> <bean id="annotationPointCut" class="com.kuang.diy.AnnotationPointCut"/> <!--開(kāi)啟注解支持! JDK(默認(rèn)是 proxy-target-class="false") cglib(proxy-target-class="true"--> <aop:aspectj-autoproxy/>
  • 測(cè)試
  • [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-AgiwYUsA-1638191586753)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211013114836793.png)]

    注意執(zhí)行順序:環(huán)繞前→方法執(zhí)行前→環(huán)繞后→方法執(zhí)行后

    12、整合Mybatis

    步驟:

  • 導(dǎo)入相關(guān)jar包
    • junit
    • mybatis
    • mysql數(shù)據(jù)庫(kù)
    • spring相關(guān)的
    • aop織入器
    • mybatis-spring【new】
    • 配置Maven靜態(tài)資源過(guò)濾問(wèn)題!
  • 編寫mybatis的配置文件
  • <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!--Spring操作數(shù)據(jù)庫(kù)的話,還需要一個(gè)spring-jdbc --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </build>
  • 測(cè)試
  • 12.1、回憶mybatis

    1.編寫pojo實(shí)體類

    package com.kuang.pojo; public class User { private int id; //id private String name; //姓名 private String pwd; //密碼}

    2.編寫mybatis核心配置文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <typeAliases> <package name="com.kuang.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <package name="com.kuang.mapper.UserMapper"/> </mappers></configuration>

    3.UserDao編寫接口

    public interface UserMapper { public List<User> selectUser();}

    4.編寫接口對(duì)應(yīng)的Mapper.xml映射文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.kuang.dao.UserMapper"> <select id="selectUser" resultType="User"> select * from user </select> </mapper>

    5.測(cè)試類

    @Test public void test() throws IOException { String resource="mybatis-config.xml"; InputStream inputStream= Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession=sqlSessionFactory.openSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); List<User> userList=mapper.selectUser(); for(User user:userList){ System.out.println(user); } sqlSession.close(); }

    12.2、Mybatis-Spring

    引入Spring之前需要了解mybatis-spring包中的一些重要類;

    http://www.mybatis.org/spring/zh/index.html

    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-73Rte2bm-1638191586755)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211128195703834.png)]

    什么是 MyBatis-Spring?

    MyBatis-Spring 會(huì)幫助你將 MyBatis 代碼無(wú)縫地整合到 Spring 中。

    知識(shí)基礎(chǔ)

    在開(kāi)始使用 MyBatis-Spring 之前,你需要先熟悉 Spring 和 MyBatis 這兩個(gè)框架和有關(guān)它們的術(shù)語(yǔ)。這很重要
    MyBatis-Spring 需要以下版本:

    MyBatis-SpringMyBatisSpring FrameworkSpring BatchJava
    2.03.5+5.0+4.0+Java 8+
    1.33.4+3.2.2+2.1+Java 6+

    如果使用 Maven 作為構(gòu)建工具,僅需要在 pom.xml 中加入以下代碼即可:

    <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version></dependency>

    要和 Spring 一起使用 MyBatis,需要在 Spring 應(yīng)用上下文中定義至少兩樣?xùn)|西:一個(gè) SqlSessionFactory 和至少一個(gè)數(shù)據(jù)映射器類。

    在 MyBatis-Spring 中,可使用SqlSessionFactoryBean來(lái)創(chuàng)建 SqlSessionFactory。要配置這個(gè)工廠 bean,只需要把下面代碼放在 Spring 的 XML 配置文件中:

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /></bean>

    注意:SqlSessionFactory需要一個(gè) DataSource(數(shù)據(jù)源)。這可以是任意的 DataSource,只需要和配置其它 Spring 數(shù)據(jù)庫(kù)連接一樣配置它就可以了。

    在基礎(chǔ)的 MyBatis 用法中,是通過(guò) SqlSessionFactoryBuilder 來(lái)創(chuàng)建 SqlSessionFactory 的。而在 MyBatis-Spring 中,則使用 SqlSessionFactoryBean 來(lái)創(chuàng)建。

    在 MyBatis 中,你可以使用 SqlSessionFactory 來(lái)創(chuàng)建 SqlSession。一旦你獲得一個(gè) session 之后,你可以使用它來(lái)執(zhí)行映射了的語(yǔ)句,提交或回滾連接,最后,當(dāng)不再需要它的時(shí)候,你可以關(guān)閉 session。

    SqlSessionFactory有一個(gè)唯一的必要屬性:用于 JDBC 的 DataSource。這可以是任意的 DataSource 對(duì)象,它的配置方法和其它 Spring 數(shù)據(jù)庫(kù)連接是一樣的。

    一個(gè)常用的屬性是 configLocation,它用來(lái)指定 MyBatis 的 XML 配置文件路徑。它在需要修改 MyBatis 的基礎(chǔ)配置非常有用。通常,基礎(chǔ)配置指的是 < settings> 或 < typeAliases>元素。

    需要注意的是,這個(gè)配置文件并不需要是一個(gè)完整的 MyBatis 配置。確切地說(shuō),任何環(huán)境配置(),數(shù)據(jù)源()和 MyBatis 的事務(wù)管理器()都會(huì)被忽略。SqlSessionFactoryBean 會(huì)創(chuàng)建它自有的 MyBatis 環(huán)境配置(Environment),并按要求設(shè)置自定義環(huán)境的值。

    SqlSessionTemplate 是 MyBatis-Spring 的核心。作為 SqlSession 的一個(gè)實(shí)現(xiàn),這意味著可以使用它無(wú)縫代替你代碼中已經(jīng)在使用的 SqlSession。

    模板可以參與到 Spring 的事務(wù)管理中,并且由于其是線程安全的,可以供多個(gè)映射器類使用,你應(yīng)該總是用 SqlSessionTemplate 來(lái)替換 MyBatis 默認(rèn)的 DefaultSqlSession 實(shí)現(xiàn)。在同一應(yīng)用程序中的不同類之間混雜使用可能會(huì)引起數(shù)據(jù)一致性的問(wèn)題。

    可以使用 SqlSessionFactory 作為構(gòu)造方法的參數(shù)來(lái)創(chuàng)建 SqlSessionTemplate 對(duì)象。

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /></bean>

    現(xiàn)在,這個(gè) bean 就可以直接注入到你的 DAO bean 中了。你需要在你的 bean 中添加一個(gè) SqlSession 屬性,就像下面這樣:

    public class UserDaoImpl implements UserDao { private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } public User getUser(String userId) { return sqlSession.getMapper...; }}

    按下面這樣,注入 SqlSessionTemplate:

    <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl"> <property name="sqlSession" ref="sqlSession" /></bean>

    整合實(shí)現(xiàn)一

    1、引入Spring配置文件beans.xml

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"></beans>

    2、配置數(shù)據(jù)源替換mybaits的數(shù)據(jù)源

    <!--配置數(shù)據(jù)源:數(shù)據(jù)源有非常多,可以使用第三方的,也可使使用Spring--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/></bean>

    3、配置SqlSessionFactory,關(guān)聯(lián)MyBatis

    <!--配置SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--關(guān)聯(lián)Mybatis--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/kuang/dao/*.xml"/></bean>

    4、注冊(cè)sqlSessionTemplate,關(guān)聯(lián)sqlSessionFactory;

    <!--注冊(cè)sqlSessionTemplate , 關(guān)聯(lián)sqlSessionFactory--><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--利用構(gòu)造器注入--> <constructor-arg index="0" ref="sqlSessionFactory"/></bean>

    5、增加Dao接口的實(shí)現(xiàn)類;私有化sqlSessionTemplate

    public class UserDaoImpl implements UserMapper { //sqlSession不用我們自己創(chuàng)建了,Spring來(lái)管理 private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public List<User> selectUser() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.selectUser(); } }

    6、注冊(cè)bean實(shí)現(xiàn)

    <bean id="userDao" class="com.kuang.dao.UserDaoImpl"> <property name="sqlSession" ref="sqlSession"/></bean>

    7、測(cè)試

    @Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserMapper mapper = (UserMapper) context.getBean("userDao"); List<User> user = mapper.selectUser(); System.out.println(user); }

    結(jié)果成功輸出!現(xiàn)在我們的Mybatis配置文件的狀態(tài)!發(fā)現(xiàn)都可以被Spring整合!

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <typeAliases> <package name="com.kuang.pojo"/> </typeAliases></configuration>

    整合實(shí)現(xiàn)二

    mybatis-spring1.2.3版以上的才有這個(gè) .

    官方文檔截圖 :

    dao繼承Support類 , 直接利用 getSqlSession() 獲得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不需要管理SqlSessionTemplate , 而且對(duì)事務(wù)的支持更加友好 . 可跟蹤源碼查看
    [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-m2PTYgZv-1638191586759)(C:\Users\jameslzhang\AppData\Roaming\Typora\typora-user-images\image-20211128200622599.png)]

    測(cè)試:

    1、將我們上面寫的UserDaoImpl修改一下

    public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper { public List<User> selectUser() { UserMapper mapper = getSqlSession().getMapper(UserMapper.class); return mapper.selectUser(); }}

    2、修改bean的配置

    <bean id="userDao" class="com.kuang.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>

    3、測(cè)試

    @Testpublic void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserMapper mapper = (UserMapper) context.getBean("userDao"); List<User> user = mapper.selectUser(); System.out.println(user);}

    13、聲明式事物

    13.1、回顧事務(wù)

    • 把一組業(yè)務(wù)當(dāng)成一個(gè)業(yè)務(wù)來(lái)做;要么都成功,要么都失敗!

    • 事務(wù)在項(xiàng)目開(kāi)發(fā)中,十分的重要,涉及到數(shù)據(jù)的一致性問(wèn)題,不能馬虎!

    • 確保完整性和一致性。

    事務(wù)ACID原則:

    • 原子性(atomicity)
      • 事務(wù)是原子性操作,由一系列動(dòng)作組成,事務(wù)的原子性確保動(dòng)作要么全部完成,要么完全不起作用。
    • 一致性(consistency)
      • 一旦所有事務(wù)動(dòng)作完成,事務(wù)就要被提交。數(shù)據(jù)和資源處于一種滿足業(yè)務(wù)規(guī)則的一致性狀態(tài)中。
    • 隔離性(isolation)
      • 可能多個(gè)事務(wù)會(huì)同時(shí)處理相同的數(shù)據(jù),因此每個(gè)事務(wù)都應(yīng)該與其他事務(wù)隔離開(kāi)來(lái),防止數(shù)據(jù)損壞。
    • 持久性(durability)
      • 事務(wù)一旦完成,無(wú)論系統(tǒng)發(fā)生什么錯(cuò)誤,結(jié)果都不會(huì)受到影響。通常情況下,事務(wù)的結(jié)果被寫到持久化存儲(chǔ)器中。

    測(cè)試

    將上面的代碼拷貝到一個(gè)新項(xiàng)目中
    在之前的案例中,我們給userMapper接口新增兩個(gè)方法,刪除和增加用戶;

    //添加一個(gè)用戶 int addUser(User user);//根據(jù)id刪除用戶 int deleteUser(int id);

    UserMapper文件,我們故意把 deletes 寫錯(cuò),測(cè)試!

    <insert id="addUser" parameterType="com.kuang.pojo.User">insert into user (id,name,pwd) values (#{id},#{name},#{pwd})</insert><delete id="deleteUser" parameterType="int">deletes from user where id = #{id}</delete>

    編寫接口的UserMapperImpl實(shí)現(xiàn)類,在實(shí)現(xiàn)類中,我們?nèi)ゲ僮饕徊?/p> public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {//增加一些操作public List<User> selectUser() {User user = new User(5, "小王", "185161");UserMapper mapper = getSqlSession().getMapper(UserMapper.class);mapper.addUser(user);mapper.deleteUser(5);return mapper.selectUser();}//新增public int addUser(User user) {return getSqlSession().getMapper(UserMapper.class).addUser(user);}//刪除public int deleteUser(int id) {return getSqlSession().getMapper(UserMapper.class).deleteUser(id);} }

    測(cè)試

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

    報(bào)錯(cuò):sql異常,delete寫錯(cuò)了

    結(jié)果 :數(shù)據(jù)庫(kù)結(jié)果顯示插入成功!

    沒(méi)有進(jìn)行事務(wù)的管理;我們想讓他們都成功才成功,有一個(gè)失敗,就都失敗,我們就應(yīng)該需要事務(wù)!

    以前我們都需要自己手動(dòng)管理事務(wù),十分麻煩!

    但是Spring給我們提供了事務(wù)管理,我們只需要配置即可;

    13.2、Spring中的事物管理

    Spring在不同的事務(wù)管理API之上定義了一個(gè)抽象層,使得開(kāi)發(fā)人員不必了解底層的事務(wù)管理API就可以使用Spring的事務(wù)管理機(jī)制。Spring支持編程式事務(wù)管理和聲明式的事務(wù)管理。

    編程式事務(wù)管理

    • 將事務(wù)管理代碼嵌到業(yè)務(wù)方法中來(lái)控制事務(wù)的提交和回滾

    • 缺點(diǎn):必須在每個(gè)事務(wù)操作業(yè)務(wù)邏輯中包含額外的事務(wù)管理代碼

    聲明式事務(wù)管理

    • 一般情況下比編程式事務(wù)好用。
    • 將事務(wù)管理代碼從業(yè)務(wù)方法中分離出來(lái),以聲明的方式來(lái)實(shí)現(xiàn)事務(wù)管理。
    • 將事務(wù)管理作為橫切關(guān)注點(diǎn),通過(guò)aop方法模塊化。Spring中通過(guò)Spring AOP框架支持聲明式事務(wù)管理。

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

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

    事務(wù)管理器

    • 無(wú)論使用Spring的哪種事務(wù)管理策略(編程式或者聲明式)事務(wù)管理器都是必須的。
    • 就是 Spring的核心事務(wù)管理抽象,管理封裝了一組獨(dú)立于技術(shù)的方法。

    2. JDBC事務(wù)

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

    3. 配置好事務(wù)管理器后我們需要去配置事務(wù)的通知

    <!--配置事務(wù)通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!--配置哪些方法使用什么樣的事務(wù),配置事務(wù)的傳播特性--><tx:method name="add" propagation="REQUIRED"/><tx:method name="delete" propagation="REQUIRED"/><tx:method name="update" propagation="REQUIRED"/><tx:method name="search*" propagation="REQUIRED"/><tx:method name="get" read-only="true"/><tx:method name="*" propagation="REQUIRED"/></tx:attributes> </tx:advice>

    spring事務(wù)傳播特性:

    事務(wù)傳播行為就是多個(gè)事務(wù)方法相互調(diào)用時(shí),事務(wù)如何在這些方法間傳播。spring支持7種事務(wù)傳播行為:

    • propagation_requierd:如果當(dāng)前沒(méi)有事務(wù),就新建一個(gè)事務(wù),如果已存在一個(gè)事務(wù)中,加入到這個(gè)事務(wù)中,這是最常見(jiàn)的選擇。
    • propagation_supports:支持當(dāng)前事務(wù),如果沒(méi)有當(dāng)前事務(wù),就以非事務(wù)方法執(zhí)行。
    • propagation_mandatory:使用當(dāng)前事務(wù),如果沒(méi)有當(dāng)前事務(wù),就拋出異常。
    • propagation_required_new:新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。
    • propagation_not_supported:以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。
    • propagation_never:以非事務(wù)方式執(zhí)行操作,如果當(dāng)前事務(wù)存在則拋出異常。
    • propagation_nested:如果當(dāng)前存在事務(wù),則在嵌套事務(wù)內(nèi)執(zhí)行。如果當(dāng)前沒(méi)有事務(wù),則執(zhí)行與propagation_required類似的操作
    • Spring 默認(rèn)的事務(wù)傳播行為是 PROPAGATION_REQUIRED,它適合于絕大多數(shù)的情況。

    假設(shè) ServiveX#methodX() 都工作在事務(wù)環(huán)境下(即都被 Spring 事務(wù)增強(qiáng)了),假設(shè)程序中存在如下的調(diào)用鏈:Service1#method1()->Service2#method2()->Service3#method3(),那么這 3 個(gè)服務(wù)類的 3 個(gè)方法通過(guò) Spring 的事務(wù)傳播機(jī)制都工作在同一個(gè)事務(wù)中。

    就好比,我們剛才的幾個(gè)方法存在調(diào)用,所以會(huì)被放在一組事務(wù)當(dāng)中!

    配置AOP

    4. 導(dǎo)入aop的頭文件!

    <!--配置aop織入事務(wù)--> <aop:config><aop:pointcut id="txPointcut" expression="execution(* com.kuang.dao.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>

    5. 進(jìn)行測(cè)試

    刪掉剛才插入的數(shù)據(jù),再次測(cè)試!

    @Test public void test2(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");UserMapper mapper = (UserMapper) context.getBean("userDao");List<User> user = mapper.selectUser();System.out.println(user); }

    思考:

    為什么需要事務(wù)?

    • 如果不配置事務(wù),可能存在數(shù)據(jù)提交不一致的情況;
    • 如果我們不在Spring中去配置聲明式事務(wù),我們就需要在代碼中手動(dòng)配置事務(wù)!
    • 事務(wù)在項(xiàng)目的開(kāi)發(fā)中十分重要,涉及到數(shù)據(jù)的一致性和完整性問(wèn)題,不容馬虎!

    總結(jié)

    以上是生活随笔為你收集整理的狂神说Spring学习笔记————(一发入魂)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。