「java注解获取值」java 获取注解
今天给各位分享java注解获取值的知识,其中也会对java 获取注解进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java获取注解的值
- 2、java 注解处理器(AbstractProcessor) 获取到 指定注解的属性值 javapoet 如何使用这个值生成类?
- 3、java@value注解为什么直接取默认值
- 4、Java 注解的读取注解信息的方法
- 5、java获取当前类上的注解内容
java获取注解的值
很多朋友都想知道java怎么获取注解的值?下面就一起来了解一下吧~
1、定义一个注解,用于给全局变量field字段赋值
package com.haha.study.annotation.value; 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; /** * description: 定义一个注解,用于给 全局变量 field 字段 赋值,并使用反射取值。
* 特别提醒: @Rentention(RetentionPolicy.RUNTIME) 时,注解才会被jvm加载,才能使用反射获取。 * @version v1.0 * @author w * @date 2018年8月1日下午2:37:40 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value=ElementType.FIELD) public @interface Fields { int sort() default 0 ; String value() ; }
2、创建一个普通的类,使用 @ConsAnnotation、@Fields 注解
package com.haha.study.annotation.value; /** * description: 创建一个普通的类,使用 @ConsAnnotation、@Fields 注解。 * @version v1.0 * @author w * @date 2018年8月1日下午2:50:23 */ @ConsAnnotation(request = { "hello","world","annotation!" }) public class User { @Fields("中华人民共和国") private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
3、针对 com.haha.study.annotation.value.User 类使用注解的测试
package com.haha.study.annotation.value; import java.lang.reflect.Field; import java.util.Arrays; /** * description: 针对 com.haha.study.annotation.value.User 类使用注解的测试 * @version v1.0 * @author w * @date 2018年8月1日下午2:37:13 */ public class ValueTest { public static void main(String[] args) throws Exception { User user = new User(); // 1、 获取 User类上的注解 @ConsAnnotation ConsAnnotation anno = user.getClass().getAnnotation(ConsAnnotation.class); String[] arr = anno.request(); System.out.println(Arrays.toString(arr)); // [hello, world, annotation!] // 2、 获取User类中 private String userName; 变量上的注解 @Field Field f = user.getClass().getDeclaredField("userName"); Fields anno2 = f.getAnnotation(Fields.class); user.setUserName(anno2.value()); System.out.println(user.getUserName()); // 中华人民共和国 } }
java 注解处理器(AbstractProcessor) 获取到 指定注解的属性值 javapoet 如何使用这个值生成类?
定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
java@value注解为什么直接取默认值
在SpringBoot项目读取配置文件中读取值,我们会用到@Value注解来读取配置值,例如我们在配置文件中配置了服务器web域名为xxx.com的配置:
server.web.domain=xxx.com
1
1
在代码中读取其配置项:
@Value("${server.web.domain}")
private String domain;
1
2
1
2
如果这个配置项在配置文件中忘记配置或者有的场景中我们不需要配置这项的时候,在项目启动的时候就会报错。
报错信息如下:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'server.web.domain' in value "${server.web.domain}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:918)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1248)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
... 18 common frames omitted
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
这个时候就需要我们给@Value注解配置项给个默认值。
解决方法如下:
@Value("${server.web.domain:#{null}}")
private String domain;
1
2
1
2
或者
@Value("${server.web.domain:xxx}")
private String domain;
1
2
1
2
不过如果默认值我们要设置为null时,我们使用${server.web.domain:null}时,拿到domain的默认值会是“null" null的字符串,所以这种情况下,我们使用 ${server.web.domain:#{null}} 这种方式进行赋予默认值
Java 注解的读取注解信息的方法
属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度
注意:要想使用反射去读取注解,必须将Retention的值选为Runtime Java代码import java.lang.annotation.Annotation;import java.lang.reflect.Method;//读取注解信息public class ReadAnnotationInfoTest { public static void main(String[] args) throws Exception { // 测试AnnotationTest类,得到此类的类对象 Class c = Class.forName(com.iwtxokhtd.annotation.AnnotationTest); // 获取该类所有声明的方法 Method[] methods = c.getDeclaredMethods(); // 声明注解集合 Annotation[] annotations; // 遍历所有的方法得到各方法上面的注解信息 for (Method method : methods) { // 获取每个方法上面所声明的所有注解信息 annotations = method.getDeclaredAnnotations(); // 再遍历所有的注解,打印其基本信息 System.out.println(method.getName()); for (Annotation an : annotations) { System.out.println(方法名为: + method.getName() + 其上面的注解为: + an.annotationType().getSimpleName()); Method[] meths = an.annotationType().getDeclaredMethods(); // 遍历每个注解的所有变量 for (Method meth : meths) { System.out.println(注解的变量名为: + meth.getName()); } } } }}
java获取当前类上的注解内容
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Target({ElementType.FIELD,ElementType.METHOD})//定义注解的作用目标**作用范围字段、枚举的常量/方法
@Documented//说明该注解将被包含在javadoc中
public @interface FieldMeta {
/**
* 是否为序列号
* @return
*/
boolean id() default false;
/**
* 字段名称
* @return
*/
String name() default "";
/**
* 是否可编辑
* @return
*/
boolean editable() default true;
/**
关于java注解获取值和java 获取注解的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。