繁体   English   中英

Spring @Value 未解析为属性文件中的值

[英]Spring @Value is not resolving to value from property file

我以前在其他一些项目中做过这个,我只是重新做同样的事情但由于某种原因它不起作用。 Spring @Value不是从属性文件中读取,而是从字面上获取值

AppConfig.java

@Component
public class AppConfig
{
    @Value("${key.value1}")
    private String value;

    public String getValue()
    {
        return value;
    }
}

应用上下文.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

在我的 controller 中,我有:

@Autowired
private AppConfig appConfig;

该应用程序启动得很好,但是当我这样做时

appConfig.getValue()

它返回

${key.value1}

它不会解析为属性文件中的值。

想法?

我还发现@value不起作用的原因是, @value需要PropertySourcesPlaceholderConfigurer而不是PropertyPlaceholderConfigurer 我做了同样的更改,它对我有用,我使用的是 spring 4.0.3 版本。 我在配置文件中使用以下代码进行了配置 -

@Bean 
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

就我而言,不会注入静态字段。

问题是由于我的 applicationContext.xml 与 spring-servlet.xml 中的问题 - 这是 bean 之间的范围问题。

pedjaradenkovic 好心地向我指出了一个现有资源: @Controller 类中的 Spring @Value 注释未评估属性文件中的值,并且Spring 3.0.5 未评估来自属性的 @Value 注释

就我而言,我缺少花括号。 我有@Value("foo.bar") String value而不是正确的形式@Value("${foo.bar}") String value

对于 Sprig-boot 用户,PropertyPlaceholderConfigurer 和 Spring 3.1 中添加的新 PropertySourcesPlaceholderConfigurer。 所以访问属性文件很简单。 只需注入

注意:确保您的属性不能是Static

@Value("${key.value1}")
private String value;

我正在使用 spring boot,对我来说将版本从1.4.0.RELEASE升级到1.5.6.RELEASE解决了这个问题。

就我而言,我有 lombok @AllArgsConstructor 并且它也获取了该属性。 删除此注释解决了问题。

阅读 pedjaradenkovic 的评论。

除了他提供的链接之外,这不起作用的原因是@Value处理需要PropertySourcesPlaceholderConfigurer而不是PropertyPlaceholderConfigurer

请注意,如果您的代码库中有多个application.properties文件,请尝试将您的值添加到项目的属性文件中。

您可以检查项目的pom.xml文件以识别当前项目的父项目是什么。

或者,尝试使用environment.getProperty()而不是@Value

@Value 有时可能需要一天或半天才能解决;)。

这是我所做的:

  1. 将属性添加到属性或 YAML 文件

  2. 确保使用 @EnableAutoConfiguration 或 @SpringBootApplication 注释主类

  3. 创建可以在其中使用 @Value 的 AppConfig

    @Value("${PROPERTY}") 私有字符串 URL;

在类级别使用 @Configuration 注释此 AppConfig

  1. 到目前为止,设置已完成 通过自动装配 AppConfig,您可以随时随地使用它

示例:在某些服务中 @Autowired 私有 AppConfig appConfig; 并在此服务的方法中调用 appConfig.getUrl() 以从属性文件中获取属性 URL 的值。

注意:不要试图在服务的构造函数中获取值,它将为空。

我的原因是导入了错误的dependency 我无意中从lombok导入了它,而不是"import org.springframework.beans.factory.annotation.Value;" 改回来解决了问题

对我来说,这是由于 Intellij IDEA 中的资源文件夹未标记为“Resources Root”。 只需右键单击资源目录->“将目录标记为”->“资源根目录”。

事情对我不起作用的原因是因为我设置了 2 个 PropertyPlaceholderConfigurer bean 实例(在一大组 spring 配置中)。 一旦设置了一个,另一个就完全没用了。 这可能是一件非常明显的事情,但对我来说并非如此。 我不明白为什么 PropertyPlaceholderConfigurer 的第二个实例在创建时不会抛出异常(当已经有另一个实例时)。 而不是默默地忽略第二个实例。 像这样你可能会得到一个有意义的错误。

希望它对那里的任何人都有帮助:.)

对我来说,这是因为我移动了我的项目根文件夹。 我不知道为什么,但是删除所有文件夹.settings.mvntarget.project并将项目重新导入到 eclipse 中对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM