1. 程式人生 > 其它 >Spring 使用原生API介面實現AOP

Spring 使用原生API介面實現AOP

技術標籤:JavaWebSpringjavaspringaop

文章目錄


友情連結:
AOP是什麼?
使用原生API介面實現AOP
使用自定義類實現AOP
使用註解實現AOP

一、搭建環境

模擬service層,首先建立個UserService介面:

package com.wzq.service;

public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}

UserService

介面的實現類:

package com.wzq.service;

public class UserServiceImpl implements UserService{
    public void add() {
        System.out.println("增加了一條資訊");
    }

    public void delete() {
        System.out.println("刪除了一條資訊");
    }

    public void update() {
        System.out.println
("更新了一條資訊"); } public void query() { System.out.println("查詢了一條資訊"); } }

pom.xml中注入aspectjweaver依賴:

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

二、實現原生API介面

原生API介面有五個,如下表所示:
在這裡插入圖片描述
這裡實現一下前兩個

建立BeforeLog類,實現MethodBeforeAdvice介面,需要寫before方法,引數說明都在註釋裡:

package com.wzq.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeLog implements MethodBeforeAdvice {
    /*
     * method:要執行的目標物件的方法
     * args:引數
     * target:目標物件
     * */
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("執行了" + target.getClass().getName() + ",中的" + method.getName() + "方法");
    }
}

建立AfterLog類,實現AfterReturningAdvice介面,寫afterReturning方法:

package com.wzq.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    /*
     * returnValue:返回值
     * method:要執行的目標物件的方法
     * args:引數
     * target:目標物件
     * */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("執行了" + target.getClass().getName() +
                "中的" + method.getName() + "方法,返回引數是:" + returnValue);
    }
}

三、配置xml檔案

.xml中配置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
        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>

首先需要註冊bean

<!-- 註冊bean -->
<bean id="userService" class="com.wzq.service.UserServiceImpl"/>
<bean id="afterLog" class="com.wzq.log.AfterLog"/>
<bean id="beforeLog" class="com.wzq.log.BeforeLog"/>

然後是配置aop

<!-- 方式一:使用原生Spring API介面 -->
<!-- 配置aop:需要匯入aop約束 -->
<aop:config>
    <!-- 切入點 -->
    <aop:pointcut id="pointcut" expression="execution(* com.wzq.service.UserServiceImpl.*(..))"/>
    <!-- 執行環繞增加 -->
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut" />
    <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut" />
</aop:config>

注意在aop:pointcut標籤裡面有一個expression表示式,它的作用就是切入點入口

expression="execution(修飾符 返回值 包名.類名/介面名.方法名(引數列表))"

在上面的例子中,忽略掉了修飾符,*表示所有,比如com.wzq.service.UserServiceImpl.*(..)),它的意思是:com.wzq.service.UserServiceImpl類下所有的方法和所有的引數

四、測試

編寫測試類:

import com.wzq.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void Test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = (UserService) context.getBean("userService");
        service.add();
    }
}

在這裡插入圖片描述