org.linuxprobe.spring.boot.start.MybatisUniversalCrudAutoConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-universal-crud-spring-boot-starter Show documentation
Show all versions of mybatis-universal-crud-spring-boot-starter Show documentation
mybatis-universal-crud-spring-boot-starter
package org.linuxprobe.spring.boot.start;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.linuxprobe.crud.mybatis.session.UniversalCrudConfiguration;
import org.linuxprobe.crud.mybatis.session.UniversalCrudSqlSessionFactory;
import org.linuxprobe.crud.mybatis.spring.UniversalCrudSqlSessionFactoryBean;
import org.linuxprobe.crud.mybatis.spring.UniversalCrudSqlSessionTemplate;
import org.mybatis.spring.mapper.ClassPathMapperScanner;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@Configuration
@ConditionalOnClass({ UniversalCrudSqlSessionFactoryBean.class, UniversalCrudSqlSessionFactory.class })
@ConditionalOnBean(value = { DataSource.class })
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSource.class, DataSourceAutoConfiguration.class })
public class MybatisUniversalCrudAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(MybatisUniversalCrudAutoConfiguration.class);
private MybatisProperties mybatisProperties;
private ResourceLoader resourceLoader;
private DataSource dataSource;
private final Interceptor[] interceptors;
private final DatabaseIdProvider databaseIdProvider;
private final List configurationCustomizers;
public MybatisUniversalCrudAutoConfiguration(MybatisProperties mybatisProperties, ResourceLoader resourceLoader,
DataSource dataSource, ObjectProvider interceptorsProvider,
ObjectProvider databaseIdProvider,
ObjectProvider> configurationCustomizersProvider) {
this.mybatisProperties = mybatisProperties;
this.resourceLoader = resourceLoader;
this.dataSource = dataSource;
this.interceptors = interceptorsProvider.getIfAvailable();
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
}
@PostConstruct
public void checkConfigFileExists() {
if (this.mybatisProperties.isCheckConfigLocation()
&& StringUtils.hasText(this.mybatisProperties.getConfigLocation())) {
Resource resource = this.resourceLoader.getResource(this.mybatisProperties.getConfigLocation());
Assert.state(resource.exists(), "Cannot find config location: " + resource
+ " (please add config file or check your Mybatis configuration)");
}
}
@Bean
@ConditionalOnMissingBean
public UniversalCrudSqlSessionFactory sqlSessionFactory() throws Exception {
UniversalCrudSqlSessionFactoryBean factory = new UniversalCrudSqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
factory.setUniversalCrudScan(mybatisProperties.getUniversalCrudScan());
Resource configLocationResources = new PathMatchingResourcePatternResolver()
.getResource(mybatisProperties.getConfigLocation());
factory.setConfigLocation(configLocationResources);
if (StringUtils.hasText(this.mybatisProperties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.mybatisProperties.getConfigLocation()));
}
UniversalCrudConfiguration configuration = this.mybatisProperties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.mybatisProperties.getConfigLocation())) {
configuration = new UniversalCrudConfiguration();
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}
factory.setConfiguration(configuration);
if (this.mybatisProperties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.mybatisProperties.getConfigurationProperties());
}
if (!ObjectUtils.isEmpty(this.mybatisProperties)) {
factory.setPlugins(this.interceptors);
}
if (this.databaseIdProvider != null) {
factory.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.mybatisProperties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(this.mybatisProperties.getTypeAliasesPackage());
}
if (StringUtils.hasLength(this.mybatisProperties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.mybatisProperties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.mybatisProperties.resolveMapperLocations())) {
factory.setMapperLocations(this.mybatisProperties.resolveMapperLocations());
}
return factory.getObject();
}
@Bean
@ConditionalOnMissingBean
public UniversalCrudSqlSessionTemplate sqlSessionTemplate(UniversalCrudSqlSessionFactory sqlSessionFactory) {
UniversalCrudSqlSessionTemplate bean = null;
try {
bean = new UniversalCrudSqlSessionTemplate(sqlSessionFactory);
} catch (Exception e) {
throw new RuntimeException(e);
}
return bean;
}
public static class AutoConfiguredMapperScannerRegistrar
implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {
private ResourceLoader resourceLoader;
private BeanFactory beanFactory;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
logger.debug("Searching for mappers annotated with @Mapper");
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
try {
if (this.resourceLoader != null) {
scanner.setResourceLoader(this.resourceLoader);
}
List packages = AutoConfigurationPackages.get(this.beanFactory);
if (logger.isDebugEnabled()) {
for (String pkg : packages) {
logger.debug("Using auto-configuration base package '{}'", pkg);
}
}
scanner.setAnnotationClass(Mapper.class);
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(packages));
} catch (IllegalStateException ex) {
logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
}
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
}
@Configuration
@Import({ AutoConfiguredMapperScannerRegistrar.class })
@ConditionalOnMissingBean(MapperFactoryBean.class)
public static class MapperScannerRegistrarNotFoundConfiguration {
@PostConstruct
public void afterPropertiesSet() {
logger.debug("No {} found.", MapperFactoryBean.class.getName());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy