cn.kduck.core.configuration.DaoConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kduck-core Show documentation
Show all versions of kduck-core Show documentation
The core of the K-Duck development framework encompasses all the featured components of the framework.
package cn.kduck.core.configuration;
import cn.kduck.core.KduckProperties;
import cn.kduck.core.KduckProperties.ShowSqlProperties;
import cn.kduck.core.dao.DefaultDeleteArchiveHandler;
import cn.kduck.core.dao.DeleteArchiveHandler;
import cn.kduck.core.dao.JdbcEntityDao;
import cn.kduck.core.dao.datasource.DataSourceMatcher;
import cn.kduck.core.dao.datasource.DataSourceSwitch;
import cn.kduck.core.dao.datasource.DynamicDataSource;
import cn.kduck.core.dao.datasource.condition.RequestMethodMatcher;
import cn.kduck.core.dao.definition.BeanDefDepository;
import cn.kduck.core.dao.definition.BeanDefSource;
import cn.kduck.core.dao.definition.MemoryBeanDefDepository;
import cn.kduck.core.dao.definition.impl.JdbcBeanDefSource;
import cn.kduck.core.dao.dialect.DatabaseDialect;
import cn.kduck.core.dao.id.IdGenerator;
import cn.kduck.core.dao.id.impl.KduckIdGenerator;
import cn.kduck.core.dao.id.impl.KduckIdGenerator.KduckSnowFlakeProperties;
import cn.kduck.core.dao.sqllog.ShowSqlLogger;
import cn.kduck.core.dao.sqllog.impl.DefaultShowSqlLogger;
import cn.kduck.core.utils.StringUtils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import javax.sql.DataSource;
import java.util.*;
import java.util.Map.Entry;
/**
* LiuHG
*/
public class DaoConfiguration {
private final KduckProperties kduckProperties;
public DaoConfiguration(KduckProperties kduckProperties){
this.kduckProperties = kduckProperties;
}
@Bean
@ConditionalOnMissingBean(IdGenerator.class)
public IdGenerator idGenerator(KduckSnowFlakeProperties kduckSnowFlakeProperties){
return new KduckIdGenerator(kduckSnowFlakeProperties.getServerIp(),kduckSnowFlakeProperties.getRegions());
}
@Bean
@ConditionalOnMissingBean(BeanDefDepository.class)
public BeanDefDepository beanDefDepository(@Lazy List beanDefSourceList){
return new MemoryBeanDefDepository(beanDefSourceList);
}
@Bean
@ConditionalOnMissingBean(BeanDefSource.class)
public BeanDefSource beanDefSource(DataSource dataSource){
JdbcBeanDefSource jdbcBeanDefSource = new JdbcBeanDefSource(dataSource,kduckProperties.getDefinition());
return jdbcBeanDefSource;
}
@Bean
@ConditionalOnMissingBean(DeleteArchiveHandler.class)
public DeleteArchiveHandler deleteArchiveHandler(){
return new DefaultDeleteArchiveHandler();
}
@Bean
@ConditionalOnMissingBean(JdbcEntityDao.class)
public JdbcEntityDao jdbcEntityDao(DataSource dataSource,List databaseDialectList,
@Autowired(required = false) ShowSqlLogger sqlLogger,
@Lazy DeleteArchiveHandler deleteArchiveHandler){
JdbcEntityDao jdbcEntityDao = new JdbcEntityDao(dataSource,databaseDialectList);
if(sqlLogger != null){
jdbcEntityDao.setSqlLogger(sqlLogger);
}
ShowSqlProperties showSqlProperties = kduckProperties.getShowSql();
if(showSqlProperties != null){
jdbcEntityDao.setShowSqlMode(showSqlProperties.getMode());
}
//deleteArchiveHandler由于使用了DefaultService进行了表数据持久,会导致循环依赖,因此需要标记为@Lazy懒加载
jdbcEntityDao.setDeleteArchiveHandler(deleteArchiveHandler);
return jdbcEntityDao;
}
@Bean
@ConditionalOnMissingBean(ShowSqlLogger.class)
@ConditionalOnProperty(prefix = "kduck.show-sql",name = "enabled",havingValue = "true")
public ShowSqlLogger showSqlLogger(){
return new DefaultShowSqlLogger(System.out,kduckProperties.getShowSql());
}
// @Bean
//// @Primary
// @ConfigurationProperties("spring.datasource")
// public DataSourceProperties dataSourceProperties() {
// return new DataSourceProperties();
// }
//
// @Bean
// @ConfigurationProperties("spring.datasource.hikari")
// public DataSource mainDataSource(DataSourceProperties properties) {
// return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
// }
@Bean
@Conditional(SpringDataSourceUnavailableCondition.class)
@Primary
public DataSource dataSource(KduckProperties properties) {
DynamicDataSource dataSource = new DynamicDataSource();
Map