@Value Annotations do not enter values ​​from the properties file

I use the @Value annotation to retrieve properties, and this succeeds in the regular method, but not in the class constructor. Can someone say what could be the reason?

 Class A { @Value("#{columnProperties['Users.Columns']}") String columnNames; A() { System.out.println("In Constructor="+columnNames); } void show() { System.out.println("In Method="+columnNames); } } 

when i do

 A obj=new A(); 

I get a conclusion

In Constructor = null

and obj.show() gives

In method = A, B, C

(this means the desired result)

I want the values ​​to be set as soon as the constructor is called. I get a compilation error if I put a String declaration in a static or initialization block.

+4
source share
2 answers

How can we be sure that the element of the object is really ready when the object is not constructed (i.e. the constructor of the objects is still not finished)? It seems to me that Spring will not enter this value until the constructor finishes.

+3
source

nicholas.hauschild is correct. @Value will be introduced after , the object will be built. If you want to do some initialization after creating the bean, then you must implement IntializingBean .

+3
source

All Articles