1. 程式人生 > 其它 >Spring 使用註解實現AOP

Spring 使用註解實現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<ersion>
    <
scope
>
runtime</scope> </dependency>

二、用註解實現AOP

使用註解,需要在自定義的類上面加上@Aspect

在執行方法前、後、環繞方法上分別加上@Before()@After()@Around(),括號裡面是具體的哪個類哪個方法需要執行

package com.wzq.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//標記這個類是一個切面
@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.wzq.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("==========方法執行前==========");
    }

    @After("execution(* com.wzq.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("==========方法執行前==========");
    }

    //在環繞增強中,可以給定一個引數,代表想要獲取處理切入的點
    @Around("execution(* com.wzq.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("Around  ==========方法執行前==========");
        //執行
        Object proceed = jp.proceed();
        //獲取簽名
        Signature signature = jp.getSignature();
        System.out.println(signature);
        System.out.println("Around  ==========方法執行後==========");
    }
}

三、配置xml

使用註解,需要開啟註解支援:

<aop:aspectj-autoproxy />

完整程式碼:

<?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">

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

    <!-- 方式三 -->
    <bean id="annotationPointCut" class="com.wzq.log.AnnotationPointCut" />
    <!-- 開啟註解支援 -->
    <aop:aspectj-autoproxy />

</beans>

四、測試

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();
    }
}

在這裡插入圖片描述