mroma95
mroma95

Reputation: 117

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:

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 :)

Upvotes: 0

Views: 703

Answers (2)

mroma95
mroma95

Reputation: 117

My IDEA view

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

Upvotes: 1

Hidi Eric
Hidi Eric

Reputation: 364

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. For example:

Java code:

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

resources/META-INF/microprofile-config.properties file:

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

Upvotes: 0

Related Questions