我以前在其他项目中已经做过这个,现在我正在重新做同样的事情,但由于某些原因它不起作用了。Spring的@Value
不能从属性文件中读取值,而是按照字面意思来处理。
AppConfig.java
@Component
public class AppConfig
{
@Value("${key.value1}")
private String value;
public String getValue()
{
return value;
}
}
applicationContext.xml:
<context:component-scan
base-package="com.test.config" />
<context:annotation-config />
<bean id="appConfigProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:appconfig.properties" />
</bean>
appconfig.properties
key.value1=test value 1
在我的控制器中,我有:
@Autowired
private AppConfig appConfig;
应用程序启动正常,但当我执行...appConfig.getValue()
它返回
${key.value1}
它没有解析为属性文件内部的值。
你有什么想法?