简体   繁体   English

java.lang.IllegalArgumentException: 在上找不到 ConfigurationProperties 注释

[英]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 应用程序时遇到以下错误。

Errors错误

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)

context.xml file上下文.xml 文件

<?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>

Spring Boot Application class: Spring Boot 应用程序类:

@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类,它应该可以工作。

Short instructions:简短说明:

  1. remove @EnableConfigurationProperties annotation移除 @EnableConfigurationProperties 注释
  2. init config properties POJOs directly as beans将 POJO 直接作为 bean 初始化配置属性

. .

  @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


Details:详情:

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.

相关问题 java.lang.IllegalArgumentException:找不到产品 - java.lang.IllegalArgumentException: no product found java.lang.IllegalArgumentException:找不到命名查询: - java.lang.IllegalArgumentException: Named query not found: 得到这个异常:java.lang.IllegalArgumentException:找不到 Retrofit 注释。 (参数#2) - Got this Exception: java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #2) java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException 为什么@Cacheable注释会导致java.lang.IllegalArgumentException? - Why @Cacheable annotation leads to java.lang.IllegalArgumentException? java.lang.IllegalArgumentException:error引用的类型不是注释类型 - java.lang.IllegalArgumentException: error Type referred to is not an annotation type java.lang.IllegalArgumentException:找不到ID为0x1020011的视图 - java.lang.IllegalArgumentException: No view found for id 0x1020011 java.lang.IllegalArgumentException:WASX7122E:未找到预期的“-” - java.lang.IllegalArgumentException: WASX7122E: Expected "-" not found JPA java.lang.IllegalArgumentException:未找到名称的 NamedQuery - JPA java.lang.IllegalArgumentException: NamedQuery of name not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM