Spring 弹簧特性(特性占位符)自动关联

Spring 弹簧特性(特性占位符)自动关联,spring,properties,dependency-injection,Spring,Properties,Dependency Injection,我的applicationContext.xml中有 <context:property-placeholder location="classpath*:*.properties" /> <bean id="clientPreferencesManager" class="pl.bildpresse.bildchat2.business.ClientPreferencesManager" > <property name="clientApiUrl"

我的applicationContext.xml中有

<context:property-placeholder location="classpath*:*.properties" />


<bean id="clientPreferencesManager" class="pl.bildpresse.bildchat2.business.ClientPreferencesManager" >
    <property name="clientApiUrl" value="${clientapi.url}" />     
</bean>

您可以使用
@Value

@Value("${clientapi.url}") 
public void setClientApiUrl(String clientApiUrl) { 
    this.clientApiUrl = clientApiUrl; 
}

对于Spring3.0,正确的方法是使用
@Value(${expression}”)

对于spring 3.0之前的版本,您可以尝试:

@Autowired
private StringValueResolver resolver;
这里没有上下文初始化问题,但我不确定它是否能工作。使用解析器可以解析属性。

我的解决方案是使用

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

这个也不错。刚刚收到。您需要添加@Autowired 比如:

@Autowired
@Value("${clientapi.url}") 
private StringValueResolver resolver;
我正在使用Spring3.0.0.0版本


干杯

我花了一些时间才明白为什么它不起作用。我总是用
而不是
$
。我总是得到这样的信息:

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

@Value("#{secretkey}')


我希望这能节省一些人的时间。

没错,对于Spring3.0,它是当前的版本。(+1)所以是美元符号!不是杂碎。谢谢你的提示。@Value的javadocs误导了meSilly的问题,但它从类路径上的属性文件@user2441441的哪里获得${clientapi.url},最有可能的是
application.properties
I被这个问题搞糊涂了……这就是修复方法!谢谢菲利克斯
EL1008E:(pos 0): Field or property 'secretkey' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
@Value("#{secretkey}')
@Value('${secretkey}')