0

I am having issues using my properties with @Value annotation and I am wondering is there a way when debugging to check the value of an @Value annotation. For example I would stop at a breakpoint in my code and evaluate

@Value("${myString}")
String x;

or say if i wanted to check the spring profile i would evaluate

@Value("${spring.profiles.active}")
String springProfile;

I do only see null values for these though?

AnonymousAlias
  • 1,149
  • 2
  • 27
  • 68
  • how did you instantiate the class those values are in? – Stultuske Jul 22 '20 at 10:51
  • I am wondering is it possible to do this from the evaluator in my IDE, say if I hadn't created String x in any class but wish to see if i could read the property that way from the debugger – AnonymousAlias Jul 22 '20 at 10:53
  • intellij shows values of @Value even in compile time - just use intellij and you will see the values that will be set – J Asgarov Jul 22 '20 at 10:55
  • Yea I use intellij, but I mean if i use the evaluator from intellij and I haven't set the @value in the code anywhere can i check what it would be if i had of – AnonymousAlias Jul 22 '20 at 10:56
  • Are you using @Value in a Spring bean? – Simon Martinelli Jul 22 '20 at 11:15
  • To programmatically check (list) what properties Spring Boot knows, see this question: [Spring: access all Environment properties as a Map or Properties object](https://stackoverflow.com/q/23506471/5221149). --- However, it is equally likely that you're using the annotation wrong, i.e. that the class in question is not a Spring Bean, but with the code taken out of context, as it currently is, we can't say if that is the case. – Andreas Jul 22 '20 at 11:15
  • So I was wondering is there a way to check if we were not using anything in a spring bean but rather stopped at a breakpoint and then trying to evaluate but i think @Andreas that will answer my question if I can list all properties. Put it as an answer if you wish and i will accept – AnonymousAlias Jul 22 '20 at 11:49
  • @AnonymousAlias if you apply the annotation correctly, sure, you can, because the value will be assigned before you can call the code. – Stultuske Jul 22 '20 at 11:55
  • @AnonymousAlias To answer your first question: yes you can see the value as that gets assigned while the bean is initializing. For the second one: it should be a managed bean then only these values will get initialized – saurabh kedia Jul 22 '20 at 12:28
  • @AnonymousAlias I wouldn't answer, I would close as duplicate of that other answer, except I can't do that right now. Maybe someone else will. – Andreas Jul 22 '20 at 14:39

1 Answers1

0

The spring values will be initialized before we call the code so we will be able to evaluate all spring properties at run time.

AnonymousAlias
  • 1,149
  • 2
  • 27
  • 68