SpringBoot AOP 的配置

已經搭建好的demo下載:
請點擊

1.首先一個基本的Springboot框架

相關的demo下載:
https://download.csdn.net/download/qq_23095607/11811555
相關的教程:
https://blog.csdn.net/qq_23095607/article/details/100930331

2.在pom.xml這種添加AOP依賴:

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

3.Application類添加相關的註解:

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableCaching
@EnableAsync

4.添加切面以及通知

package com.example.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class BrokerAspect {
	/**
	 * 定義切入點,切入點爲com.example.demo.aop.AopController中的所有函數
	 * 通過@Pointcut註解聲明頻繁使用的切點表達式
	 */
	@Pointcut("execution(public * com.example.demo.AopController.*(..)))")
	public void BrokerAspect() {

	}

	/**
	 * @description 在連接點執行之前執行的通知
	 */
	@Before("BrokerAspect()")
	public void doBeforeGame() {
		System.out.println("喫飯之前先訂餐!");
	}

	/**
	 * @description 在連接點執行之後執行的通知(返回通知和異常通知的異常)
	 */
	@After("BrokerAspect()")
	public void doAfterGame() {
		System.out.println("喫完之後好舒服1");
	}

	/**
	 * @description 在連接點執行之後執行的通知(返回通知)
	 */
	@AfterReturning("BrokerAspect()")
	public void doAfterReturningGame() {
		System.out.println("喫完之後好舒服2");
	}

	/**
	 * @description 在連接點執行之後執行的通知(異常通知)
	 */
	@AfterThrowing("BrokerAspect()")
	public void doAfterThrowingGame() {
		System.out.println("外賣壞了");
	}
}

附言:@Pointcut爲定義要切的位置
相關通知的加強使用請查看:https://blog.csdn.net/qq_23095607/article/details/101211322
以及加強通知的demo下載:
https://download.csdn.net/download/yeyinglingfeng/9904070

然後就可以了,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章