注解简单来说就是配置,是特别的配置,之前常用的配置文件,可以用注解替换。然后通过反射去获取注解的信息。

如何定义一个注解

你在IDE中新建一个注解定义,是这样的结构的:

package com.nicchagil.exercise.springbootexercise.annotation;

public @interface MyFirstAnnotation {

}

然后大概有4个对上述结构加上一些配置,当然,这配置是以注解的形式添加的=_=!

此注解使用在哪里

此注解会应用的哪里,可通过如下配置:

保留在什么时候

保留到什么时候:

注解体现在文档中

@Documented

子类是否继承父类的注解

@Inherited

用反射获取注解的信息

我们先定义一个注解:

package com.nicchagil.exercise.springbootexercise.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PojoPostProcessing { public Class targetClass(); public String[] whiteProperties(); }

使用注解,比如这里我设置到一个方法上:

	@PojoPostProcessing(targetClass=User.class, whiteProperties={"name"})
public List<User> selectXxx(String id) {
......
}

反射获取注解的信息:

	@Test
public void test() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
Class clazz = Class.forName("com.nicchagil.exercise.springbootexercise.service.UserService"); Method method = clazz.getMethod("selectXxx", String.class); boolean isAnnotationPresent = method.isAnnotationPresent(PojoPostProcessing.class);
if (!isAnnotationPresent) {
return;
} PojoPostProcessing dpp = (PojoPostProcessing)method.getAnnotation(PojoPostProcessing.class);
this.logger.info("dpp : {}", dpp);
}

日志:

dpp : @com.nicchagil.exercise.springbootexercise.annotation.PojoPostProcessing(targetClass=class com.nicchagil.exercise.springbootexercise.mapper.entity.User, whiteProperties=[name])

在Spring AOP中解析注解,然后实体你注解需要做的业务

比如以下就是在Spring AOP中拦截到方法,通过JoinPoint获取反射的Method,然后看是否有指定注解,如果有注解,后面就是关于此注解要做的业务逻辑了:

package com.nicchagil.exercise.springbootexercise.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; import com.nicchagil.exercise.springbootexercise.annotation.PojoPostProcessing; @Aspect
@Configuration
public class ServiceControllerLogAop { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Pointcut("execution(* com.nicchagil.exercise.springbootexercise.service.*Service.*(..))")
public void myPointcut() {
} @AfterReturning("myPointcut()")
public void myBefore(JoinPoint joinPoint) {
/* 获取方法对象 */
Signature signature = joinPoint.getSignature();
if (signature == null && !(signature instanceof MethodSignature)) {
return;
} MethodSignature methodSignature = (MethodSignature)signature;
Method method = methodSignature.getMethod(); /* 是否有指定注解 */
boolean isAnnotationPresent = method.isAnnotationPresent(PojoPostProcessing.class);
if (!isAnnotationPresent) {
return;
} /* 获取注解信息 */
PojoPostProcessing dpp = (PojoPostProcessing)method.getAnnotation(PojoPostProcessing.class);
this.logger.info("dpp : {}", dpp);
} }

过滤注解方法的返回值的指定属性

将属性置空的工具类:

