package com.lutao.cloud.common.datasource;

import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.lutao.cloud.common.datasource.config.DruidDataSourceProperties;
import com.lutao.cloud.common.datasource.config.JdbcDynamicDataSourceProvider;
import com.lutao.cloud.common.datasource.config.LastParamDsProcessor;
import com.lutao.cloud.common.datasource.config.WebMvcConfig;
import lombok.AllArgsConstructor;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**

 * @date 2020-02-06
 * <p>
 * 动态数据源切换配置
 */
@Configuration
@AllArgsConstructor
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DruidDataSourceProperties.class)
public class DynamicDataSourceAutoConfiguration{

	private final StringEncryptor stringEncryptor;

	private final DruidDataSourceProperties properties;

	@Bean
	public DynamicDataSourceProvider dynamicDataSourceProvider(){
		return new JdbcDynamicDataSourceProvider(stringEncryptor, properties);
	}

	@Bean
	public DsProcessor dsProcessor(){
		return new LastParamDsProcessor();
	}

	@Bean
	public WebMvcConfigurer webMvcConfigurer(){
		return new WebMvcConfig();
	}

}
