✨✨个人主页:沫洺的主页
📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏
📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏
📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏
💖💖如果文章对你有所帮助请留下三连✨✨
它不属于spring;
AspectJ是一个AOP的框架;
定义了AOP语法;
有一个专门的编译器用来生成遵守Java字节编码规范的Class文件
- AspectJ是使用面向切面的一个框架
- 它扩展了Java语言(它本身也是一种语言)
- 支持原生Java代码 有自己的编译器
- 将代码翻译成Java字节码文件
- 是为了方便编写AOP代码而出现的
- 使用AOP编程的三个重点 通知 切点 织入
添加AOP坐标
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-aopartifactId> dependency>创建增强类MyAOP
对service层下的所有类的所有方法进行增强
@Component @Aspect public class MyAOP { //定义切点 @Pointcut("execution(* com.moming.service.*.*(..))") public void point(){} @Before("point()") public void before(){ System.out.println("===>前置通知"); } @After("point()") public void after(){ System.out.println("===>后置通知"); } @Around("point()") public Object arround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("===>环绕前"); Object resules = pjp.proceed(); System.out.println("===>环绕后"); return resules; } @AfterReturning(value = "point()",returning = "ret") public void returning(JoinPoint jp, String ret){ Object[] args = jp.getArgs(); System.out.println("返回后通知获取参数: "+Arrays.toString(args)); System.out.println("===>返回后通知,返回值: "+ret); } @AfterThrowing("point()") public void throwing(){ System.out.println("===>异常通知"); } }service/OrderService
@Service public class OrderService { public String order(int id){ System.out.println("===>目标方法:订单业务ID:"+id); return "001202210121010"; } }启动类测试
@SpringBootApplication public class App2 { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App2.class, args); OrderService bean = context.getBean(OrderService.class); System.out.println(bean.order(1)); } }无异常时
有异常时,后续代码就不再执行了
补充配置说明
//两种占位符 //* 代表的是一个单词,b* 代表的是以b开头的单词。 例如 bds //.. 通配符 ,代表的是0个或者多个匹配项
声明注解NeedCut
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD}) public @interface NeedCut { }切换注解
@Component @Aspect public class MyAOP { //定义切点 @Pointcut("@annotation(com.moming.annotation.NeedCut)") public void point(){} @Before("point()") public void before(){ System.out.println("===>前置通知"); } @After("point()") public void after(){ System.out.println("===>后置通知"); } @Around("point()") public Object arround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("===>环绕前"); Object resules = pjp.proceed(); System.out.println("===>环绕后"); return resules; } @AfterReturning(value = "point()",returning = "ret") public void returning(JoinPoint jp, String ret){ Object[] args = jp.getArgs(); System.out.println("返回后通知获取参数: "+Arrays.toString(args)); System.out.println("===>返回后通知,返回值: "+ret); } @AfterThrowing("point()") public void throwing(){ System.out.println("===>异常通知"); } }使用注解@NeedCut
@Service public class OrderService { @NeedCut public String order(int id){ System.out.println("===>目标方法:订单业务ID:"+id); return "001202210121010"; } public void add(){ System.out.println("===>添加订单"); } }测试
@SpringBootApplication public class App2 { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App2.class, args); OrderService bean = context.getBean(OrderService.class); System.out.println(bean.order(01)); System.out.println("-------------------"); bean.add(); } }使用@NeedCut注解的方法才进行增强