package com.nicchagil.exercise.springbootexercise.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.MethodParameter; public class BeanClearUtils { private static Logger logger = LoggerFactory.getLogger(BeanClearUtils.class); public static void clearProperty(Object obj, String[] whiteProperty) {
List<PropertyDescriptor> clearList = BeanClearUtils.getClearProperty(obj.getClass(), whiteProperty); try {
for (PropertyDescriptor pd : clearList) {
MethodParameter mp = BeanUtils.getWriteMethodParameter(pd);
Method method = mp.getMethod();
method.invoke(obj, new Object[] {null});
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException("设置空属性失败", e);
}
} /**
* 获取需要清除的字段列表
*/
public static List<PropertyDescriptor> getClearProperty(Class<?> clazz, String[] whiteProperty) {
PropertyDescriptor[] allPropertyDescriptor = BeanUtils.getPropertyDescriptors(clazz); /* 需保留的字段 */
Set<String> whitePropertySet = new HashSet<String>(Arrays.asList(whiteProperty));
whitePropertySet.add("class"); // 此字段无须清除 /* 需清除的字段 */
List<PropertyDescriptor> clearList = new ArrayList<PropertyDescriptor>();
for (PropertyDescriptor propertyDescriptor : allPropertyDescriptor) {
if (!whitePropertySet.contains(propertyDescriptor.getName())) {
clearList.add(propertyDescriptor);
}
}
return clearList;
} }

定义注解:

package com.nicchagil.exercise.springbootexercise.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PojoPostProcessing { public String[] whiteProperties(); }

处理注解逻辑:

package com.nicchagil.exercise.springbootexercise.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; import com.nicchagil.exercise.springbootexercise.annotation.PojoPostProcessing;
import com.nicchagil.exercise.springbootexercise.util.BeanClearUtils; @Aspect
@Configuration
public class BeanPropertyFilterAop { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Pointcut("execution(* com.nicchagil.exercise.springbootexercise.service.*Service.*(..))")
public void myPojoPostProcessingPointcut() {
} @AfterReturning(value = "myPojoPostProcessingPointcut()", returning = "returnObject")
public void myAfter(JoinPoint joinPoint, Object returnObject) {
/* 获取方法对象 */
Signature signature = joinPoint.getSignature();
if (signature == null && !(signature instanceof MethodSignature)) {
return;
} MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod(); /* 是否有指定注解 */
boolean isAnnotationPresent = method.isAnnotationPresent(PojoPostProcessing.class);
if (!isAnnotationPresent) {
return;
} /* 获取注解信息 */
PojoPostProcessing dpp = (PojoPostProcessing) method.getAnnotation(PojoPostProcessing.class);
this.logger.info("dpp : {}", dpp); /* 只保留bean的指定属性值(其它属性值置空) */
logger.info("before bean : {}", returnObject);
BeanClearUtils.clearProperty(returnObject, dpp.whiteProperties());
logger.info("after bean : {}", returnObject);
} }

使用注解:

@PojoPostProcessing(whiteProperties={"name", "createTime"})

【Java基础】Java注解简单入门的更多相关文章

  1. Java基础-Java中23种设计模式之常用的设计模式

    Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...

  2. Java基础-Java数据类型

    Java基础-Java数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...

  3. java基础---->java中正则表达式二

    跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...

  4. Java基础-Java中的堆内存和离堆内存机制

    Java基础-Java中的堆内存和离堆内存机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  5. Java基础-Java中的内存分配与回收机制

    Java基础-Java中的内存分配与回收机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.

  6. Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)

    Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在学习Java的之前,你可能已经听说过读 ...

  7. Java基础-Java中的并法库之线程池技术

    Java基础-Java中的并法库之线程池技术 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是线程池技术 二.

  8. Java基础-JAVA中常见的数据结构介绍

    Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...

  9. 【转载】JAVA基础:注解

    原文:https://www.cnblogs.com/xdp-gacl/p/3622275.html#undefined 一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的 ...

随机推荐

  1. 英语口语练习系列-C25-冒险-课堂用语-葬我

    词汇-冒险 adventure noun [ C or U ] UK /ədˈven.tʃər/ US /ədˈven.tʃɚ/ an unusual, exciting, and possibly ...

  2. 【PYTHON】 Missing parentheses in call to 'print'

    Microsoft Windows [版本 10.0.15063] (c) 2017 Microsoft Corporation.保留所有权利. C:\Users\Jam>python Pyth ...

  3. 【DWM1000】 code 解密2一 工程初始化代码分析

    instance_init 函数追下去,绝大多数的代码都在初始化如下结构体 typedef struct { INST_MODE mode; instance_init -ANCHOR //insta ...

  4. [AMPPZ2014]Jaskinia

    [AMPPZ2014]Jaskinia 题目大意: 一个\(n(n\le3\times10^5)\)的树,\(m(m\le3\times10^5)\)个约束条件\((a_i,b_i,d_i)\).请你 ...

  5. React动画组件——React-Transitio-group动画实现

    React动画组件--React-Transitio-group动画实现 安装 项目目录下使用命令行 yarn add react-transition-group 安装组件.在需要使用动画的页面加入 ...

  6. C++程序设计方法5:接口设计实例

    //例子:旋转方阵填充,要求矩阵大小任意,尺寸在运行时输入 设计思路:从上到下 int main() { cout << "Please input N:"; int ...

  7. js间隔一段时间打印数据库中的值

    https://blog.csdn.net/qq_33599109/article/details/78417474

  8. websocket是什么

    websocket是什么? 这是知乎上面一个作者写的一篇风趣幽默又容易理解关于 websocket 的文章. 提供一下连接地址:https://www.zhihu.com/question/20215 ...

  9. go test :Wrong test signature

    err: Wrong test signature 写测试函数出了个错,找半天才找到原因:虽然很愚蠢,还是记录下. 是因为 参数没加*导致的.

  10. 解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。

    本文是原创文章,未经允许,请勿转载. 原来程序中是通过运行exe,然后加上参数,然程序运行起来的.也就是main(int argc, char** argv)这里是通过argv参数是从命令行 传过来的 ...