繁体   English   中英

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

[英]java.lang.IllegalArgumentException: No ConfigurationProperties annotation found on

我在 context.xml 中的我的项目(菜单提供程序服务)中集成了一个外部项目合同服务客户端。 我正在使用 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 文件

<?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 应用程序类:

@SpringBootApplication
@EnableConfigurationProperties({ MenuProviderServiceConfig.class, CreateRatesConfig.class,CommonConfiguration.class, ContractClientConfig.class })
@Import({
    CommonConfiguration.class
})
@ImportResource({ "classpath:/menu-provider-service/context.xml" })
public class MenuProviderServiceApplication

@EnableConfigurationProperties注释期望所有提供的类在参数中都应该用@ConfigurationProperties注释。

使用@ConfigurationProperties注释ContractClientConfig类,它应该可以工作。

简短说明:

  1. 移除 @EnableConfigurationProperties 注释
  2. 将 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

参考: https : //docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-3rd-party-configuration


详情:

从外部(以及本地)项目重新使用 ConfigigurationPropertes 的更好方法是在 @Configuration 类中显式初始化属性对象。 所以你不需要任何 @EnableProperties 或 @ConfigurationProperties 注释。 尤其是在您无法控制的外部项目中。

上面的代码片段假设了两件事:您的application.yml具有app.my-menu.*属性:

app:
  my-menu:
    text: "Help"
    path: "/help/index.html"

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.

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