org.zodiac.autoconfigure.datasource.reactive.ReactiveDataSourceConfigAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.datasource.reactive;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.zodiac.autoconfigure.datasource.DataSourceConfigProperties;
import org.zodiac.autoconfigure.datasource.condition.ConditionalOnDataSourceEnabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.datasource.config.DatabaseType;
import org.zodiac.datasource.r2dbc.reactive.ConnectionPoolWrapper;
import org.zodiac.datasource.r2dbc.util.R2dbcUtil;
@SpringBootConfiguration
@ConditionalOnDataSourceEnabled
public class ReactiveDataSourceConfigAutoConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveDataSourceConfigAutoConfiguration.class);
public ReactiveDataSourceConfigAutoConfiguration() {
}
static class ConnectionPoolBeanPostProcessor implements EnvironmentAware, BeanPostProcessor, Ordered {
private Environment environment;
private DataSourceConfigProperties dataSourceConfigProperties;
public ConnectionPoolBeanPostProcessor(DataSourceConfigProperties dataSourceConfigProperties) {
this.dataSourceConfigProperties = dataSourceConfigProperties;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof ConnectionPoolWrapper) {
io.r2dbc.pool.ConnectionPool connectionPool = (io.r2dbc.pool.ConnectionPool)bean;
Map dataSources = R2dbcUtil.getR2dbcDataSourcePoolBindings(environment);
if (!dataSources.isEmpty() && dataSources.containsKey(R2dbcUtil.parseDataSourceKey(beanName))) {
Binder.get(environment).bind(
String.format("%s.%s", SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_R2DBC_POOL_PREFIX, R2dbcUtil.parseDataSourceKey(beanName)),
Bindable.ofInstance(connectionPool));
}
DatabaseType databaseType = dataSourceConfigProperties.getDatabaseType();
//nettyLogger.info("ConfigDataSource: {}, url={}", beanName, connectionPool.);
}
return bean;
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
}