工作总结——Aspect注解式切面

这两天真是头大,分了个story,判断超时,然后弹框提醒。上网查了很多的资料,总结一下自己的做法历程吧。

最先想到的是用HttpSessionBindingListener的一个监听器,于是乎我就写了一个aop。。。呵呵呵。切了一个判断session的方法,为什么这么做呢?业务需要。。。

上代码:当然啦,代码是自己回家之后重新写的,基于自己写的小东西实现的一个自定义注解式aop。

  • Auth 注解类
package org.ssm.king.utils.auth;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface Auth {

    String name() default "";
}

解释下三个注解。

  1. Retention呢是java.lang.annotation包下的一个注解类,表示这种类型的注解会被保持在哪一阶段,以下有三种阶段可供使用:
        /**
         * 注解将被编译器丢弃。
         */
        SOURCE,
    
        /**
         * 注解将被编译器记录在类文件中,但是在运行时不需要被VM保留 —— 该类型为缺省类型
         */
        CLASS,
    
        /**
         * 注解将被编译器记录在类文件中,并在运行时被VM保留,因此它们可以反射性地读取。
         *
         * @see java.lang.reflect.AnnotatedElement
         */
        RUNTIME

    三种保留策略的说明在注释中。

  2. Target是java.lang.annotation包下的注解类。其作用是说明注解的作用范围:

    public enum ElementType {
        /** Class, interface (including annotation type), or enum declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation type declaration */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE
    }

    该类型就不一一说了。常用METHOD

  3. Documented表明带有类型的注释将在缺省情况下由javadoc和类似的工具记录。这种类型应该用于注释那些注释影响其客户使用带注释的元素的类型的声明。如果一个类型声明被注释为文档化,那么它的注释将成为带注释元素的公共API的一部分。

  • AuthAspect
package org.ssm.king.utils.auth;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.ssm.king.model.User;

import java.util.ArrayList;
import java.util.List;

@Component
@Aspect
public class AuthAspect {
    //用户本身的权限
    private List<User> userList = new ArrayList();

    public List getUserPrivilege() {
        return userList;
    }

    @Pointcut("execution(* org.ssm.king.utils.auth.Auth.*(..))")
    public void authAspect() {
    }

    @Around("authAspect()")
    public Object accessMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        /** * 得到目标类的class形式 * 得到目标方法 */
        Class targetClass = joinPoint.getTarget().getClass();
        String targetMethodName = joinPoint.getSignature().getName();
        String methodAccess = AnnotationParse.parse(targetClass, targetMethodName);
        boolean flage = false;
        for (User user : userList) {
            if (methodAccess.equals(user.getId())) {
                flage = true;
                break;
            }
        }
        if (flage) {
            return joinPoint.proceed();
        } else {
            System.out.println("权限不足");
            return null;
        }
    }
}
  • AnnotaionParse
package org.ssm.king.utils.auth;

import java.lang.reflect.Method;

/**
 * 注释解析器
 */
public class AnnotationParse {

    /**
     * 解析注释
     *
     * @param targetClassName
     * @param methodName
     * @return
     * @throws Exception
     */
    public static String parse(Class targetClassName, String methodName) throws Exception {
        //获得目标方法
        Method method = targetClassName.getMethod(methodName);

        String methodAccess = "";
        //判断目标方法上面是否存在@Auth注解
        if (method.isAnnotationPresent(Auth.class)) {
            //得到方法上的注解
            Auth auth = method.getAnnotation(Auth.class);
            //得到注解中的id值
            methodAccess = auth.id();
        }
        return methodAccess;
    }
}

呃呃呃  跑偏了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值