1

Is there a way to create @ConfigurationProperties beans in runtime using spring's functionality? Let's say I want to state the prefixes in my custom annotation and create beans for them in runtime because creating them manually seems like a boiler-plate to me.

Something like this:

@MyAnnotation({
     @CustomProps(prefix="foo"),
     @CustomProps(prefix="bar")
})

And then in runtime, I want to have two config beans of the specified type created from properties with these prefixes. I know I can generate code for them using an annotation processor, but maybe it's easier to achieve by spring's bean processors or something like this?

1 Answer 1

1

Yes! you can achieve it but you can't have class fields for each property. So, easy approach is use spring annotation processor and for fields you can use map which you could map using Environment bean.

https://www.baeldung.com/spring-annotation-bean-pre-processor blog would be helpful in understanding how it works with annotation processor.

(Here)[Spring: access all Environment properties as a Map or Properties object you can see how to get map of properties.

Sign up to request clarification or add additional context in comments.

3 Comments

Do I get it right that I will have to map properties manually in this case?
yes! you have to create the bean object and map the properties manually/grammatically in the code
That's something I wanted to avoid. Thanks for the answer anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.