Spring bean properties of imported Jar are overridden by application bean definition

Tags: , ,



I have a class with same name in the imported jar file.

@Configuration
@ConfigurationProperties(prefix = "queues")
public class QueueProperties {
String queuename;
String queuemanager;
//Rest code
}

The same class with the same properties also there in the jar file.

I have given bean definition in my configuration file for the jar bean.

@Bean
public com.jar.class.path getQueueProperties() {
    return new com.jar.class.path.QueueProperties();
}

But when the application started it is using the properties of my bean class instead of the properties defined for jar class bean.

Property values are kept in application.yml file.

queue:
  queueManager: 'queuemanager'
  queuename: 'queuename'

jar:
 class: 
   queue:
     queueManager: 'queuemanager'
     queuename: 'queuename'

I want to use properties defined in yml for jar file bean. Could there be any solution around this?

Answer

You can use Spring profile.

queue:
  queueManager: 'queuemanager'
  queuename: 'queuename'
---
spring:
  profiles: jar
queue:
  queueManager: 'queuemanager'
  queuename: 'queuename'

You can use –spring.profiles.active=jar on command line to use your jar profile.

You should not use configuration as normal beans, do this:

@Configuration
public class MyConfiguration {

    
    @Bean
    @ConfigurationProperties(prefix = "queue")
    public com.jar.class.path getQueueProperties() {
        return new com.jar.class.path.QueueProperties();
    }

}

public class QueueProperties {
    String queuename;
    String queuemanager;
    //Rest code
}


Source: stackoverflow