一、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";
}
- 将注解加在测试类的类头和方法上
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");
}
}
- 使用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、运行结果
未登录时:
登录接口:
再次访问管理员接口:
三、总结
在一些很繁琐的工作中可以使用注解来解决,注解是非常好用的。
码云地址:码云