Spring @Value未解析来自属性文件的值

91

我以前在其他项目中已经做过这个,现在我正在重新做同样的事情,但由于某些原因它不起作用了。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}

它没有解析为属性文件内部的值。

你有什么想法?


6
需要翻译的内容:Duplicated https://dev59.com/v2ct5IYBdhLWcg3wsPd5 and https://dev59.com/dm435IYBdhLWcg3wrSLN这两个链接都提到了Spring框架中@Value注解无法在Controller类中正确解析属性文件中的值的问题。 - pedjaradenkovic
谢谢!我没有找到那个线程,大多数我找到的都与值为NULL有关。 - TS-
15个回答

80

我发现@value无法工作的原因是,@value 需要使用 PropertySourcesPlaceholderConfigurer 而不是 PropertyPlaceholderConfigurer。我进行了相同的更改,并且它对我起作用了,我正在使用 spring 4.0.3 release 版本。我在我的配置文件中使用以下代码进行配置 -

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

你可以从http://blog.codeleak.pl/2015/09/placeholders-support-in-value.html获取有关此配置的更多信息。 - Kishor K
1
请注意:您不再需要在Spring Boot 1.5.9中添加此Bean。 - realPK
1
@realPK 看起来你是这样做的,我使用的是Spring 4.2.6版本,仍然需要添加它。 - Florian Wicher
1
@FlorianWicher 我在谈论Spring Boot,我相信它使用的是Spring框架4.3版本。参考链接-https://spring.io/blog/2017/11/28/spring-boot-1-5-9-available-now。 - realPK
@Florian Wicher - 也许你可以尝试将Spring Boot版本升级到2.x,这样就可以避免这个问题了。 - Sachchidanand Singh

20

18

在我的情况下,我缺少了花括号。我写成了@Value("foo.bar") String value,而正确的形式应该是@Value("${foo.bar}") String value


2
你让我的一天变得美好,谢谢。 - Bassel Kh

15

对于Spring Boot用户,无论是PropertyPlaceholderConfigurer还是在Spring 3.1中新增的PropertySourcesPlaceholderConfigurer都可以轻松访问属性文件。只需注入即可。

注意:确保你的属性不是静态的Static

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

4

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

这是我所做的:

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

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

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

    @Value("${PROPERTY}") private String URL;

在类级别上用@Configuration对AppConfig进行注解

  1. 到目前为止,设置完成后,通过自动连接AppConfig使用它

例如:在某个服务中 @Autowired private AppConfig appConfig; 以及在该服务的方法中调用appConfig.getUrl()来获取属性文件中URL属性的值。

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


1
非常感谢 - 这正是我的观点:“注意:不要在服务的构造函数中尝试获取值,它将为空。” - Tony

4

我使用的是Spring Boot,将版本从1.4.0.RELEASE升级到1.5.6.RELEASE解决了这个问题。


3

在我的情况下,我使用了lombok的@AllArgsConstructor注解,它也捕获了该属性。删除此注解解决了问题。


1
请阅读pedjaradenkovic的评论。
除了他提供的链接,这个不起作用的原因在于@Value处理需要一个PropertySourcesPlaceholderConfigurer而不是PropertyPlaceholderConfigurer。

1
PropertyPlaceholderConfigurer 对我来说完全没问题。我只需要修复我的应用程序上下文 XML 和 Spring Servlet XML 中的 context:component-scan。 - TS-
@TS,请问你使用的 Spring 版本是哪个? - Muel
你难道不能用@Autowire Environment吗?参考https://dev59.com/nmYr5IYBdhLWcg3wYZKD#15562319。 - jediz

0
对我来说,这是因为在Intellij IDEA中资源文件夹没有标记为“Resources Root”。只需右键单击资源目录->“标记目录为”->“Resources Root”。

0
我的问题是因为导入了错误的依赖项。 我不小心从lombok导入了它,而不是从"import org.springframework.beans.factory.annotation.Value;" 将其改回来解决了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接