Spring值注入无法从属性中获取值

时间:2018-01-31 15:16:48

标签: java spring spring-boot dependency-injection

我有这样的课程:

@Component
public class FaultsConfiguration {
    private int interval;

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

在application.properties中我有这个:

faults.interval=130

我认为bean应该将值130注入其中。相反,当bean被初始化时,我得到了这个错误:

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)

我已经尝试了我能找到的每一个变种。我尝试过这样的替代语法:

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

和此:

 @Value("#{faults.interval}"

和此:

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

这一切都以类似的方式失败。

我已经设法通过使用@ConfigurationProperties来注释属性而不对application.properties进行任何更改,就像这样(使用Lombok):

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

很明显,Spring很乐意从application.properties中读取属性。

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

3 个答案:

答案 0 :(得分:4)

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

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

spring使用这个bean来解析属性并将它们放在你的表达式上,然后你可以使用

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

注入你的财产

不要忘记让你知道你的属性文件,添加到FaultsConfiguration这个:

@PropertySource("classpath:yourPropertiesFile.properties")

答案 1 :(得分:-1)

application.properties中声明的变量faults.interval自动装配为bean。在这种情况下,要使用该变量,您只需将其声明为要使用它的类中的变量。在您的情况下,您应该尝试按如下方式声明:

@Component
public class FaultsConfiguration {

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

答案 2 :(得分:-1)

我们可以获得“faults.interval”的值,如下所述,一旦属性配置正确,

@Component
public class FaultsConfiguration {

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

private int Testinterval;

    @Autowired
    public FaultsConfiguration(int interval) {
        this.Testinterval = interval;
    }
}