使用注解定义切面
采用注解的方式定义切面以及通知
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Demo {
String value() default "";
boolean required() default true;
}
@Aspect
@Slf4j
@Component
public class SysPlatLogAspect {
/**
* 指定切面
*/
@Pointcut("execution(public * com.rest.module..*.*(..))")
public void getMethods() {
}
/**
* 指定注解
*/
@Pointcut("@annotation(com.rest.utils.Demo )")
public void withAnnotationMethods() {
}
@Around(value = "getMethods() && withAnnotationMethods()")
public Object aroundDeleteArticle(ProceedingJoinPoint proceedingJoinPoint){
logger.debug("--------方法执行之前---------");
//打印方法所有的参数列表
Object[] args = proceedingJoinPoint.getArgs();
for (Object arg : args){
logger.debug(arg + " , ");
}
Object retValue = null;
try {
retValue = proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
logger.debug("------方法执行之后------");
return retValue;
}
/***
* 拦截控制层的操作日志
* @param joinPoint
* @return
* @throws Throwable
*/
@After(value = "getMethods() && withAnnotationMethods()")
public void recordLog(JoinPoint joinPoint) throws Throwable {
SysLog sysLog = new SysLog();
SysPlatLog sysPlatLog = getInter(joinPoint);
sysLog.setOperateName(sysPlatLog.operateName());
sysLog.setLogNote(sysPlatLog.logNote());
sysLog.setLogTime(new Date());
sysLog.setAppCode(AppSupPlatProperties.getAppCode());
}
public SysPlatLog getInter(JoinPoint joinPoint) throws ClassNotFoundException {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
SysPlatLog sysPlatLog = method.getAnnotation(SysPlatLog.class);
return sysPlatLog;
}
}
}
return null;
}
}
XML配置的方式
<bean id="dataSourceAspect" class="com.xxx.aop.DataSourceAspect"/>
<aop:config proxy-target-class="true">
<aop:aspect ref="dataSourceAspect" order="1">
<aop:pointcut id="dataSourcePointcut"
expression="(execution(* com.xx.*.service.*.*(..))) and (!execution(* com.xxx.service.*.*(..)))" />
<aop:before pointcut-ref="dataSourcePointcut" method="intercept" />
</aop:aspect>
</aop:config>
@Pointcut里面的参数形式
由下列方式来定义或者通过 &&、 ||、 !、 的方式进行组合:
execution:用于匹配方法执行的连接点;
within:用于匹配指定类型内的方法执行;
this:用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;
target:用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
args:用于匹配当前执行的方法传入的参数为指定类型的执行方法;
@within:用于匹配所以持有指定注解类型内的方法;
@target:用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;
@args:用于匹配当前执行的方法传入的参数持有指定注解的执行;
@annotation:用于匹配当前执行方法持有指定注解的方法;如@Pointcut("@annotation(Demo)")
括号中各个pattern分别表示:
修饰符匹配(modifier-pattern?)
返回值匹配(ret-type-pattern)可以为*表示任何返回值,全路径的类名等
类路径匹配(declaring-type-pattern?)
方法名匹配(name-pattern)可以指定方法名 或者 *代表所有, set* 代表以set开头的所有方法
参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“*”来表示匹配
任意类型的参数,如(String)表示匹配一个String参数的方法;(*,String) 表示匹配有两个参数的方法,第一个
参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数
异常类型匹配(throws-pattern?)
其中后面跟着“?”的是可选项
现在来看看几个例子:
execution(* *(..))
//表示匹配所有方法
execution(public * com. savage.service.UserService.*(..))
//表示匹配com.savage.server.UserService中所有的公有方法
execution(* com.savage.server..*.*(..))
//表示匹配com.savage.server包及其子包下的所有方法