J2EE進階學(xué)習——Spring框架(五):AOP

AOP概念

1.AOP:面向切面(方面)編程,擴展功能不修改源代碼實現(xiàn)

2.AOP采取橫向抽取機制,取代了傳統(tǒng)縱向繼承體系重復(fù)性代碼(性能監(jiān)測、事務(wù)管理、安全檢查、緩存)


AOP原理

未改進的做法

縱向抽取機制

動態(tài)代理方式

橫向抽取機制

AOP操作術(shù)語(重點掌握加粗的三個)

  • Joinpoint(連接點):所謂連接點是指那些被攔截到的點,在Spring中,這些點指的是方法,因為Spring只支持方法類型的連接點
  • Pointcut(切入點):所謂切入點是指我們要對哪些Joinpoint進行攔截的定義

  • Advice(通知/增強):所謂通知是指攔截到Joinpoint之后所要做的事情就是通知。通知分為前置通知、后置通知、異常通知、最終通知、環(huán)繞通知(切面完成的功能)

  • Aspect(切面):是切入點和通知(引介)的結(jié)合

  • Introduction(引介):引介是一種特殊的通知在不修改類代碼的前提下,Introduction可以在運行期為類動態(tài)地添加一些方法或Field
  • Target(目標對象):代理的目標對象(要增強的類)
  • Weaving(織入):是把增強應(yīng)用到目標的過程,把advice應(yīng)用到target的過程
  • Proxy(代理):一個類被AOP織入增強后,就產(chǎn)生一個結(jié)果代理類
操作術(shù)語

Spring的AOP操作

1.在Sping里面進行AOP操作,使用aspectj實現(xiàn)

  • aspectj不是Spring的一部分,和Spring一起使用進行AOP操作
  • Spring2.0以后新增了對Aspectj支持

2.使用aspectj實現(xiàn)AOP有兩種方式

  • 基于aspectj的xml配置
  • 基于aspectj的注解方式
AOP操作的準備工作:
  • 導(dǎo)包


    jar包
  • 創(chuàng)建Spring核心配置文件,導(dǎo)入AOP的約束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

</beans>

附上文檔網(wǎng)站:https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html

3.使用表達式配置切入點

  • 切入點:實際增強的方法
  • 常用的表達式
    execution(<訪問修飾符>?<返回類型><方法名>(<參數(shù)>)<異常>)
    • execution(* com.TiHom.aop.Book.add(...))
    • execution(* com.TiHom.aop.Book.*(...))
    • execution(* .(...))
    • 匹配所有save開頭的方法 execution(* save*(...))

4.基于AspecJ的配置文件方式的AOP操作

Book

public class Book {
    public void add(){
        System.out.println("add...");
    }
}

MyBook

public class MyBook {
    public void before1(){
        System.out.println("before...");
    }

    public void after1(){
        System.out.println("after...");
    }

    //環(huán)繞增強
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("方法之前");
        //執(zhí)行被增強的方法
        proceedingJoinPoint.proceed();
        System.out.println("方法之后");
    }
}

bean3.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    <aop:aspectj-autoproxy />
    <bean id="book" class="com.TiHom.aop.Book"></bean>
    <bean id="myBook" class="com.TiHom.aop.MyBook"></bean>

    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* com.TiHom.aop.Book.*(..))"/>
        <aop:aspect ref="myBook">
            <aop:before method="before1" pointcut-ref="pointcut1"/>
            <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
            <aop:around method="around" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
</beans>    

5.log4j介紹

  • 通過log4j可以看到程序運行過程中更詳細的內(nèi)容
    • 經(jīng)常使用log4j查看日志
  • 使用
    • 導(dǎo)入log4j的jar包
    • 復(fù)制log4j的配置文件,到src下
  • 設(shè)置日志級別
    • info:看到基本信息
    • debug:看到更詳細的信息

6.spring web整合

讓spring的配置文件在服務(wù)器啟動的時候加載

要導(dǎo)入spring-web的jar包,這里貼一個spring jar包下載地址
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring-web/4.3.1.RELEASE/spring-web-4.3.1.RELEASE.jar

在web.xml中配置

<!-- 指定監(jiān)聽器的位置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring配置的位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:bean1.xml</param-value>
</context-param>

7.基于Aspecj注解的AOP操作

切面中

@Aspect
public class MyBook {
    @Before(value = "execution(* com.TiHom.aop.Book.*(..))")
    public void before1(){
        System.out.println("before...");
    }
}

bean3.xml中

    <!-- 開啟AOP操作 -->
    <aop:aspectj-autoproxy />
    <!-- 創(chuàng)建對象 -->
    <bean id="book" class="com.TiHom.aop.Book"></bean>
    <bean id="myBook" class="com.TiHom.aop.MyBook"></bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容