0

I have created below bean in spring 4. Currently declared HOST and other variable in this below configuration file as static. But want to get it from database dynamically. Not able to find a way how to get it.

Please suggest what is the way to get it from database.

@Configuration
public class RabbitMqConfiguration {

    public static final String HOST = "localhost";
    public static final String USERNAME = "test";
    public static final String PASSWORD = "test";
    public static final int CHANNEL_CACHE_SIZE = 25;
    public static final int CONNECTION_CLOSE_TIMEOUT = 30000;
    public static final int CONNECTION_CACHE_SIZE = 1;
    public static final int REQUEST_HEART_BEAT = 0; 

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
        connectionFactory.setUsername(USERNAME);
        connectionFactory.setPassword(PASSWORD);
        connectionFactory.setCloseTimeout(CONNECTION_CLOSE_TIMEOUT);
        connectionFactory.setChannelCacheSize(CHANNEL_CACHE_SIZE);
        connectionFactory.setConnectionCacheSize(CONNECTION_CACHE_SIZE);
        connectionFactory.setRequestedHeartBeat(REQUEST_HEART_BEAT);
        return connectionFactory;
    }
}
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

1 Answers1

0

There are two ways to load the properties either using MutablePropertySources or store properties simply in HashMap.

The basic idea behind the solution is as below:

  1. Configure your database bean.
  2. Use @PostConstruct annotation on method to read the your messaging configuration from db.
  3. Autowire your DBConfiguration class in your RabbitMqConfiguration.
  4. Either edit the PropertySources suing MutablePropertySources or simply create HashMap to load the configuration.

You can take reference from MutablePropertySources or

loadConfigFromDB

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48