gpt4 book ai didi

java - Spring 值注入(inject)无法从属性中获取值

转载 作者:行者123 更新时间:2023-12-05 05:17:57 28 4
gpt4 key购买 nike

我有这样一个类:

@Component
public class FaultsConfiguration {
private int interval;

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

在 application.properties 我有这个:

faults.interval=130

我认为应该将值 130 注入(inject)到 bean 中。相反,当 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 进行任何更改来注入(inject)属性,就像这样(使用 Lombok):

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

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

那么为什么当我尝试使用值注入(inject)时它不起作用?

最佳答案

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

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

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

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

注入(inject)你的属性

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

@PropertySource("classpath:yourPropertiesFile.properties")

关于java - Spring 值注入(inject)无法从属性中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48545498/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com