自定义mybatis-starter工程,其他工程引用,但是mybatis-starter中的配置文件不生效的问题解决

自定义mybatis-starter工程,其他工程引用,但是mybatis-starter中的配置文件不生效的问题解决

解决思路

  • 在spring容器初始化完成,还未使用bean的时候
  • 取出spring的环境配置文件,在配置文件集合中添加进我们自定义的配置文件

BeanFactoryPostProcessor

BeanFactoryPostProcessor接口是针对bean容器的,它的实现类可以在当前BeanFactory初始化(spring容器加载bean定义文件)后,bean实例化之前修改bean的定义属性,达到影响之后实例化bean的效果。
也就是说,Spring允许BeanFactoryPostProcessor在容器实例化任何其它bean之前读取配置元数据,并可以根据需要进行修改

@Component
public class SmsPropertySourcePostProcessor implements BeanFactoryPostProcessor{
    private final ResourceLoader resourceLoader;
    private final List<PropertySourceLoader> propertySourceLoaders;

    public SmsPropertySourcePostProcessor() {
        this.resourceLoader = new DefaultResourceLoader();
        this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        ConfigurableEnvironment environment = beanFactory.getBean(ConfigurableEnvironment.class);
        //取出环境配置文件
        MutablePropertySources propertySources = environment.getPropertySources();
        Map<String, PropertySourceLoader> loaderMap = new HashMap<>(16);

        for (PropertySourceLoader loader : propertySourceLoaders) {
            String[] loaderExtensions = loader.getFileExtensions();
            for (String extension : loaderExtensions) {
                loaderMap.put(extension, loader);
            }
        }
        PropertySourceLoader sourceLoader = loaderMap.get("yaml");
        Assert.notNull(sourceLoader,"sourceLoader为null");
        Resource resource = resourceLoader.getResource("sms-mybatis.yml");
        Assert.notNull(sourceLoader,"sms-mybatis.yml未找到");
        try {
            List<PropertySource<?>> load = sourceLoader.load("applicationConfig: [classpath:/sms-mybatis.yml]", resource);
           //将自定义的配置文件添加进环境中
            propertySources.addLast(load.get(0));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

mybatis.yml

其他服务直接在pom文件中引用该starter即可
在这里插入图片描述

mybatis-plus:
  mapper-locations: classpath:com/mvc/sms/**/mapper/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.mvc.sms.**.dto
  #typeEnumsPackage: org.springblade.dashboard.entity.enums
  global-config:
    # 关闭MP3.0自带的banner
    banner: true
    db-config:
      #主键类型  0:"数据库ID自增", 1:"不操作", 2:"用户输入ID",3:"数字型snowflake", 4:"全局唯一ID UUID", 5:"字符串型snowflake";
      id-type: assign_id
      #字段策略
      insert-strategy: not_null
      update-strategy: not_null
      select-strategy: not_empty
      #驼峰下划线转换
      table-underline: true
      # 逻辑删除配置
      # 逻辑删除全局值(1表示已删除,这也是Mybatis Plus的默认配置)
      logic-delete-value: 1
      # 逻辑未删除全局值(0表示未删除,这也是Mybatis Plus的默认配置)
      logic-not-delete-value: 0
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false

iguration:
map-underscore-to-camel-case: true
cache-enabled: false


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值