spring学习(三十九)--自定义注解

时间:2019-11-19
本文章向大家介绍spring学习(三十九)--自定义注解,主要包括spring学习(三十九)--自定义注解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文讲解如何通过拦截器interceptor实现自定义注解。

要想实现自定义注解,要先了解下面的四个注解,他们是用来开发自定义注解的。

1、@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:             

ElemenetType.CONSTRUCTOR   构造器声明             

ElemenetType.FIELD   域声明(包括 enum 实例)            

ElemenetType.LOCAL_VARIABLE   局部变量声明           

ElemenetType.METHOD   方法声明             

ElemenetType.PACKAGE   包声明             

ElemenetType.PARAMETER   参数声明             

ElemenetType.TYPE   类,接口(包括注解类型)或enum声明         

2、@Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:            

RetentionPolicy.SOURCE   注解将被编译器丢弃            

RetentionPolicy.CLASS   注解在class文件中可用,但会被VM丢弃            

RetentionPolicy.RUNTIME   VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。

3、 @Inherited:允许子类继承父类的注解。

4、Documented注释的作用及其javadoc文档生成工具的使用

下面例子用于控制类方法的调用,只有拥有某个角色时才能调用。

1、编写注解类PermissionCheckAnnotion.java:

package myAnnotions;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface PermissionCheckAnnotion {

    /**
     * 资源权限key
     */
    String resourceKey();
    
}

2、编写拦截器类PermissionCheckInterceptor.java

package myAnnotions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class PermissionCheckInterceptor extends HandlerInterceptorAdapter{

    /**
     * 前置拦截方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        //在方法上寻找注解
        PermissionCheckAnnotion annotion = handlerMethod.getMethodAnnotation(PermissionCheckAnnotion.class);
        if (null==annotion) {
            //在类上寻找注解
            annotion = handlerMethod.getBeanType().getAnnotation(PermissionCheckAnnotion.class);
        }
        System.out.println("annotion=" + annotion);
        if (null==annotion) {
            //没有找到注解
            return true;
        }
        String resourceKey = annotion.resourceKey();
        System.out.println("resourceKey=" + resourceKey);
        if (!"test".equals(resourceKey)) {
            return false;
        }
        return true;
    }
}

3、编写spring配置文件spring-myAnnotions.xml,配置包扫描和拦截器

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     
     <!-- 扫描拦截器所在的包,实例化拦截器 -->
     <context:component-scan base-package="myAnnotions" />
     
     <!-- 配置拦截器 -->
     <mvc:interceptors>  
         <mvc:interceptor>  
             <mvc:mapping path="/PermissionCheckAnnotionTest/PermissionCheckAnnotionTest"/>  
             <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->  
             <bean class="myAnnotions.PermissionCheckInterceptor"/>  
         </mvc:interceptor>  
     </mvc:interceptors>
     
</beans>

4、编写测试类PermissionCheckAnnotionTest.java:

package myAnnotions;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ResponseBody
@RequestMapping("/PermissionCheckAnnotionTest")
public class PermissionCheckAnnotionTest {

    @RequestMapping("/PermissionCheckAnnotionTest")
    @PermissionCheckAnnotion(resourceKey="test")
    public String PermissionCheckAnnotionTest(){
        return "测试自定义注解";
    }
}

5、通过tomcat启动该工程,在页面访问这个http://localhost:8088/mySpringDemo/PermissionCheckAnnotionTest/PermissionCheckAnnotionTest接口:

控制台打印:

annotion=@myAnnotions.PermissionCheckAnnotion(resourceKey=test)
resourceKey=test

说明已经进入到自定义注解。

原文地址:https://www.cnblogs.com/gllegolas/p/11891974.html