自定义注解

简介: /

1.前言
自定义注解目前在我使用过的项目中,主要用用作日志丰富,参数处理,其核心还是借助于Spring的AOP进行实现,本文将结合具体代码演示简单的自定义注解实现流程。

2.实现
2.1 定义User
2.2 定义UserDAO
2.3 定义UserService
2.4 定义Controller
此时浏览器访问:http://{domain}/user/1即可出现对应效果
2.5 定义自定义注解
说明:
@interface 不是interface,是注解类  定义注解
Documented
这个Annotation可以被写入javadoc  
@Retention
修饰注解,是注解的注解,称为元注解
SOURCE,     // 编译器处理完Annotation后不存储在class中  
CLASS,       // 编译器把Annotation存储在class中,这是默认值  
RUNTIME  // 编译器把Annotation存储在class中,可以由虚拟机读取,反射需要
@Target
注解的作用目标
@Target(ElementType.TYPE)                                           //接口、类、枚举、注解
@Target(ElementType.FIELD)                                         //字段、枚举的常量
@Target(ElementType.METHOD)                                   //方法
@Target(ElementType.PARAMETER)                              //方法参数
@Target(ElementType.CONSTRUCTOR)                        //构造函数
@Target(ElementType.LOCAL_VARIABLE)                     //局部变量
@Target(ElementType.ANNOTATION_TYPE)                //注解
@Target(ElementType.PACKAGE)                                 //包    

可以定义多个方法,每个方法在使用时参照下面的Controller使用即可,实际就是类似于@PostMapping这样的注解中使用过的value,method,produces等,如下:

@TargeT(LeeTypeMTHOD)

@Retention(RetentionPolicy.RUNTIMe)

@Documented

@ReguestMapping(methodRequesMethodP)

public@interfacePostMapping

ALiaSFor(annotationRequestMapping.class)

stringname)default"";

ALiaSFor(annotationRequestMapping.class)

stringIvalue)default0;

CAliaSFor(annotationRequesTMapping.class)

stringI]patho)defaultf;

@ALiaSFor(annotationRequestMapping.class)

stringIjparams)defaultou;

@ALiSFor(annotationRequestMapping.class)

string[jheaders)defaultl;

ALiASFor(annotationRequestMapping.class)

stringIconsumeso)defauttf;

@ALiaSFor(annotationRequestMapping.class)

stringljproduceso)defaulto;


2.6 AOP+Controller使用自定义注解
3.总结
自定义注解其核心是借助于:@Target 和 @Rentention,@Documented组合实现,其实现还是需要依赖于Spring的AOP进行具体体现,除了上面的用作日志拦截,还可以自定义:数据验证注解,权限注解,缓存注解等多种用途,但其实现基本都遵循上述步骤。
4.自定义注解+过滤器实现登陆相关
4.1 定义自定义注解@Login
4.2 过滤器匹配


package com.zhicall.majordomo.core.security.interceptor;


import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.multipart.MultipartResolver;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import com.alibaba.fastjson.JSON;

import com.zhicall.care.realtime.util.ResultMessageBuilder;

import com.zhicall.care.realtime.util.ResultMessageBuilder.ResultMessage;

import com.zhicall.care.system.basic.BeanFactory;

import com.zhicall.majordomo.core.common.constant.GlobalCst;

import com.zhicall.majordomo.core.common.enums.YesOrNo;

import com.zhicall.majordomo.core.security.annotation.Login;

import com.zhicall.majordomo.core.security.constant.Cst;

import com.zhicall.majordomo.core.security.util.UserAuthHelper;


public class UserLoginInterceptor extends HandlerInterceptorAdapter {


@SuppressWarnings({ "unchecked", "rawtypes" })

protected RedisTemplate<String, String> redisTemplate = (RedisTemplate) BeanFactory.getInstance().getBean("redisTemplate");


@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

HandlerMethod handlerMethod = (HandlerMethod) handler;

Login login = handlerMethod.getMethodAnnotation(Login.class);

// 方法被 @Login(YesOrNo.No)标记 表示不需要登陆即可访问 否者都要登录

if (login != null && YesOrNo.NO.equals(login.value())) {

return true;

}

// 做鉴权

       ......

}

}

4.3 Controller中具体使用

@Login(YesOrNo.NO)

@RequestMapping(value = "/filter", method = RequestMethod.POST)

public @ResponseBody ResultMessageBuilder.ResultMessage filter(String companyId, String code) {

List<TabInfoVo> merchantsInfoDtos = new ArrayList<>();

merchantsInfoDtos = historyTradeService.filter(companyId, code);

return ok("查询成功", merchantsInfoDtos);

}

若有收获,就点个赞吧


相关文章
|
自然语言处理 编译器 Linux
【Latex】texstudio使用和ACL论文模板初步解读
LaTeX是一类用于编辑和排版的软件,用于生成PDF文档。 LaTeX编辑和排版的核心思想在于,通过\section和\paragraph等语句,规定了每一句话在文章中所从属的层次,从而极大方便了对各个层次批量处理。 LaTeX在使用体验方
2487 0
【Latex】texstudio使用和ACL论文模板初步解读
|
2月前
|
机器学习/深度学习 人工智能 自然语言处理
|
Ubuntu Java Linux
Dockerfile语法,自定义镜像
Dockerfile语法,自定义镜像
241 1
|
缓存 Linux 网络安全
docker的镜像无法下载如何解决?
【10月更文挑战第31天】docker的镜像无法下载如何解决?
8025 30
|
缓存 Linux Docker
【最新版正确姿势】Docker安装教程(简单几步即可完成)
之前的老版本Docker安装教程已经发生了变化,本文分享了Docker最新版安装教程,其他操作系统版本也可以参考官 方的其他安装版本文档。
11863 3
【最新版正确姿势】Docker安装教程(简单几步即可完成)
|
开发框架 JavaScript 前端开发
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势。通过明确的类型定义,TypeScript 能够在编码阶段发现潜在错误,提高代码质量;支持组件的清晰定义与复用,增强代码的可维护性;与 React、Vue 等框架结合,提供更佳的开发体验;适用于大型项目,优化代码结构和性能。随着 Web 技术的发展,TypeScript 的应用前景广阔,将继续引领 Web 开发的新趋势。
427 2
|
存储 Ubuntu Java
【Linux】已解决:Ubuntu虚拟机安装Java/JDK
【Linux】已解决:Ubuntu虚拟机安装Java/JDK
1077 1
|
Linux
CentOS yum源设置为国内aliyun yum源
CentOS yum源设置为国内aliyun yum源
10121 0
required a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ that could not be found.
required a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ that could not be found.
754 0
IDEA2022中一个项目同时运行多个实例
1.场景 我们在进行网络开发时,有时候需要测试多个客户端对服务端的链接状态,这时就需要在一个项目中运行多个实例,IDEA是默认不运行运行多个实例的,下面给出方法🙌
707 0
IDEA2022中一个项目同时运行多个实例