弹簧值注入无法从属性中获取价值 [英] Spring value injection unable to get value from properties

查看:48
本文介绍了弹簧值注入无法从属性中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的课程:

I've got a class like this:

@Component
public class FaultsConfiguration {
    private int interval;

    @Autowired
    public FaultsConfiguration(@Value("${faults.interval}") int interval) {
        this.interval = interval;
    }
}

在application.properties中,我有这个:

In application.properties I have this:

faults.interval=130

我认为Bean应该将值130注入其中.相反,当初始化bean时,出现此错误:

I think the bean should get the value 130 injected into it. Instead when the bean is initialized I get this error:

java.lang.IllegalArgumentException: Could not resolve placeholder 'faults.interval' in value "${faults.interval}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.telensa.puma.etl.core.CoreApplication.main(CoreApplication.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

我已经尝试了所有可以找到的变化.我已经尝试过像这样的替代语法:

I've tried every variation on this I can find. I've tried alternative syntax like this:

@Value("${classpath:faults.interval}"

和这个:

 @Value("#{faults.interval}"

和这个:

"#{new Integer.parseInt('${faults.interval}')}"

都以类似的方式失败.

我设法通过使用 @ConfigurationProperties 来获取注入的属性,而无需对application.properties进行任何更改,例如(使用Lombok):

I have managed to get the property injected by using @ConfigurationProperties instead without any change to application.properties, like this (using Lombok):

@Service
@ConfigurationProperties(prefix="faults")
@Data
@NoArgsConstructor
public class FaultsConfiguration {
    private int interval;
}

显然,Spring很高兴从application.properties中读取属性.

So clearly Spring is happy reading properties from application.properties.

那么当我尝试使用价值注入时为什么不起作用?

So why doesn't it work when I try and use value injection?

推荐答案

为了让spring能够识别并解析属性文件,您需要将类型PropertySourcesPlaceholderConfigurer的bean添加到配置类中( FaultsConfiguration )

In order to let spring recognize and resolve the properties file you need to add the bean of type PropertySourcesPlaceholderConfigurer to you config class (FaultsConfiguration )

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

spring使用此bean来解析属性并将其放在表达式上,然后可以使用

The spring use this bean to resolve properties and place them on you expression, then you could use

(@Value("${faults.interval}") int interval

注入财产

别忘了让spring知道您的属性文件,将其添加到 FaultsConfiguration 中:

Don't forget to make spring aware of your properties file , add to FaultsConfiguration this :

@PropertySource("classpath:yourPropertiesFile.properties")

这篇关于弹簧值注入无法从属性中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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