Spring @Value不能解析为属性文件中的值 [英] Spring @Value is not resolving to value from property file

查看:452
本文介绍了Spring @Value不能解析为属性文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾在其他项目中使用过此工具,我只是在做同样的事情,但由于某种原因它无法正常工作. Spring @Value不是从属性文件中读取,而是从字面上获取值

I've had this working in some other project before, I am just re-doing the same thing but for some reason it's not working. The Spring @Value is not reading from property file, but instead it's taking the value literally

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

在我的控制器中,我有:

In my controller, where I have:

@Autowired
private AppConfig appConfig;

应用程序启动正常,但是当我这样做

The application starts just fine, but when I do

appConfig.getValue()

它返回

${key.value1}

它不能解析为属性文件中的值.

It doesn't resolve to the value inside the properties file.

有想法吗?

推荐答案

问题是由于我的applicationContext.xml和spring-servlet.xml中的问题所致-它正在确定bean之间的问题.

Problem is due to problem in my applicationContext.xml vs spring-servlet.xml - it was scoping issue between the beans.

pedjaradenkovic友善地向我指出了一个现有资源: @Controller类中的Spring @Value注释不评估属性文件中的值 Spring 3.0.5不会评估@Value注释从属性中

pedjaradenkovic kindly pointed me to an existing resource: Spring @Value annotation in @Controller class not evaluating to value inside properties file and Spring 3.0.5 doesn't evaluate @Value annotation from properties

这篇关于Spring @Value不能解析为属性文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