ConfigurationProperties在Spring Boot2中不起作用

时间:2019-06-26 12:30:12

标签: spring-boot

Spring Boot Version 2.1.4 RELEASE

我有一个客户端jar,其中的Settings Bean定义为

    @ConditionalOnBean(annotation = Some.class)
    @ConfigurationProperties(prefix = "service")
    @Data
    public class WebserviceSettings  {
      //bunch of Properties
      private String serviceUrl;
      @PostConstruct
      public void init() { if(serviceUrl==null) throw ValidationException("bla") }
    }

    Another Config Class
    @ConditionalOnBean(annotation = Some.class)
    @EnableConfigurationProperties(WebserviceSettings.class)
    @Configuration
    public class ClientConfig{ 
       @Bean("webserviceSettings")
       public WebserviceSettings webserviceSettings(){
          return new WebserviceSettings();
       }
    }

客户端jar已添加到spring boot rest服务。

为Client jar编写的JUNIT测试可以正常工作,并且可以加载Bean,而不会从PostConstruct方法中产生任何验证异常。

在客户端内编写的测试类有效!

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = { ClientTestBootApplication.class })
    @TestPropertySource(properties="spring.config.location=classpath:application-test.yaml")

但是,当我加载Spring Boot Service时-它无法加载具有PostConstruct方法抛出的ValidationException的Settings bean。

application.yml
 service: 
   url: "bla"

这意味着将客户端添加到服务项目时,不会从yaml加载属性。该如何解决?

项目结构

spring-boot-restEasy Application
 - src/main/resources/application-DEV.yml
 --client jar A 
 --client jar B

客户端jar = [服务调用和某些业务逻辑], application-DEV.yml包含客户端Jar的服务设置,例如url

Exception 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service-packagName.Settings': Invocation of init method failed; nested exception is ValidationException: [url cannot be Null]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1754)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    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.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    ... 52 more

2 个答案:

答案 0 :(得分:0)

由于您已经在类WebserviceSettings.java的以下注释中将服务作为前缀提到了。

@ConfigurationProperties(prefix = "service")

然后无需在变量(private String serviceUrl;)中添加服务作为前缀。

要进行更正::将private String serviceUrl;更改为private String url;

答案 1 :(得分:0)

在application.yml中,您有:

service:
   url: greetings

您的配置类必须类似于:

    @ConditionalOnBean(annotation = Some.class)
    @ConfigurationProperties(prefix = "service")
    @Data
    public class WebserviceSettings  {
      //bunch of Properties
      private String url; // do not need to use service because you've already specified it in the prefix
      @PostConstruct
      public void init() { if(serviceUrl==null) throw ValidationException("bla") }
    }
相关问题