Injecting property using @Value to abstract class

前端 未结 2 2059
再見小時候
再見小時候 2021-02-07 23:09

I have an abstract class in which I am trying to use the @Value annotation to inject value from a property file

public abstract class Parent {
     @Value (\"${s         


        
2条回答
  •  隐瞒了意图╮
    2021-02-07 23:31

    I think you'll find the sharedVal is being set, but you're trying to use it too soon in the constructor. The constructor is being called (must be called) before Spring injects the value using the @Value annotation.

    Instead of processing the value in the constructor, try a @PostContruct method instead, eg:

    @PostConstruct
    void init() {
        //perform common action using sharedVal
     }
    

    (or alternatively, implement Spring's InitializingBean interface).

提交回复
热议问题