简体   繁体   中英

Configuring custom properties in a DB2 autoconfigured datasource

I am relying on Spring Boot to autoconfigure a DB2 data source using the standard configuration properties ( spring.datasource.* ).

The point is I need to set up a custom DB2 configuration property in the datasource. If I created the datasource as a bean manually I would define it with @Bean of type DB2SimpleDataSource and would set the value invoking the setter method offered by that class:

DB2SimpleDataSource db2DS = new DB2SimpleDataSource();
db2DS.setClientDebugInfo("xxx");

Is there any way to keep using the autoconfiguration and at the same time being able to configure that property?

I solved it adding a BeanPostProcessor like this:

@Configuration
@ConditionalOnClass(name = Constants.DB2_PRESENCE_TOKEN_CLASS)
@AutoConfigureAfter(value = {
        DataSourceAutoConfiguration.class})
@Slf4j
public class Db2CustomPropertiesAutoConfiguration implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.debug("bean {}", bean);
        if (bean instanceof DataSourceProxy && ((DataSource) bean).getDriverClassName().equals("com.ibm.db2.jcc.DB2Driver")) {
            ((DataSource) bean).getDbProperties().put("clientDebugInfo", "XXX")
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM