0

Spring JDBC allows to specify in properties file for PROD:

jdbc.driverClassName = oracle.jdbc.OracleDriver
jdbc.url = jdbc:oracle:thin:@...

and for tests

jdbc.driverClassName = org.h2.Driver
jdbc.url = jdbc:h2:mem:test;INIT=...

Thus it's possible to instantiate needed java.sql.DataSource instance depends of configuration settings with generic code

@Bean
public DataSource dataSource(
        @Value("${jdbc.driverClassName}") String driverClass,
        @Value("${jdbc.url}") String url,
        @Value("${jdbc.username}") String username,
        @Value("${jdbc.password}") String password
) {
    DriverManagerDataSource dataSource = new DriverManagerDataSource(url, username, password);
    dataSource.setDriverClassName(driverClass);
    return dataSource;
}

Is it possible in Spring to configure specific type of java.jms.ConnectionFactory via driver and URL properties' strings like in Spring JDBC?

Actually, my goal is to use Tibco connection factory for PROD and ActiveMQ for tests.

Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41

1 Answers1

0

You can use spring profiles to pull in a different Bean for tests or you can simply override the connection factory bean with a different one in the test case.

EDIT

@Bean
public FactoryBean<ConnectionFactory> connectionFactory(@Value("${jms.url}") final String urlProp) {
    return new AbstractFactoryBean<ConnectionFactory>() {

        @Override
        public Class<?> getObjectType() {
            if (urlProp.startsWith("activemq:")) {
                return ActiveMQConnectionFactory.class;
            }
            // ...
            throw new RuntimeException("bad url: " + urlProp);
        }

        @Override
        protected ConnectionFactory createInstance() throws Exception {
            if (urlProp.startsWith("activemq:")) {
                URI uri = new URI(urlProp.substring(urlProp.indexOf(":") + 1));
                return new ActiveMQConnectionFactory(uri);
            }
            // ...
            throw new RuntimeException("bad url: " + urlProp);
        }

    };

}

and

jms.url=activemq:vm://localhost
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • This is exactly what we do. But now I would like to have more flexibly solution. – Andriy Kryvtsun Aug 17 '17 at 22:37
  • More flexible in what way? You can create a simple factory bean to return a different implementation based on a property value. But it seems overkill to me. – Gary Russell Aug 17 '17 at 22:42
  • Our app is kind of a black box with config in properties. For implementing end-to-end testing it's good to have ability to change infra implementations with only properties file change. – Andriy Kryvtsun Aug 18 '17 at 14:40