[英]java.lang.IllegalArgumentException: No ConfigurationProperties annotation found on
I'm integrating a external project contract service client in my project(Menu provider service) in context.xml.我在 context.xml 中的我的项目(菜单提供程序服务)中集成了一个外部项目合同服务客户端。 I'm using spring boot to run my project in STS, I'm going through below error while starting the spring boot application.
我正在使用 spring boot 在 STS 中运行我的项目,我在启动 spring boot 应用程序时遇到以下错误。
java.lang.IllegalArgumentException: No ConfigurationProperties annotation found on 'com.cnanational.contract.client.config.ContractClientConfig'.
at org.springframework.util.Assert.notNull(Assert.java:134)
2018-02-22 10:59:52.867 INFO 10184 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@79501dc7: startup date [Thu Feb 22 10:59:51 GMT-07:00 2018]; root of context hierarchy
2018-02-22 10:59:52.869 WARN 10184 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@79501dc7: startup date [Thu Feb 22 10:59:51 GMT-07:00 2018]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:427)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="timeElapsedAspect" class="com.cnanational.servicecommon.aop.TimeElapsedAspect"></bean>
<aop:config>
<aop:aspect id="timeElapsedAspect" ref="timeElapsedAspect">
<aop:pointcut id="controllerPointcut" expression="execution(public * com.cnanational.menuprovider.controller.MenuProviderServiceController.*(..))"/>
<aop:around method="logTimeElapsed" pointcut-ref="controllerPointcut"/>
</aop:aspect>
</aop:config>
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources">
<list>
<value>classpath:/menu-provider-service/contract-service-client.yml</value>
<value>classpath:/menu-provider-service/menu-provider-service.yml</value>
</list>
</property>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
</beans>
@SpringBootApplication
@EnableConfigurationProperties({ MenuProviderServiceConfig.class, CreateRatesConfig.class,CommonConfiguration.class, ContractClientConfig.class })
@Import({
CommonConfiguration.class
})
@ImportResource({ "classpath:/menu-provider-service/context.xml" })
public class MenuProviderServiceApplication
@EnableConfigurationProperties
annotation expects all the classes provided, in argument, should be annotated with @ConfigurationProperties
. @EnableConfigurationProperties
注释期望所有提供的类在参数中都应该用@ConfigurationProperties
注释。
Annotate the class ContractClientConfig
with @ConfigurationProperties
and it should work.使用
@ConfigurationProperties
注释ContractClientConfig
类,它应该可以工作。
. .
@Bean
@ConfigurationProperties(prefix = "app.my-menu") // spring will get app.my-menu.* properties
MenuProperties menuConfig() { // and will set them into
return new MenuProperties(); // the returned object
REF: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-3rd-party-configuration参考: https : //docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-3rd-party-configuration
A better way to re-use ConfgigurationPropertes from external (as well as from local) project is to init the property objects explicitly in your @Configuration class.从外部(以及本地)项目重新使用 ConfigigurationPropertes 的更好方法是在 @Configuration 类中显式初始化属性对象。 So you don't need any @EnableProperties, or @ConfigurationProperties annotations.
所以你不需要任何 @EnableProperties 或 @ConfigurationProperties 注释。 Especially, in external projects, where you don't have control over.
尤其是在您无法控制的外部项目中。
The above snippet assumed two things: your application.yml has app.my-menu.*
properties:上面的代码片段假设了两件事:您的application.yml具有
app.my-menu.*
属性:
app:
my-menu:
text: "Help"
path: "/help/index.html"
The POJO properties class has setters with the same as properties names: POJO 属性类具有与属性名称相同的 setter:
public class MenuProperties {
public String text; // getters are optional
public String path;
public void setText(String v) { text = v; } // setters is a must
public void setPath(String v) { path = v; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.