Spring Boot AOP實(shí)例demo

1.項(xiàng)目結(jié)構(gòu):

項(xiàng)目結(jié)構(gòu)

1.1 基于Aspect的AOP

1.1.1 父pom.xml配置

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
    </parent>

    <groupId>com.lance.demo</groupId>
    <artifactId>com-lance-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

</project>

1.1.2 module的pom.xml配置

<?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">
    <parent>
        <artifactId>com-lance-demo</artifactId>
        <groupId>com.lance.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>com-lance-demo-provider</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId> 
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.1.3 主類:

package com.lance.demo.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.lance.demo")
@SpringBootApplication
public class SpringBootDemoMain {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoMain.class, args);
    }
}

1.1.4 MyService.java

package com.lance.demo.service;

import com.lance.demo.exception.MyException;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    public int myFunction() {
        System.out.println("MyFunction execute");
        return 0;
    }

    public int myBizFunction() throws MyException {
        System.out.println("myBizFunction execute");
        return 1;
    }
}

1.1.5 MyAspject.java

package com.lance.demo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    @Pointcut("execution(public * com.lance.demo.service.*.*(..))")
    public void aspect() {
        System.out.println("aspect");
    }

    @Before("aspect()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("doBefore:"+ joinPoint.getArgs() +"," + joinPoint.getSignature());
    }

    @After("aspect()")
    public void doAfter(JoinPoint joinPoint) {
        System.out.println("doAfter:"+ joinPoint.getArgs() +"," + joinPoint.getSignature());
    }

    @AfterReturning("aspect()")
    public void doAfterReturning(JoinPoint joinPoint) {
        System.out.println("doAfterReturning:" + joinPoint.getArgs() +"," + joinPoint.getSignature());
    }

    @AfterThrowing(pointcut = "aspect()",throwing = "e")
    public void deAfterThrowing(JoinPoint joinPoint,Throwable e) {
        if(e == null) {
            System.out.println("deAfterThrowing");
        } else {
            System.out.println("deAfterThrowing:" + e);
        }
    }

    @Around("aspect()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("deAround before");
        Object object = joinPoint.proceed();
        System.out.println("deAround after");
        return object;
    }
}

1.1.6單元測(cè)試類MyServiceTest

package com.lance.demo.service;

import com.lance.demo.main.SpringBootDemoMain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootDemoMain.class)
@WebAppConfiguration
public class MyServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void myFunction(){
        myService.myFunction();
    }

    @Test
    public void myBizFunction(){
       int result =  myService.myBizFunction();
       System.out.println("result:" + result);
    }
}

1.1.7 輸出結(jié)果:

deAround before
doBefore:[Ljava.lang.Object;@444cc791,int com.lance.demo.service.MyService.myBizFunction()
myBizFunction execute
doAfterReturning:[Ljava.lang.Object;@444cc791,int com.lance.demo.service.MyService.myBizFunction()
doAfter:[Ljava.lang.Object;@444cc791,int com.lance.demo.service.MyService.myBizFunction()
deAround after
result:1

1.2 ControllerAdvice僅對(duì)controller層有效的AOP

1.2.1 MyController.java

package com.lance.demo.controller;

import com.lance.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyService myService;

    @RequestMapping("/hello")
    public String hello() {
        int result = myService.myFunction();
        return "Hello World!" + result;
    }

    @RequestMapping("/biz/hello")
    public String bizHello() {
        int result = myService.myBizFunction();
        return "Hello World!" + result;
    }
}

1.2.2 GlobalExceptionHandler.java

package com.lance.demo.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception e) {
        log.error("exceptionHandler error.",e);
        return "Exception";
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Object myExceptionHandler(Exception e) {
        log.error("myExceptionHandler error.",e);
        return "MyException";
    }
}

1.2.3 自定義異常類MyException.java

package com.lance.demo.exception;

public class MyException extends RuntimeException {

    public MyException(String message) {
        super(message);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末桶错,一起剝皮案震驚了整個(gè)濱河市赘风,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌江场,老刑警劉巖赛惩,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件造成,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡泽篮,警方通過查閱死者的電腦和手機(jī)盗尸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來帽撑,“玉大人泼各,你說我怎么就攤上這事】骼” “怎么了扣蜻?”我有些...
    開封第一講書人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵逆巍,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我莽使,道長(zhǎng)锐极,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任芳肌,我火速辦了婚禮灵再,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘亿笤。我一直安慰自己翎迁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開白布净薛。 她就那樣靜靜地躺著汪榔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪肃拜。 梳的紋絲不亂的頭發(fā)上痴腌,一...
    開封第一講書人閱讀 49,144評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音燃领,去河邊找鬼士聪。 笑死,一個(gè)胖子當(dāng)著我的面吹牛柿菩,可吹牛的內(nèi)容都是我干的戚嗅。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼枢舶,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了替久?” 一聲冷哼從身側(cè)響起凉泄,我...
    開封第一講書人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蚯根,沒想到半個(gè)月后后众,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡颅拦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年蒂誉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片距帅。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡右锨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出碌秸,到底是詐尸還是另有隱情绍移,我是刑警寧澤悄窃,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站蹂窖,受9級(jí)特大地震影響轧抗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瞬测,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一横媚、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧月趟,春花似錦分唾、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至碳褒,卻和暖如春折砸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背沙峻。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工睦授, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人摔寨。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓去枷,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親是复。 傳聞我的和親對(duì)象是個(gè)殘疾皇子删顶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345