Reflections 扫描并获取自定义注解

整理代码的时候发现项目中spring的拦截器 在定义不拦截路径的时候过于臃肿~
如下:实在乱的不行。

if ((uri.indexOf("gainedTitle") != -1)||(uri.indexOf("shareGame") != -1)||(uri.indexOf("getUserType") != -1)
        ||(uri.indexOf("wechatGetUserId") != -1)  || (uri.indexOf("temporary") != -1) || (uri.indexOf("gameCustomerController") != -1)
        ||(uri.indexOf("method=jumpPageBase") != -1)||(uri.indexOf("method=wxjumpPageBase") != -1)|| (uri.indexOf("activityController") != -1) 
        ||(uri.indexOf("activityDrawApiController") != -1)||(uri.indexOf("smsGetCode") != -1)
        ||((uri.indexOf("KnowledgeAnswerController") != -1)&& (uri.indexOf("userCode")!=-1))||(uri.indexOf("gameListInfo")!=-1)
        ||(uri.indexOf("activityAnswerApiController")!=-1)||(uri.indexOf("activityAnswerController")!=-1)
        ||(uri.indexOf("activityAnswerController")!=-1)||(uri.indexOf("answerApiController")!=-1)){
        super.preHandle(request, response, handler);
}else if(uri.contains("actTrainController") || uri.contains("atcTrainApiController")){
        return true;
}else{
        logger.info("拦截器获取userId-----------------------");          
        response.sendRedirect(pro.getProperty("base_path")+"/wechatGetUserId.do?requestUrl="+uri);
        return false;
}

跟同事讨论后决定 使用自定义的注解来改善代码,也便于日后的开发和维护.

一.定义注解如下:
name 就是放开的路径

**
 * @Author: mark
 * @Description:
 * @Date: Created in 11:48 2018/5/22
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface InterceptorPassController {

    public String name() default "";

}

二.这里我用到了Reflections类用来扫面注解.pom文件加入依赖:

<dependency>
    <groupId>org.reflectionsgroupId>
    <artifactId>reflectionsartifactId>
    <version>0.9.10version>
dependency>

三.下面就扫描并且获取有注解的类的(也就是不拦截的controller)

/**
     * 根据@InterceptorPassController 获取不拦截路径
     */
    public static void getNotInterceptor(){
        List notInterceptorList = new ArrayList();
        //不拦截的@RequestMapping("") 路径
        ConfigurationBuilder config = new ConfigurationBuilder();
        config.filterInputsBy(new FilterBuilder().includePackage("com.gcol.qy.web.system.api").includePackage("com.gcol.qy.web.system.controller"));
        config.addUrls(ClasspathHelper.forPackage("com.gcol.qy.web.system"));
        config.setScanners(new TypeAnnotationsScanner(),new SubTypesScanner());
        Reflections reflections = new Reflections(config);

        Set> typesAnnotatedWith = reflections.getTypesAnnotatedWith(InterceptorPassController.class);
        for (Class el : typesAnnotatedWith){
            //取到标签对象
            InterceptorPassController annotation = el.getAnnotation(InterceptorPassController.class);
            notInterceptorList.add(annotation.name());
        }
    }

例如:我想放开2个controller

@RequestMapping(value = "/activityAnswerApiController")
@Controller
@InterceptorPassController(name = "activityAnswerApiController")
@Controller
@RequestMapping("/answerApiController")
@InterceptorPassController(name = "answerApiController")

通过getNotInterceptor();方法就能获取到所有注解name的集合(可以放在静态代码块中去调用加载 ):
[activityAnswerApiController , answerApiController]
这样就可以去拦截器里 去进行判断了。

有建议的小伙伴 +qq 511831389

你可能感兴趣的:(Reflections 扫描并获取自定义注解)