SpringBoot多重属性文件配置方案笔记

  1. 需要重写PropertyPlaceholderConfigurer

  2. 同时要忽略DataSourceAutoConfiguration

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    //@EnableTransactionManagement 2. 尝试xml配置事务
    @ImportResource(locations = {"classpath:spring-tx.xml"})
    public class DemoTransactionApplication { public static void main(String[] args) {
    SpringApplication.run(DemoTransactionApplication.class, args);
    } }
  3. 由于忽略了DataSourceAutoConfiguration,所以要配置datasource的Bean,和sqlSessionFactory 的Bean

    @Configuration
    @MapperScan(basePackages = {MyBatisConfig.MAPPER_PACKAGE}, sqlSessionFactoryRef = MyBatisConfig.SESSIONFACTORY_NAME)
    public class MyBatisConfig { /**SqlSessionFactory名称.*/
    public final static String SESSIONFACTORY_NAME = "sqlSessionFactory";
    /**mapper包路径,必须与其他SqlSessionFactory-mapper路径区分.*/
    public final static String MAPPER_PACKAGE = "com.grady.demotransaction.mapper";
    /**mapper.xml文件路径,必须与其他SqlSessionFactory-mapper路径区分.*/
    public final static String MAPPER_XML_PATH = "classpath:mapper/*.xml"; @Value("${spring.datasource.url}")
    private String datasourceUrl; @Value("${spring.datasource.driver-class-name}")
    private String driverClassName; @Value("${spring.datasource.username}")
    private String userName; @Value("${spring.datasource.password}")
    private String password; @Bean(name = "dataSource")
    public DataSource dataSource() {
    //建议封装成单独的类
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(datasourceUrl);
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
    return dataSource;
    } //默认Bean首字母小写,简化配置
    //将SqlSessionFactory作为Bean注入到Spring容器中,成为配置一部分。
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_XML_PATH));
    return sqlSessionFactoryBean.getObject();
    }
    }
    1. 这样就可以将本来都写在application.properties中的配置拆成多个了(这里是两个)

      application.properties

      spring.main.allow-bean-definition-overriding=true
      server.port=8080
      mybatis.config-location=classpath:config/mybatis-config.xml
      mybatis.mapper-locations=classpath:mapper/*.xml

      datasource.properties

      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
      spring.datasource.url=jdbc:mysql://localhost/sampledb
      spring.datasource.username=root
      spring.datasource.password=123456

SpringBoot多重属性文件配置方案笔记的更多相关文章

  1. Springboot多属性文件配置

    Springboot 多属性文件配置 配置文件后缀有两种: .properties和.yml 要完成多属性配置需要自定义PropertySourcesPlaceholderConfigurer 这个B ...

  2. 初识spring boot maven管理--属性文件配置

    在使用springboot的时候可以使用属性文件配置对属性值进行动态配置,官方文档原文如下: Spring Boot uses a very particular PropertySource ord ...

  3. 使用外部属性文件配置Bean以及Bean的生命周期方法

    1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...

  4. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  5. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  6. VS2010默认属性文件配置

    问题: 在VS2010中,同一个解决方案下有多个项目,都需要使用某一个库. 如果项目比较多,或者编译链接环境属性变动频繁,分别对项目进行配置就很麻烦. 解决: 在VS的配置文件中统一配置属性: 我的配 ...

  7. SpringBoot多profile文件配置

    1.多Profile文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml默认使用application.properties的配置: ...

  8. 黄聪:MYSQL5.6缓存性能优化my.ini文件配置方案

    使用MYSQL版本:5.6 [client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=10 ...

  9. spring常用的连接池属性文件配置

    (一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...

随机推荐

  1. SAP 实例 6 HTML input

    REPORT demo_html_input. CLASS demo DEFINITION. PUBLIC SECTION. CLASS-METHODS main. PRIVATE SECTION. ...

  2. vue 的常用事件

    vue 的常用事件 事件处理 1.使用 v-on:xxx 或 @xxx 绑定事件,其中 xxx 是事件名: 2.事件的回调需要配置在 methods 对象中,最终会在 vm 上: 3.methods ...

  3. 多校联训 DP 专题

    [UR #20]跳蚤电话 将加边变为加点,方案数为 \((n-1)!\) 除以一个数,\(dp\) 每种方案要除的数之和即可. 点击查看代码 #include<bits/stdc++.h> ...

  4. Tapdata 携手精诚瑞宝,共拓 Real Time DaaS 蓝海市场

      2021年10月22日,深圳钛铂数据有限公司「Tapdata」 与精诚瑞宝计算机系统有限公司「精诚瑞宝」战略合作签约仪式在深圳举行,Tapdata 创始人唐建法先生与精诚瑞宝副总经理余灿雄先生签署 ...

  5. 字符输入流_Reader类&FileReader类介绍和字符输入读取字符数据

    java.io.Reader:字符输入流的最顶层父类,其中定义了一些共性的成员方法,是一个抽象类共性的成员方法 int read() 读取单个字符并返回 int read(char[] cbuf) 将 ...

  6. CSS进阶内容——布局技巧和细节修饰

    CSS进阶内容--布局技巧和细节修饰 我们在之前的文章中已经掌握了CSS的大部分内容,但仍有一些内容我们没有涉略,这篇文章就是为了补充前面没有涉及的内容,为我们的知识做出补充并且介绍一些布局技巧 当然 ...

  7. 华为云Stack南向开放框架,帮助生态伙伴高效入云

    摘要:CloudBonder的生态社区通过一系列生态项目,解决提交叉组合.架构分层不清晰.运维界面不清晰等问题,简化对接流程,降低生态伙伴对接成本,缩短对接时间. 本文分享自华为云社区<[华为云 ...

  8. Docker安装及基础命令

    一.docker安装 1.安装docker #关闭防火墙和selinux systemctl stop firewalld.service setenforce 0  ​ #安装依赖包 yum ins ...

  9. 递归概念&分类&注意事项和使用递归计算1-n之间的和

    递归 概述 递归:指在当前方法内调用自己的这种现象. 递归的分类: 递归分为两种,直接递归和间接递归 直接递归称为方法自身调用自己 简介递归可以A方法调用B方法,B方法调用C方法,C方法调用A方法 注 ...

  10. 手把手带你实现基于 Vite+Vue3 的在线Excel表格系统

    今天,葡萄带你了解如何基于Vite+Vue3实现一套纯前端在线表格系统. 在正式开始项目介绍之前,首先咱们首先来介绍一下Vite和Vue3. Vue3 2020年09月18日Vue.js 3.0发布, ...