Spring properties (property-placeholder) autowiring

前端 未结 5 1579
天命终不由人
天命终不由人 2021-01-01 09:03

I have in my applicationContext.xml






        
相关标签:
5条回答
  • 2021-01-01 09:20

    It took me some time to understand why it didn't work. I always used a # instead of a $. I always got the message:

    EL1008E:(pos 0): Field or property 'secretkey' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    

    Just had to change it from:

    @Value("#{secretkey}')
    

    to

    @Value('${secretkey}')
    

    I hope this saves somebody's time.

    0 讨论(0)
  • 2021-01-01 09:23

    You can use @Value:

    @Value("${clientapi.url}") 
    public void setClientApiUrl(String clientApiUrl) { 
        this.clientApiUrl = clientApiUrl; 
    }
    
    0 讨论(0)
  • 2021-01-01 09:27

    For spring 3.0, the correct way is the one shown - using @Value("${expression}")

    For spring pre-3.0, you can try:

    @Autowired
    private StringValueResolver resolver;
    

    There were no context initialization problems here, but I'm not sure it will work. Using the resolver you can resolve properties.

    0 讨论(0)
  • 2021-01-01 09:31

    Ok. Just got it. You need to add @Autowired Something like:

    @Autowired
    @Value("${clientapi.url}") 
    private StringValueResolver resolver;
    

    I'm using spring 3.0.0.RELEASE

    Cheers

    0 讨论(0)
  • 2021-01-01 09:31

    My solution is to use

    <context:property-override location="classpath:clientapi.properties" />
    

    and then in clientapi.properties file

    clientPreferencesManager.clientApiUrl=http://localhost:8084/ClientAPI/resources/
    

    This one is good too

    0 讨论(0)
提交回复
热议问题