gpt4 book ai didi

java - 配置 SpringBootApplication 从属性读取时遇到问题

转载 作者:行者123 更新时间:2023-12-02 01:20:29 26 4
gpt4 key购买 nike

我正在努力在简单的 SpringBootApplication 中加载配置。当我尝试打印配置类中的变量时,它们始终为空。

最初,我在 WatchApplication.java 中有可配置变量。然而,我认为将从配置中读取的所有变量仍然为空。配置位于/main/resources/application.properties。此后,我添加了一个类来保存我计划在 WatchApplication 中读取的配置,称为 WatchConfiguration。

我是 Spring 新手,感觉自己极其愚蠢

我的类(class)结构是:

-main
|--java
| |--WatchApplication.java
| |--WatchConfiguration.java
|
|--resources
|--application.properties

这是 WatchApplication.java

@SpringBootApplication
public class WatchApplication {

private WatchConfiguration configuration;

public static void main(String[] args)
{
SpringApplication.run(WatchApplication.class, args);
}

/**
* Initialize the application
*/
@PostConstruct
public void initialize()
{
configuration = new WatchConfiguration();
System.out.println(configuration.getVideoDirectory());
}
}

这是我的 WatchConfiguration.java 的样子

@Configuration
public class WatchConfiguration {
@Value("${video.output.directory}")
private String videoDirectory;

@Value("${target.sites}")
private List<String> targetSites;

public WatchConfiguration getWatchConfiguration ()
{
return new WatchConfiguration();
}

public String getVideoDirectory() {
return videoDirectory;
}

public void setVideoDirectory(String videoDirectory) {
this.videoDirectory = videoDirectory;
}

public List<String> getTargetSites() {
return targetSites;
}

public void setTargetSites(List<String> targetSites) {
this.targetSites = targetSites;
}
}

还有我的 application.properties

video.output.directory = C:\\Users\\Alex\\Desktop\\output

# Target Sites
target.sites[0] = https://youtube.com
target.sites[1] = https://bing.com

这是输出。请注意控制台中的“null”,它应该是 video.output.directory 属性值。

[2019-09-04 20:09:00.752] - 16812 INFO [restartedMain] --- com.pirate.baywatch.BaywatchApplication: Starting BaywatchApplication on DESKTOP-ULP9VBQ with PID 16812 (C:\Users\Alex\IdeaProjects\baywatch\target\classes started by Alex in C:\Users\Alex\IdeaProjects\baywatch)
[2019-09-04 20:09:00.765] - 16812 INFO [restartedMain] --- com.pirate.baywatch.BaywatchApplication: No active profile set, falling back to default profiles: default
[2019-09-04 20:09:00.792] - 16812 INFO [restartedMain] --- org.springframework.boot.devtools.env.DevToolsPropertyDefaultsPostProcessor: Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
null
[2019-09-04 20:09:01.142] - 16812 WARNING [restartedMain] --- org.springframework.context.annotation.AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baywatchConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'target.sites' in value "${target.sites}"
[2019-09-04 20:09:01.148] - 16812 INFO [restartedMain] --- org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2019-09-04 20:09:01.154] - 16812 SEVERE [restartedMain] --- org.springframework.boot.SpringApplication: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baywatchConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'target.sites' in value "${target.sites}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
at com.pirate.baywatch.BaywatchApplication.main(BaywatchApplication.java:16)
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)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'target.sites' in value "${target.sites}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:851)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
... 21 more

解决方案通过在成员变量中添加@Autowired并在initialize方法中去掉配置类的初始化来解决

@SpringBootApplication
public class WatchApplication {

@Autowired
private WatchConfiguration configuration;

public static void main(String[] args)
{
SpringApplication.run(WatchApplication.class, args);
}

/**
* Initialize the application
*/
@PostConstruct
public void initialize()
{
System.out.println(configuration.getVideoDirectory());
}
}

最佳答案

问题是您正在自己使用 new 创建配置。您应该将其从上下文 @Autowired 到您的应用程序类中。

删除此行configuration = new Watch Configuration并将@Autowired添加到配置定义中。我还建议您改用配置属性。

关于java - 配置 SpringBootApplication 从属性读取时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57797946/

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