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. ABAP CDS - DEFINE VIEW, name_list

    Syntax ... ( name1, name2, ... ) ... Effect Defines the element names of a CDS view in ABAP CDS in a ...

  2. sql-sql优化

    SQL执行流程 a. 编写过程: select dinstinct .. from .. join .. on .. where .. group by .. having .. order by . ...

  3. 零基础学Python:元组(Tuple)详细教程

    Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可https://jq.qq.com/?_wv=1 ...

  4. Java 向数组中添加元素

    一般数组是不能添加元素的,因为他们在初始化时就已定好长度了,不能改变长度. 向数组中添加元素思路 第一步:把数组转化为集合 list = Arrays.asList(array); 第二步:向集合中添 ...

  5. Chrome安装Vue.js devtool F12无效

    要安装 vue-devtools-4.1.4_0 链接: https://pan.baidu.com/s/1aeUxKJEUDW0U_i6uuAZFvQ 提取码: 4btc vue-devtools- ...

  6. [System.OutOfMemoryException] {函数求值已禁用,因为出现内存不足异常。

    [System.OutOfMemoryException] {函数求值已禁用,因为出现内存不足异常. StringBuilder 赋值的时候超过内存的大小,要即时去清空文本的值. 也可能是DataSe ...

  7. C++算数运算符和位运算符

    C++根据功能和用途将运算符分为算数运算符.位运算符.关系运算符和逻辑运算符等不同类型.四种不同运算符的优先级从大到小依次位算-位-关-逻. 一.算数运算符 1.加减乘除(+ - * /) 加减乘除位 ...

  8. docker容器数据管理

    Docker容器数据卷 Docker中的数据可以存储在类似于虚拟机磁盘的介质中,在Docker中称为数据卷(Data Volume). 数据卷可以用来存储Docker应用的数据,也可以用来在Docke ...

  9. harbor之HTTPS安装

    1.下载解压 # tar -xvf harbor-offline-installer-v1.7.6.tgz # cd /harbror 2.下载python2.7 # apt install pyth ...

  10. for_in循环

    for-in循环也可以简单称为for循环 in表达从(字符串,序列等)中依次取值,又称为遍历(全部都要取到) for-in遍历的对象必须是可迭代对象 目前可以简单认为只有字符串和序列是可迭代对象 它是 ...