piotrek
piotrek

Reputation: 14540

How to create spring property programatically?

I can easily create new property by adding it to yml or properties file: my.custom.property=5. but how can i do the same from the code? sth like:

@SomeSpringAnnotation("my.custom.property")
fun myFun() = 5

or

registerProperty("my.custom.property", 5)

Upvotes: 0

Views: 329

Answers (3)

Michal
Michal

Reputation: 2423

Adding @PropertySource with custom factory allows for creating dynamic PropertySource.

I did not try it though. Ii might also be evaluated too late for some properties. I personally also think it is not very good idea to create properties dynamically. Customizing the code which is using the property would be better solution IMO.

Another approach would be using @Value with EL expression along the lines of @Value("#{someBean.someValue}").

Upvotes: 1

akuma8
akuma8

Reputation: 4691

Have a look at, System.setProperty(...) or System.setProperties(), those methods can be used to set properties at runtime.

Upvotes: 0

pigman
pigman

Reputation: 96

programatically?i don't think it's meaningful! what's the difference between a programatical property and a constant?

if you want to create a property advanced, the default-value expression of Value annotation is a good way.eg.@Value("${my.custom.property:5}") private int myCustomProperty;

Upvotes: 0

Related Questions