-
定义切面类
-
切面类进行增强的两种写法
-
切点匹配注解和规则
切面类定义
-
引入相应的aop包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency> -
定义切面类(类名自定义)
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class DatabaseResourcetoggle {
}
切入点定义注解说明
@pointcut :用来定义一个切面(切入点),即所关注的某件事情的入口。切入点决定了连接点关注的内容,使得我们可以控制通知什么时候执行。
切入点表达式说明
先说明两个通配符:*、..
-
*:主要用于匹配单个单词,或者是以某个词为前缀或后缀的单词
-
..:表示0个或多个项。主要用于类全路径或者参数,用于类全路径中表示匹配当前包及其子包,如果用于参数中,则表示匹配0个或多个参
1、匹配使用自定义注解的方法:
@pointcut("@annotation()")
@pointcut("@within()")
@pointcut("@args()")
@pointcut("@target()")
2、匹配对象下的方法:
@pointcut("this()")
@pointcut("bean()")
@pointcut("target()")
3、匹配方法和参数:
@pointcut("within()")
@pointcut("args()")
@pointcut("execution()")
@pointcut("@annotation(com.gzt.annotation.DatabaseResourcedefine)"):匹配方法上有DatabaseResourcedefine注解的方法
@pointcut("@within(com.gzt.annotation.DatabaseResourcedefine)"):匹配有DatabaseResourcedefine注解标记的类里面的方法(最常用)
@pointcut("@args(com.gzt.annotation.DatabaseResourcedefine)"):匹配传入的参数有DatabaseResourcedefine注解标记的方法
@pointcut("@target(com.gzt.annotation.DatabaseResourcedefine)"):匹配有DatabaseResourcedefine注解标记的类及其子类里面的方法
@pointcut("this(com.gzt.annotation.DatabaseResourcedefine)"):匹配AOP对象的目标对象为指定类型的方法
@pointcut("target(com.gzt.annotation.DatabaseResourcedefine)"):匹配实现DatabaseResourcedefine接口的目标对象里面的方法
@pointcut("bean(DatabaseResourcedefine)"):匹配指定的bean
@pointcut("within(com.gzt.annotation.DatabaseResourcedefine)"):匹配指定包下的方法
@pointcut("args(Long,..)"):匹配任何以Long参数开头的方法(一般搭配其他匹配条件使用)
@pointcut("execution(* com.gzt.service.impltestImpl.*(..))"):匹配某个方法(最常用)
@within、execution两个一般最常用,使用@within+自定义注解,可以很好的对某一个类下面的方法进行增强,execution用来匹配单个的
增强方式
@afterReturning(返回后通知): 在方法返回后执行
@afterThrowing(异常通知): 在抛出异常时执行
例子:
public class DatabaseResourcetoggle {
final Logger logger = LoggerFactory.getLogger(getClass());
@pointcut("execution(* com.gzt.service.impltestImpl.*(..))")
//@pointcut("@within(com.gzt.annotation.DatabaseResourcedefine)")
public void pointcut() {}
@Before(value = "pointcut()")
public void before1(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
System.out.println(arg.toString());
}
Object target = joinPoint.getTarget();
System.out.println(target.getClass().getTypeName());
Object target1 = joinPoint.getTarget();
DatabaseResourcedefine annotation = target1.getClass().getAnnotation(DatabaseResourcedefine.class);
String value = annotation.value();
System.out.println("value:"+value);
DatabaseAdapt.flag = value;
logger.info(value);
}
//使用前置通知或者是环绕通知
//@Before("@annotation(databaseResourcedefine) || @within(databaseResourcedefine)")
//public void before(JoinPoint joinPoint, DatabaseResourcedefine databaseResourcedefine) {
// String value = databaseResourcedefine.value();
// DatabaseAdapt.flag = value;
// logger.info(value);
//}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。