package com.taotao.anno;
/**
* create by 刘鸿涛
* 2022/4/24 16:19
*/
@SuppressWarnings({"all"})
public interface TargetInterface {
public void save();
}
package com.taotao.anno;
/**
* create by 刘鸿涛
* 2022/4/24 16:27
*/
@SuppressWarnings({"all"})
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("正在存储数据....");
}
}
package com.taotao.anno;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* create by 刘鸿涛
* 2022/4/24 19:51
*/
@SuppressWarnings({"all"})
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
public void afterReturning(){
System.out.println("后置增强.....");
}
//Proceeding JoinPoint:正在执行的连接点 === 切点
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强...");
Object proceed = pjp.proceed(); //切点方法
System.out.println("环绕后增强...");
}
public void afterThrowing(){
System.out.println("异常抛出增强......");
}
public void after(){
System.out.println("最终增强...");
}
}
使用注解@Component(“myAspect”)
package com.taotao.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/24 19:51
*/
@SuppressWarnings({"all"})
@Component("myAspect")
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
public void afterReturning(){
System.out.println("后置增强.....");
}
//Proceeding JoinPoint:正在执行的连接点 === 切点
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强...");
Object proceed = pjp.proceed(); //切点方法
System.out.println("环绕后增强...");
}
public void afterThrowing(){
System.out.println("异常抛出增强......");
}
public void after(){
System.out.println("最终增强...");
}
}
@Component(“target”)
package com.taotao.anno;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/24 16:27
*/
@SuppressWarnings({"all"})
@Component("target")
public class Target implements TargetInterface {
@Override
public void save() {
int i = 1 / 0;
System.out.println("正在存储数据....");
}
}
@Aspect //标注当前MyAspect是一个切面类
注解配置通知方法,并写入切点表达式
package com.taotao.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/24 19:51
*/
@SuppressWarnings({"all"})
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
//配置前置通
@Before("execution(* com.taotao.anno.*.*(..))")
public void before(){
System.out.println("前置增强....");
}
public void afterReturning(){
System.out.println("后置增强.....");
}
//Proceeding JoinPoint:正在执行的连接点 === 切点
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强...");
Object proceed = pjp.proceed(); //切点方法
System.out.println("环绕后增强...");
}
public void afterThrowing(){
System.out.println("异常抛出增强......");
}
public void after(){
System.out.println("最终增强...");
}
}
//配置前置通
@Before("execution(* com.taotao.anno.*.*(..))")
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.taotao.anno"/>
<aop:aspectj-autoproxy/>
beans>
package com.taotao;
import com.taotao.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* create by 刘鸿涛
* 2022/4/24 20:17
*/
@SuppressWarnings({"all"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}
package com.taotao.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/24 19:51
*/
@SuppressWarnings({"all"})
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
public void afterReturning(){
System.out.println("后置增强.....");
}
@Around("execution(* com.taotao.anno.*.*(..))")
//Proceeding JoinPoint:正在执行的连接点 === 切点
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强...");
Object proceed = pjp.proceed(); //切点方法
System.out.println("环绕后增强...");
}
public void afterThrowing(){
System.out.println("异常抛出增强......");
}
public void after(){
System.out.println("最终增强...");
}
}
package com.taotao.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/24 19:51
*/
@SuppressWarnings({"all"})
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
public void afterReturning(){
System.out.println("后置增强.....");
}
@Around("pointcut()")
//Proceeding JoinPoint:正在执行的连接点 === 切点
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强...");
Object proceed = pjp.proceed(); //切点方法
System.out.println("环绕后增强...");
}
public void afterThrowing(){
System.out.println("异常抛出增强......");
}
public void after(){
System.out.println("最终增强...");
}
@Pointcut("execution(* com.taotao.anno.*.*(..))")
public void pointcut(){}
}