10 Spring AOP

AOP(Aspect Oriented Programming,面向切面編程),通過提供另一種思考程序的方式來補充OOP(Object Oriented Programming,面向?qū)ο缶幊蹋OP是橫向抽取,OOP是縱向抽象。
切面可以用于事務(wù)管理、日志等方面的模塊化

AOP核心概念

  • Aspect(切面)
  • Join Point(連接點)
  • Advice(通知/增強)
  • Pointcut(切點)
  • Introduction(引入)
  • Target Object(目標對象)
  • AOP Proxy(AOP代理)
  • Weaving(織入)

其中,Advice的主要類型有:

  • Before Advice(前置通知)
  • After Returning Advice(返回后通知)
  • After Throwing Advice(拋出異常后通知)
  • After (finally)Advice(最后通知)
  • Around Advice(環(huán)繞通知)

切入點和連接點的匹配,是AOP的關(guān)鍵

Spring AOP

Spring AOP用純Java實現(xiàn),目前僅支持方法調(diào)用作為連接點。
Spring AOP通常和Spring IoC容器一起使用

Hello的前置增強練習(xí)

  • pom.xml中添加AOP相關(guān)依賴
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring</groupId>
    <artifactId>aop</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>aop</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.5.RELEASE</spring.version>
        <aspectj.version>1.9.2</aspectj.version>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.12</slf4j.version>
    </properties>

    <dependencies>
        <!--spring-context依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring-aop依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--aspectj依賴-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <!--spring-test依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--junit依賴-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- log4j日志依賴 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
  • Hello接口和實現(xiàn)類
package com.spring.aop;

public interface Hello {
    String sayHello();
}
package com.spring.aop;

public class HelloImpl implements Hello {
    @Override
    public String sayHello() {
        return "Spring AOP";
    }
}
  • MyBefore類
package com.spring.aop;

/**
 * 用戶自定義前置增強類
 */
public class MyBefore {
    public void beforeMethod() {
        System.out.println("This is a before method...");
    }
 }
  • 配置文件
    <bean id="hello" class="com.spring.aop.HelloImpl"/>
    <bean id="myBefore" class="com.spring.aop.MyBefore"/>
    <aop:config>
        <aop:aspect id="before" ref="myBefore">
            <aop:pointcut id="myPointcut" expression="execution(* com.spring.aop.*.*(..))"/>
            <aop:before method="beforeMethod" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
  • 應(yīng)用主類
package com.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloApp {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello = (Hello) ac.getBean("hello");
        System.out.println(hello.sayHello());
    }
}
  • 運行結(jié)果


    運行結(jié)果

可以看到:Mybefore的方法被織入到了Hello的調(diào)用方法前面,前置增強生效

實戰(zhàn):使用@AspectJ注解的例子

“武松打虎”——武松(Fighter)在山里等著老虎(Tiger)出現(xiàn),只要發(fā)現(xiàn)老虎出來,就打老虎。

  • 定義業(yè)務(wù)模型
    Tiger類
package com.spring.aspectj;

public class Tiger {
    public void walk() {
        System.out.println("Tiger is walking...");
    }
}
  • 定義切面和配置
    前置通知和后置通知
    Fighter類
package com.spring.aspectj;

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

@Aspect
public class Fighter {
    @Pointcut("execution(* com.spring.aspectj.Tiger.walk())")
    public void foundTiger() {

    }

    @Before(value = "foundTiger()")
    public void foundBefore() {
        System.out.println("Fighter wait for tiger...");
    }

    @AfterReturning("foundTiger()")
    public void foundAfter() {
        System.out.println("Fighter fight with tiger...");
    }
}
  • 配置文件
    <!--啟動AspectJ支持-->
    <aop:aspectj-autoproxy/>
    <!--配置bean-->
    <bean id="tiger" class="com.spring.aspectj.Tiger"/>
    <bean id="fighter" class="com.spring.aspectj.Fighter"/>
  • 主程序
package com.spring.aspectj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FighterApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Tiger tiger = context.getBean(Tiger.class);
        tiger.walk();
    }
}
  • 運行結(jié)果


    運行結(jié)果
?著作權(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)容