Home > Net >  @Value annotation unable to read properties file in spring boot camel application
@Value annotation unable to read properties file in spring boot camel application

Time:11-03

I have a spring-boot application where I read data from queue and send data to transformation class using .bean()

Integration.java

class Integration {

    @Value("${someURL}")
    private String someURL; //able to read someURL from property file

    from("queue")
    // some intermediate code

    .bean(new TransformationClass(), "transformationMethod")

    // other code

}

Now, Inside TransformationClass I have @Value annotation to read values from properties file but it always returns a null.

TransformationClass.java

@Component
class TransformationClass {

    @Value("${someURL}")
    private String someURL; //someURL return null though there is key-value associated in props file.

    public void transformationMethod(Exchange exchange) {
        // other stuff related to someURL
    }
}

Note - I am able to read values from property file in class Integration.java but unable to read from class TransformationClass.java

I am using spring boot version - 2.7.2 and camel version - 3.18.1 jdk - 17

I tried to read using camel PropertiesComponent but it did not worked.

CodePudding user response:

Problem here is, that new TransformationClass() is not a "spring managed instance", thus all @Autowire/Value/Inject/...s have no effect.

Since TransformationClass is (singleton, spring-managed) @Component and is needed by Integration, wiring these makes sense:

Via field... :

class Integration {
   @Autowired
   private TransformationClass trnsObject;
   // ...

Or constructor injection:

class Integration {

   private final TransformationClass trnsObject;

   public Integration(/*im- /explicitely @Autowired*/ TransformationClass pTrnsObject) {
      trnsObject = pTrnsObject;
   }
   // ...
   // then:
       doSomethingWith(trnsObject); // has correct @Values
}
  • Related