mr.nothing mr.nothing - 1 year ago 171
Java Question

Spring @Value annotation

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

private String key;

//...some code

Maybe there is any standard technique to solving this type of tasks.

Thanks for help!

p.s. I have
file which contains

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download