简体   繁体   English

如何使用注释从文件中为字段添加值?

[英]How to add value to field from file by using annotation?

I'm using maven project and i need to keep info in file in project it can be.properties/.config file I don't care but i need to have initalized field in this file like:我正在使用 maven 项目,我需要将信息保存在项目中的文件中,它可以是 .properties/.config 文件,我不在乎,但我需要在这个文件中有初始化字段,例如:

test.properties or test.config
test.name = Test

And next i want to use this value to initialize field in one of my method:接下来我想使用这个值在我的一种方法中初始化字段:

 public String sendName()  {
        @Value("${test.name}")
        String name;
        return name;
      }

but lombok annotation doesn't work, I'm using maven project it isn't Spring project, I know that one of the option is scan this file and take value who i want, but I hope that is another simpler option:)但是 lombok 注释不起作用,我使用的是 maven 项目,它不是 Spring 项目,我知道其中一个选项是扫描此文件并获取我想要的价值,但我希望这是另一个更简单的选项:)

Use these two annotations:使用这两个注释:

@Inject // javax.inject.Inject
@ConfigProperty(name = "name of the property", defaultValue = "if the property can't be found") // org.eclipse.microprofile.config.inject.ConfigProperty
Field declaration;

The properties must be defined in the resources/META-INF/microprofile-config.properties file.这些属性必须在 resources/META-INF/microprofile-config.properties 文件中定义。 For example:例如:

Java code: Java代码:

@Inject
@ConfigProperty(name = "mysql.server.host", defaultValue = "localhost")
String remoteIP;

resources/META-INF/microprofile-config.properties file:资源/META-INF/microprofile-config.properties 文件:

mysql.server.host=192.168.100.25
mysql.server.port=1111

我的想法视图

I created by hand META-INF dir and add application.properties also when i click on "user.test" in annotation I was redirected to my properties file我手动创建 META-INF 目录并添加 application.properties,当我单击注释中的“user.test”时,我被重定向到我的属性文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从属性文件定义注释的字段值 - How to define field value of an annotation from property file 如何使用Groovy从Java文件获取Annotation值? - How to get the Annotation value from Java file using Groovy? 在非字符串注释字段 spring 中使用 yml 文件中的值 - Use value from yml file in non string annotation field spring Java批注:将字段值用于另一个字段 - Java annotation: using a field value into another field 将最新字段值添加到HashMap的注释 - Annotation to add the latest field value into HashMap 如何使用自定义注释来验证带有Spring @Value字段的注释? - How to validate annotated with Spring @Value field using custom annotation? 如何(De)使用Jackson基于注释从对象序列化字段? - How to (De)serialize field from object based on annotation using Jackson? 如果使用@Value注释从属性文件注入参数,如何调用函数 - How to call a function if it's parameters are injected from properties file using @Value annotation Spring-使用值批注从本地配置文件读取 - Spring - Using Value annotation to read from the local config file 使用Spring配置文件中的条目作为注释属性的值 - Using an entry from the Spring configuration file as value for annotation property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM