美文网首页
下线adb引发的怀疑人生

下线adb引发的怀疑人生

作者: 安然在路上 | 来源:发表于2022-06-10 16:49 被阅读0次

    最近在做一个数据库下线的需求,配置了多个数据源,要把数仓对应的默认数据源下线,涉及了很多和spring容器相关的注解.

    Spring 容器相关注解

    @Configuration:表明一个类是配置类,@Configuration标注的类等同于一个xml文件
    @Bean: @Bean标注的方法等同于xml文件里的一个标签
    @Primary
    @Resource byName 类型的依赖注入。使用方式:@Resource(name="xxBean"). 不带参数的 @Resource 默认值类名首字母小写
    @Autowired 其实就是 autowire=byType 就是根据类型的自动注入依赖,可以被使用在属性域,方法,构造函数上
    @Qualifier:autowire=byName, @Autowired注解判断多个bean类型相同时,就需要使用 @Qualifier("xxBean") 来指定依赖的bean的id
    @Configuration

    代码改造过程

    代码改造完成后,准备把adb对应的数据源注释掉,看程序哪里会报错,也许可以看到哪里没有改到。
    原来的写法:(主数据源)

    @Configuration
    public class OdsMybatisConfig {
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        @Primary
        public DataSource dataSource(){
            return DataSourceBuilder.create().type(DruidDataSource.class).build();
        }
    
        @Bean
        @Primary
        public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mybatis/mapper/**/*Mapper.xml"));
            return bean.getObject();
        }
    
        @Bean
        @Primary
        public DataSourceTransactionManager transactionManager(DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean
        @Primary
        public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

    使用主数据源:

    @Repository
    public class ImporterInformationDaoImpl extends BaseDaoImpl<ImporterInformation> implements ImporterInformationDao {
        @Resource
        ImporterInformationMapper importerInformationMapper;
    
        @Override
        protected BaseMapper<ImporterInformation> getBaseMapper() {
            return importerInformationMapper;
        }
    }
    

    使用非主数据源:

    @Repository("FbaConsignmentExport")
    public class FbaConsignmentExportData<R, T> implements ExportDataQueryDao<R, T> {
    
        @Resource
        @Qualifier("masterSqlSessionFactory")
        private SqlSessionTemplate masterSqlSessionTemplate;
    
        @Override
        public Integer count(R condition) {
            FbaConsignmentExportMapper mapper = masterSqlSessionTemplate.getMapper(FbaConsignmentExportMapper.class);
            return mapper.countExportFbaConsignment((FbaConsignmentExportCriteria) condition);
        }
    
        @Override
        public List<T> queryData(R condition) {
            FbaConsignmentExportMapper mapper = masterSqlSessionTemplate.getMapper(FbaConsignmentExportMapper.class);
            return (List<T>) mapper.exportFbaConsignment((FbaConsignmentExportCriteria) condition);
        }
    }
    

    注释掉主数据源的@Configuration之后,重启报skuMapper找不到SqlSessionFactory,各种尝试还是无解,包括升级mysql版本...
    大佬给我发来这样两行代码,竟然可以...

    @MapperScan(basePackages = "com.fpx.fb4.datawh.provider.mdata.sku.dao", sqlSessionFactoryRef = "mdataSlaveSqlSessionFactory")
    public class mdataSlaveMybatisConfig {
    

    疑问(未解决)

    1. 为什么这个tempate注入不进来



      怀疑是加载顺序的问题,配置类没装配完,就开始装配这里,只是怀疑...或者是template和mapper之间有问题

        @Resource
        @Qualifier("masterSqlSessionTemplate")
        private SqlSessionTemplate masterSqlSessionTemplate;
    
        private InventoryDiffMapper inventoryDiffMapper;
    
        @PostConstruct
        public void init(){
            inventoryDiffMapper = masterSqlSessionTemplate.getMapper(InventoryDiffMapper.class);
        }
    

    同事写了这样的代码,已经上线且运行良好,spring的生命周期要着重了解一下了~

    1. 这里masterSqlSessionTemplate注入masterSqlSessionFactory怎么能注入进来呢
    @Repository("FbaConsignmentExport")
    public class FbaConsignmentExportData<R, T> implements ExportDataQueryDao<R, T> {
    
        @Resource
        @Qualifier("masterSqlSessionFactory")
        private SqlSessionTemplate masterSqlSessionTemplate;
    
        @Override
        public Integer count(R condition) {
            FbaConsignmentExportMapper mapper = masterSqlSessionTemplate.getMapper(FbaConsignmentExportMapper.class);
            return mapper.countExportFbaConsignment((FbaConsignmentExportCriteria) condition);
        }
    
        @Override
        public List<T> queryData(R condition) {
            FbaConsignmentExportMapper mapper = masterSqlSessionTemplate.getMapper(FbaConsignmentExportMapper.class);
            return (List<T>) mapper.exportFbaConsignment((FbaConsignmentExportCriteria) condition);
        }
    }
    

    号外

    springboot的多数据源:https://www.imooc.com/article/details/id/44010

    你能抓住变化中不变的东西吗

    相关文章

      网友评论

          本文标题:下线adb引发的怀疑人生

          本文链接:https://www.haomeiwen.com/subject/qejbmrtx.html