SSM-Spring-17:Spring中aspectJ注解版

时间:2023-03-09 08:56:08
SSM-Spring-17:Spring中aspectJ注解版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

AspectJ

  AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供代码的织入

       @AspectJ是AspectJ 5新增的功能,使用JDK 5.0 注解技术和正规的AspectJ切点表达式语言描述切面

       Spring通过集成AspectJ实现了以注解的方式定义增强类,大大减少了配置文件中的工作量

       使用@AspectJ,首先要保证所用的JDK 是5.0或以上版本

注解版案例:

  一个接口,一个类,一个普通类,实现注解的

  ISomeService接口:

package cn.dawn.day19aspectj;

/**
* Created by Dawn on 2018/3/8.
*/
public interface ISomeService {
public void insert();
public void delete();
public void select();
public void update();
}

  SomeServiceImpl类,上面那个的实现类

package cn.dawn.day19aspectj;

/**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl implements ISomeService { public void insert() {
System.out.println("insert ok");
} public void delete() {
System.out.println("delete ok"); } public void select() {
System.out.println("select ok"); } public void update() {
System.out.println("update ok");
int a=/;
System.out.println(a); }
}

  他有一个方法中模拟了除零的算数异常,方便一会测试异常增强:

  刚才我说的那个实现aspectj注解的类

package cn.dawn.day19aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; /**
* Created by Dawn on 2018/3/12.
*/
@Aspect
public class MyAspectJ { /*单独拎出来的注解,一会下面用他的方法名可以调用到他的切点表达式*/
@Pointcut(value = "execution(* *..day19aspectj.*.select(..))")
public void select(){ }
@Pointcut(value = "execution(* *..day19aspectj.*.update(..))")
public void update(){ } /*后置增强*/
@AfterReturning("execution(* *..day19aspectj.*.select(..))")
public void myAfterReturning(){
System.out.println("后置增强");
} /*前置增强*/
@Before("execution(* *..day19aspectj.*.insert(..))")
public void myBefore(){
System.out.println("前置增强");
} /*异常增强*/
@AfterThrowing("execution(* *..day19aspectj.*.update(..))")
public void myAfterThrowing(){
System.out.println("异常增强");
} /*最终增强*//*
@After("execution(* *..day19aspectj.*.update(..))||execution(* *..day19aspectj.*.select(..))")
public void myafter(){
System.out.println("我是最终增强");
}*/ /*最终增强*/
@After("update()||select()")
public void myafter(){
System.out.println("我是最终增强");
} /*环绕增强*/
@Around("execution(* *..day19aspectj.*.insert(..))")
public void myAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕增强前");
pjp.proceed();
System.out.println("环绕增强后");
}
}

  此处要说的只有一个他匹配的时候想要传入多个条件,符合任意一个就对方法增强,只需要用||逻辑或来分割,他上面我多写了一种方式,可以把切点表达式提出来,下面都可以使用,有点像提前声明,而不是用到的时候再声明的感觉

  单测方法:

package cn.dawn.day19aspectj;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180312 {
@Test
/*aop代理工厂bean异常增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day19aspectj.xml");
ISomeService service = (ISomeService) context.getBean("service"); try {
service.update();
}catch (Exception e){
e.printStackTrace();
}
service.delete();
service.select();
service.insert(); }
}

  我知道这块比较繁琐,不太好用语言解释,多练,毕竟博客的方式不如现场和视频,直播等,多练,练的时候理解