4

I'm Spring newbie and I'm wondering what are prerequisites/conditions of using @Value annotation in Spring?

What I have for now is a Token class which has hardcoded secret key as a field. What I'm trying to do is to move this secret key to config file to eliminate hardcoding but for some reason the following is not working.

public class Token {
//...some code

    @Value("${my.secretKey}")
    private String key;

//...some code
}

Maybe there is any standard technique to solving this type of tasks.
Thanks for help!

p.s. I have .properties file which contains my.secretKey=123 entry.

mr.nothing
  • 5,141
  • 10
  • 53
  • 77
  • Maybe related to: http://stackoverflow.com/questions/33344895/spring-configuration-file-with-propertyplaceholderconfigurer-bean-doesnt-resol/33370122#33370122 – superbob Sep 13 '16 at 11:23
  • @Reimeus, could you please clarify what info it is required to provide? To speak frank, it is almost a new project with a couple of classes. – mr.nothing Sep 13 '16 at 11:23
  • have a properties file, define them in **applicationcontext**, use them as you are using. – BeardAspirant Sep 13 '16 at 11:24
  • 2
    Is the instance of the `Token` class a Spring bean? If you create a new instance yourself by using `new Token()` then Spring injection does not work; Spring cannot set the value of the `key` variable. – Jesper Sep 13 '16 at 11:25
  • @superbob, the thing is that my token is not a configuration class. It is a class I create by myself using java new. – mr.nothing Sep 13 '16 at 11:27
  • @Jesper are you saying that I can't use value annotation with classes that are not spring beans? Thanks, didn't find it anywhere in documentation. – mr.nothing Sep 13 '16 at 11:28
  • 1
    If you create objects yourself with `new` then Spring doesn't get a chance to do things like dependency injection and injecting values. Such things only work on objects that are created through Spring (Spring beans). – Jesper Sep 13 '16 at 11:30
  • 2
    From the [doc](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html): "Property values can be injected directly into your **beans** using the @Value" (that's spring-boot but same thing) –  Sep 13 '16 at 11:30
  • @Jesper, thank you very much. Could you please post your answer so I can accept it? – mr.nothing Sep 13 '16 at 11:43

3 Answers3

8

Injecting dependencies and values only works on objects that are managed by Spring - when you manually create an object using new, for example by doing new Token(), then Spring cannot process the object to inject dependencies and values.

The Token object must be a Spring bean for this to work.

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

To use @Value annotation your Spring version must be 3.0+

You must register a static PropertySourcesPlaceholderConfigurer bean in either XML or annotation, so that Spring @Value know how to interpret ${}

For more information

https://www.mkyong.com/spring3/spring-value-default-value/

A0__oN
  • 8,740
  • 6
  • 40
  • 61
0

One mistake can also be in proper location of .properties file. Your .properties file location is very important. Your xyz.properties file must be place in your project's src folder, like : src/xyz.properties After that,
Load your properties file in the XML config file : File: applicationContext.xml Add the following lines:

<context:property-placeholder location="classpath:xyz.properties"/>

This code should appear just after the <context:component-scan .../> line in your xml config file. And now
Inject the properties values into your Token class, It should work now.
Happy Coding.

Shubh
  • 35
  • 9