简体   繁体   中英

@Value Annotation not injecting values from properties file

I'm using @Value annotation to fetch properties & it is happening successfully in Normal method but not in Class constructor.Can anyone tell what may 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 the output

In Constructor=null

and obj.show() gives

In Method=A,B,C

(that means desired result)

I want values to be set as soon as constructor is called.I'm getting compilation error if i put the String declaration in static or initialize block.

How can we be sure that a member of an object is truly ready when the object is not finished being constructed (that is, the objects constructor is still not finshed)? It seems likely to me that Spring will not inject that value until AFTER the constructor has finished.

nicholas.hauschild is correct. The @Value will be injected after the object is constructed. If you want to do some initialization after the bean is constructed then you should implement IntializingBean .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM