一 自定义注解
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD,ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Verify {
public String value() default "";
}
二 aspectJ 类进行增强
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class VerifyAuthority {
@Pointcut(value="@annotation(com.souche.aspect.Verify)")
public void pointCut() {
}
// 前置 方法执行前执行
@Before(value="pointCut()")
public void verifyUser(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (null == args || args.length == 0) {
return;
}
for (Object arg : args) {
if(arg ==null)
{
continue;
}
// 一 :做方法拦截
// 拿到参数进行处理校验等
System.out.println(2);
// if(arg instanceof User) {
// 业务: 验证用户是否登录,权限等
//
// //
//
//
// }
// 二 对象属性拦截
//通过反射获取class 对象
Class<? extends Object> class1 = arg.getClass();
// 包含私有字段
Field[] fields = class1.getDeclaredFields();
for (Field field : fields) {
// // 得到的是当前成员所有的注释,不包括继承的
// Annotation[] annotations = field.getDeclaredAnnotations();
// // 包括继承的所有注释
// Annotation[] annotations2 = field.getAnnotations();
// // 获取指定注解
// Verify annotation = field.getAnnotation(Verify.class);
// // 获取指定注解
// Verify annotation1 = field.getDeclaredAnnotation(Verify.class);
// // 是否是Verify注解
// boolean b = field.isAnnotationPresent(Verify.class);
// if(annotation !=null) {
// String value = annotation.value();
// System.out.println(value);
// }
// 处理自己业务逻辑
}
}
}
// 统一异常
@AfterThrowing(throwing="ex",pointcut=""//或者value="pointCut()"
)
public void throwing(Exception ex) {
// 业务逻辑
}
// 后置 方法执行后执行
@AfterReturning(value = "pointCut()")
public void afterReturning() {
// 业务逻辑
}
//环绕 方法执行前后,都会执行
@Around(value="pointCut()")
public void around() {
// 业务逻辑
}
// 最终通知
@After(value = "pointCut()")
public void after() {
// 业务逻辑
}
}
三 配置文件形式
<!-- 增强 -->
<bean id="verifyAuthority" class="cn.aop.VerifyAuthority"></bean>
<aop:config>
<aop:pointcut expression="execution(public * *(..))" id="allpointcut"/>
<aop:aspect ref="verifyAuthority">
<!-- 前置 -->
<aop:before method="before" pointcut-ref="allpointcut"/>
<!-- 后置 -->
<aop:after-returning method="afterReturning" pointcut-ref="allpointcut"/>
<!-- 环绕 -->
<aop:around method="around" pointcut-ref="allpointcut"/>
<!-- 异常抛出 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="allpointcut"/>
<!-- 最终增强 -->
<aop:after method="after" pointcut-ref="allpointcut"/>
</aop:aspect>
</aop:config>
sprint aspectj配合注解使用
最新推荐文章于 2021-11-29 16:09:08 发布