无法配置SpringBootApplication以读取属性

时间:2019-09-05 03:11:20

标签: java spring spring-boot

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

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

我是Spring的新手,感觉终极愚蠢

我的课程结构是:

-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

这是输出。请注意,控制台中的“空”应该是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());
    }
}

1 个答案:

答案 0 :(得分:2)

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

删除此行配置=新的监视配置,并将@Autowired添加到配置定义中。我还建议您改用配置属性。