java properties / xml as constant


I try to collect values from a .properties to use it in an annotation, but it is not possible to do it with ResourceBundle since the annotations work with constants and the result of this, it is not. I tried to change the properties to .xml by collecting the values with .getProperty () and the same thing happens. The question is: is there any way to collect these values as constants? Or write in the xml as constants?

Thanks in advance.

asked by Juan 04.09.2018 в 13:01

1 answer


No 1 .

Annotations are part of the class code. The parameters are defined at compile time , not at runtime. Reflection methods do not allow any change in annotations.

The option you have to select a class with an annotation with a certain parameter at runtime is to define an interface, implement X classes with the X parameters, and use a method / class Factory that is responsible for instantiating the correct subclass depending on the configuration.

If you want more flexibility, we are already talking about incorporating dependency injection (for example, with CDI or Spring) to inject classes with specific annotations as needed.

But all this smells like you're trying to use annotations to solve a problem they're not designed for; I suggest you rephrase what you want to do.

1 Maybe there is some way generating "on the fly" classes with javassist and the like. But I do not want to go there, and I doubt you want to.     
answered by 04.09.2018 / 14:10