简体   繁体   English

ConfigurationProperties在Spring Boot2中不起作用

[英]ConfigurationProperties Not Working in Spring Boot2

Spring Boot Version 2.1.4 RELEASE

I have client jar with a Settings Bean defined as 我有一个带有定义Bean的客户端jar

    @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();
       }
    }

Client jar is added to spring boot rest service. 客户端jar已添加到spring boot rest服务。

The JUNIT Test written for the Client jar works fine and loads up the Bean without any validation Exception coming out of the PostConstruct Method. 为Client jar编写的JUNIT测试可以正常工作,并且可以加载Bean,而不会从PostConstruct方法中产生任何验证异常。

Test Class written within the client works! 在客户端内编写的测试类作品!

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

However, when I am loading the Spring Boot Service - it fails to load the Settings bean with ValidationException thrown from PostConstruct Method. 但是,当我加载Spring Boot Service时-它无法加载具有PostConstruct方法抛出的ValidationException的Settings bean。

application.yml
 service: 
   url: "bla"

It means that when the client is added to services project, it's not loading the property from yaml. 这意味着将客户端添加到服务项目时,不会从yaml加载属性。 How to solve this? 如何解决呢?

Project Structure 项目结构

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

Client jar = [Service Call and some Business Logic], application-DEV.yml Contains Service Settings of Client Jar like url 客户端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

Since you already mentioned the service as a prefix in the following annotation on the class WebserviceSettings.java. 由于您已经在类WebserviceSettings.java的以下注释中将服务作为前缀提到了。

@ConfigurationProperties(prefix = "service") , @ConfigurationProperties(prefix = "service")

then no need to add service as a prefix in the variable ( private String serviceUrl; ). 则无需在变量中添加服务作为前缀( private String serviceUrl; )。

To correct it: Change private String serviceUrl; 要更正它:更改private String serviceUrl; to private String url; private String url;

in your application.yml, you have : 在您的application.yml中,您有:

service:
   url: greetings

Your config class must look like : 您的配置类必须类似于:

    @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") }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM