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