Poonkodi Sivapragasam
Poonkodi Sivapragasam

Reputation: 1317

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. I'm using spring boot to run my project in STS, I'm going through below error while starting the spring boot application.

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

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

Upvotes: 13

Views: 22990

Answers (2)

epox
epox

Reputation: 10890

Short instructions:

  1. remove @EnableConfigurationProperties annotation
  2. init config properties POJOs directly as beans

.

  @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


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. So you don't need any @EnableProperties, or @ConfigurationProperties annotations. 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:

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

The POJO properties class has setters with the same as properties names:

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

Upvotes: 6

Kedar Joshi
Kedar Joshi

Reputation: 1511

@EnableConfigurationProperties annotation expects all the classes provided, in argument, should be annotated with @ConfigurationProperties.

Annotate the class ContractClientConfig with @ConfigurationProperties and it should work.

Upvotes: 12

Related Questions