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. 集成学习——XGBoost(手推公式)

  2. 揭秘GaussDB(for Redis):全面对比Codis

    摘要:Codis集群在国内Redis生态圈很流行,社区已停止维护.本文从架构和特性两方面对比,带你感受华为云GaussDB(for Redis)的全新价值. 本文分享自华为云社区<华为云Gaus ...

  3. 学习C4C的视频分享

  4. win10设置Python程序定时运行(设置计划任务)

    今天来设置一下定时执行Pycharm内的脚本: 这个要基于win10 的任务计划程序(设置 > 控制面板 > 系统和安全 > 管理工具 > 任务计划程序) 1. create ...

  5. eclipse使用小记录

    (手动狗头)之前用eclipse的时候左侧的project栏不知道为什么整没了....记录一下 1.击Window--how View--other 2.Project Explorer,就可以了

  6. CMP0065警告问题

    参考链接: https://cmake.org/cmake/help/latest/policy/CMP0065.html https://cmake-developers.cmake.narkive ...

  7. 基础数学知识 / Math(updating)

    埃氏筛:朴素筛法求素数,o(nloglogn) int prime[N], tot; bool st[N]; // true:not prime, false:is prime void get_pr ...

  8. 在oracle中创建管理员密码

    1.因为在安装Oracle11g时没有设置sys和system用户的密码,导致登陆不上SQLplus,后面用sqlplus / as sysdba ,密码为:root登陆上去创建了密码. 2.如下图

  9. Python 实现列表与二叉树相互转换并打印二叉树封装类-详细注释+完美对齐

    # Python 实现列表与二叉树相互转换并打印二叉树封装类-详细注释+完美对齐 from binarytree import build import random # https://www.cn ...

  10. Luogu1038 神经网络 (拓扑排序)

    拓扑排序,裸的,水的. 第一发:题读错,输出错,输入错,到处错 \(\longrightarrow\) 40pts (excuse me ?) 第二发:漏了输入层特判 \(\longrightarro ...