0

I would like to get a list of the properties configured in my system using the @Value annotation from spring. Is it possible? I need the configuration key that is set in the @Value().

I know I can scan for annotations, but is there a better way to do this? If perhaps there is some support in the spring framework to get this information.

Jarle Hansen
  • 1,993
  • 2
  • 16
  • 29
  • 1
    Take a look at ["Reflection"](https://github.com/ronmamo/reflections) –  Oct 30 '15 at 09:44
  • Thanks, I did considered this but hoped it was a better way of doing it, if perhaps spring had support for it somehow. – Jarle Hansen Oct 30 '15 at 09:45
  • If you only want the values that are used in the app and not all the values that are available in properties files (or command line), you'll have to cope with reflection (from scratch or with Reflection tool or Spring itself). – Gaël J Oct 30 '15 at 09:51
  • @Jarle Spring has a class `AnnotationUtils` that *might* help you here. – Tomas Oct 30 '15 at 10:06

1 Answers1

1

This problem is the main reason why scattering @Value all over your code is a bad idea.

You should instead create some kind of central "configuration service" in your application, and let that be the single point of contact for your configuration values. In there, you can have all the @Value fields you want.

Then, you @Autowire your configuration service instead, and the classes can ask it for the necessary stuff.

Tomas
  • 1,315
  • 10
  • 17
  • Thank you for the advice. This is all fine and good (and we actually do this), but I still want to generate some sort of documentation automatically that displays the configuration options for all types of services. – Jarle Hansen Oct 30 '15 at 09:55
  • @Jarle Would putting Javadoc on the various fields and methods in the configuration service not serve this purpose? – Tomas Oct 30 '15 at 10:06
  • That is certainly an option. It still feels like a somewhat manual process to me, would like something that works without the service developer needing to do anything. – Jarle Hansen Oct 30 '15 at 10:09