【Java基础】Java注解简单入门
2017-12-30 21:29
489 查看
注解简单来说就是配置,是特别的配置,之前常用的配置文件,可以用注解替换。然后通过反射去获取注解的信息。
然后大概有4个对上述结构加上一些配置,当然,这配置是以注解的形式添加的=_=!
使用注解,比如这里我设置到一个方法上:
反射获取注解的信息:
日志:
定义注解:
处理注解逻辑:
使用注解:
如何定义一个注解
你在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 基础总结--注解(Annotation)自定义注解入门
- java IO文件操作简单基础入门例子,IO流其实没那么难
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
- java注解简单入门
- JAVA之Mybatis基础入门--框架搭建与简单查询
- java入门基础及简单登录界面的实现
- 【Java 基础】Java 泛型简单入门
- java基础入门-建立简单的ServerSocket
- 【Java基础知识】Annotation02_注解(Annotation)自定义注解入门
- java基础加强--注解的入门与使用
- 【javaJDBC基础】【简单的入门语句代码】
- java基础之注解(annotation)——实现一个简单的ButterKnife
- 【JAVA零基础入门系列】Day12 Java类的简单应用
- java基础入门-建立简单的ServerSocket
- java基础强化——深入理解java注解(附简单ORM功能实现)
- JavaWeb基础—dbutils的简单入门
- 简单的入门Android开发和Java语言基础[图]
- java IO文件操作简单基础入门例子,IO流其实没那么难
- Java基础学习总结(46)——JAVA注解快速入门
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)