java注解开发--springmvc拦截器

本文介绍了Java注解开发,包括内置注解、自定义注解及其高级应用,如@Target、@Retention、@Document和@Inherited。接着详细讲解了SpringMVC中的注解拦截器,从定义注解、在Controller中使用,到实现HandlerInterceptor、注册拦截器,并展示了未登录和登录后的运行结果。最后进行了总结,强调注解在简化繁琐工作中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、java 注解开发

1、定义

  注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
  java总共有三种内置注解:

  • @Override,它的作用是对覆盖超类中方法的方法进行标记,如果被标记的方法并没有实际覆盖超类中的方法,则编译器会发出错误警告。
  • @Deprecated,它的作用是对不应该再使用的方法添加注解,当编程人员使用这些方法时,将会在编译时显示提示信息,它与javadoc里的@deprecated标记有相同的功能
  • @SuppressWarnings, 关闭不当编译器警告信息

2、自定义注解

1、申明一个@interface类文件,它有点像接口定义,在注解内可以添加变量,使用default可以默认变量的值:
这里写图片描述
2、使用自定义注解:
这里写图片描述
3、注解可以添加值:
这里写图片描述

3、高级注解应用

1、@Target 注解,用来表示注解的使用范围:

public enum ElementType {
    /** 表示类,接口,枚举,不能是注解 */
    TYPE,
    /** 字段上,包括枚举常量 */
    FIELD,
    /** 方法上 */
    METHOD,
    /** 方法的参数上 */
    PARAMETER,
    /** 构造函数上 */
    CONSTRUCTOR,
    /** 本地变量 */
    LOCAL_VARIABLE,
    /** 注解类型 */
    ANNOTATION_TYPE,
    /** java包上 */
    PACKAGE,
    /**
     * 类型参数声明
     * 1.8以后可用
     */
    TYPE_PARAMETER,
    /**
     * 类型使用
     *1.8以后可用
     */
    TYPE_USE
}

2、@Retention 注解,表示需要在什么级别保存该注解信息

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     * 注解将被编译器丢弃
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 注释将由编译器记录在类文件中
     *但在运行时不需要由VM保留。 这是默认值行为。
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *注释将由编译器记录在类文件中,在运行时由VM保留,因此可以反射性地读取它们。
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

RUNTIME是比较常用的
3.@Document 表示将注解记录在文档中
这里写图片描述
4.@Inherited 表示允许子类继承父类中的注解
这里写图片描述

4、读取注解中的值

1.首先定义一个注解:
Intercept.java

package com.demo.annotation;

import java.lang.annotation.*;

/**
 * @author xyd
 * @version V1.0
 * @Package com.demo.common
 * @Description:
 * @date 2018/8/7 14:32
 */
@Target({ElementType.METHOD,ElementType.TYPE}) // 可以使用在方法和类头上
@Retention(RetentionPolicy.RUNTIME) // 运行时级别
@Inherited  // 子类可以继承
@Documented // 注解记录在文档中
public @interface Intercept {
    String value() default "guest";
}
  1. 将注解加在测试类的类头和方法上
package com.demo.controller;

import com.demo.annotation.Intercept;

/**
 * @author xyd
 * @version V1.0
 * @Package com.demo.controller
 * @Description:
 * @date 2018/8/7 15:21
 */
@Intercept(value = "Class")
public class test {

    @Intercept(value = "Method")
    public void method(){
        System.out.println("method");
    }
}
  1. 使用Main函数来进行测试
public static void main(String[] args) throws ClassNotFoundException {
        // 获得test类的类对象
        Class<?> clazz = Class.forName("com.kuyuntech.demo.controller.Test");
        // 获得类的注解的值
        Intercept intercept = clazz.getAnnotation(Intercept.class);
        System.out.println("类注解:" + intercept.value());
        // 获取所有声明的方法
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods){
            Intercept intercept1 = method.getAnnotation(Intercept.class);
            // 如果包含该注解
            if (intercept1 != null){
                System.out.println("方法注解:" + intercept1.value());
            }
        }
    }

5、结果

这里写图片描述

二、springmvc注解拦截器

由于springboot搭建框架很快,所以使用springboot来进行demo

