Spring Boot Version 2.1.4 RELEASE我有一个带有设置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();
}
}客户端jar被添加到spring boot rest服务。
为客户端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发布于 2019-06-26 21:26:12
因为您已经在WebserviceSettings.java类的以下注释中将服务作为前缀提到。
@ConfigurationProperties(prefix = "service"),
然后,不需要在变量(private String serviceUrl;)中添加服务作为前缀。
更正错误的:将private String serviceUrl;改为private String url;
发布于 2019-06-26 21:26:27
在您的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") }
}https://stackoverflow.com/questions/56772877
复制相似问题