spring batch环境配置 spring batch in action

admin2024-05-30  23

spring 整合mybatis,无非主要两点:
1、spring、mybatis中connection整合
2、mapper接口的扫描(得自定义扫描类去扫描接口、因spring扫描类会过滤接口)、生成代理类

一、解决connection整合问题
1、spring整合mybatis基于配置类,需要指定datasource、SqlSessionFatoryBean(可以理解为SqlSessionFactoryBuilder、只不过功能比起更强大)

@Configuration
@ComponentScan(basePackages = "com")
@MapperScan(basePackages = "com.dao")
public class MybatisConfig{

    @Autowired
    private MybatisProperties mybatisProperties;
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(mybatisProperties.getDriver());
        dataSource.setUrl(mybatisProperties.getUrl());
        dataSource.setUsername(mybatisProperties.getUsername());
        dataSource.setPassword(mybatisProperties.getPassword());
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources(mybatisProperties.getMapperLocations());
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
        return sqlSessionFactoryBean;
    }
}

datasource作用主要就是获取连接

SqlSessionFactoryBean核心功能点:

1、解析的配置文件封装到Configuration(mybatis核心)

2、指定事物工厂这一步指定spring的事物工厂(主要整合connection)

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_Source,第1张

因其实现InitializiBean接口构建bean的时候会执行其afterPropertiesSet()进行构建SqlSessionFactory

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_sql_02,第2张

而在buildSqlSessionFactory()方法中会指定事物管理器工厂其内部会创建SpringManagedTransactionFactory

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_spring_03,第3张

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_mybatis_04,第4张

下面就看看SpringManagedTransactionFactory事物管理器获取connection时候从哪里获取的

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_sql_05,第5张

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_spring_06,第6张

可以看到connection是从事物管理器中获取的

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_sql_07,第7张

继续走下去看看从getResource获取什么

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_Source_08,第8张

而这个resource是个ThreadLocal这其中就是存放connection当业务从加@Transcation时会生成conncetion放到这个threadlocal中,所以拿到是同一个connection

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_spring batch环境配置_09,第9张

下面验证下这个resource中的connection什么时候放进去的,看下大概

(1)spring开启事物会往connectionholder绑定当前生成的connection,并把connectionholder放到resouce中即ThradLocal中

DataSourceTransactionManager#doBegin方法中会把生成的connectionholder放到resouce中即ThradLocal中

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_sql_10,第10张

下图可以看到已经绑定到resouce中了即ThreadLocal中、myabtis获取连接就是从这个resource中获取的,记住此时ConnctionHolder的id为16694上图可以看到

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_sql_11,第11张

下面看下执行到mybatis获取连接时可以看到从resouce中获取,而且ConnctionHolder的id为16694上图可以看到说明spring mybatis中conncetion已经整合

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_mybatis_12,第12张

二、spring是怎样把mapper接口生成代理放到容器中的
spring自带的扫描器只能扫描不是抽象类的、非接口的类才能扫描成bean定义
PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors
postProcessor.postProcessBeanDefinitionRegistry中后置处理器的调用过程回去扫描mapper接口

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_mybatis_13,第13张

下面扫描mapper接口封装成bean定义做了个偷天换日设置class为MapperFactoryBean用了创建代理具体创建过程可参考

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_spring batch环境配置_14,第14张

下面看看MapperFactoryBean对象、getObject创建代理(实现了FactoryBean接口)、具体创建细节同mybatis

spring batch环境配置 spring batch in action,spring batch环境配置 spring batch in action_Source_15,第15张


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!