1、首先,定义注解

Intercept.java

package com.demo.annotation;

import java.lang.annotation.*;

/**
 * @author xyd
 * @version V1.0
 * @Package com.demo.common
 * @Description:
 * @date 2018/8/7 14:32
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Intercept {
    String value() default "guest";    //默认是游客
}

2、在controller类中加入注解

package com.demo.controller;

import com.demo.annotation.Intercept;
import com.demo.common.BusinessConstant;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


/**
 * @author xyd
 * @version V1.0
 * @Package com.demo.common
 * @Description:
 * @date 2018/8/7 14:32
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @Intercept(value = BusinessConstant.LOGIN_MANAGER_SESSION_KEY)
    @GetMapping("/get")
    public String get(){
        String value = "管理员权限";
        return value;
    }

    @Intercept
    @GetMapping("/login")
    public String login(HttpSession session){
        String value = "普通用户权限";
        session.setAttribute(BusinessConstant.LOGIN_MANAGER_SESSION_KEY, "LOGIN_MANAGER_SESSION_KEY");
        return value;
    }
}

3、实现HandlerInterceptor来编写拦截器

InterceptorConfig.java

package com.demo.filter;

import com.alibaba.fastjson.JSONObject;
import com.demo.annotation.Intercept;
import com.demo.common.BusinessConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * @author xyd
 * @version V1.0
 * @Package com.demo.common
 * @Description:
 * @date 2018/8/7 14:32
 */
public class InterceptorConfig implements HandlerInterceptor {
    private Logger logger = LoggerFactory.getLogger(InterceptorConfig.class);
    private static final Logger log = LoggerFactory.getLogger(InterceptorConfig.class);

    /**
     * 进入controller层之前拦截请求
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession session = httpServletRequest.getSession();
        // 判断是否访问springmvc方法
        if(o.getClass().isAssignableFrom(HandlerMethod.class)) {
            // 先获取请求类的类型
            Class<?> clazz = ((HandlerMethod) o).getBeanType();
            // 类头是否标注该注解
            Intercept interceptType = clazz.getAnnotation(Intercept.class);
            if (interceptType != null){
                // 进入判断
                return handle(interceptType, session, httpServletResponse);
            }
            // 如果没有类头注解,查看方法注解
            Intercept intercept = ((HandlerMethod)o).getMethodAnnotation(Intercept.class);
            if (intercept != null){
                return handle(intercept, session, httpServletResponse);
            }
        }
        sendErrorMessage(httpServletResponse, "无权限");
        return false;
    }


    private boolean handle(Intercept intercept, HttpSession session, HttpServletResponse httpServletResponse) throws Exception {
        if (intercept != null){
            // 如果是管理员权限,则通过
            if (intercept.value().equals((String)session.getAttribute(BusinessConstant.LOGIN_MANAGER_SESSION_KEY))){
                return true;
                // 如果访问的方法是游客可访问的,通过
            }else if (intercept.value().equals("guest")){
                return true;
            } else {
                sendErrorMessage(httpServletResponse, "无权限");
                return false;
            }

        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        logger.info("--------------处理请求完成后视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        logger.info("---------------视图渲染之后的操作-------------------------0");
    }

    private void sendErrorMessage(HttpServletResponse response, String message) throws Exception {

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(message);
        out.close();
    }
}

4、 将拦截器注册到springmvc中

WebAppConfig.java

package com.demo.config;

import com.demo.filter.InterceptorConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author xyd
 * @version V1.0
 * @Package com.demo.common
 * @Description:
 * @date 2018/8/7 14:32
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //注册自定义拦截器,添加拦截路径和排除拦截路径
        registry.addInterceptor(new InterceptorConfig());

    }

}

5、启动类

AnnotationInterceptApplication.java

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AnnotationInterceptApplication {

    public static void main(String[] args) {
        SpringApplication.run(AnnotationInterceptApplication.class, args);
    }
}

6、整体目录

这里写图片描述

7、运行结果

未登录时:
这里写图片描述
登录接口:
这里写图片描述
再次访问管理员接口:
这里写图片描述

三、总结

在一些很繁琐的工作中可以使用注解来解决,注解是非常好用的。

码云地址:码云

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值