java.lang.IllegalArgumentException:找不到ConfigurationProperties批注

时间:2018-02-22 18:16:01

标签: java spring spring-boot

我在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)

context.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

2 个答案:

答案 0 :(得分:1)

简短说明:

  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

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


详细信息:

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

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

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

POJO属性类具有与属性名称相同的设置器:

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; }

答案 1 :(得分:0)

@EnableConfigurationProperties注释要求在参数中提供的所有类都应使用@ConfigurationProperties进行注释。

使用ContractClientConfig注释班级@ConfigurationProperties,它应该有效。