前言:如果你一点spring的基础没有,建议你不要学习springboot,至少先有一个spring的项目经验或者自己搭建过spring的项目再学习springboot,这样你会发现在spring中搞不懂的,在springboot中得到一些答案。springboot的原则是“约定大于配置”,所以在使用springboot的时候如果出现问题,没有一点基础,解决问题就很困难。

  目标:将spring的容器中的配置:数据库的配置,定时器的配置转换到springboot中,实现spring与springbooot的配置对接。

  数据库的配置转换:

  spring中数据库连接的配置如下

<!--数据库实例-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.mybatis.driver}" />
<property name="url" value="${jdbc.mybatis.url}" />
<property name="username" value="${jdbc.mybatis.username}" />
<property name="password" value="${jdbc.mybatis.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="10" />
<!-- 连接池最大数量 -->
<property name="maxActive" value="1000" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="30" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="10" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="2000" />
</bean>  

pringboot中的配置

	@Bean(name = "dataSource")
public BasicDataSource myGetDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setPassword(passWord);
dataSource.setUsername(userName);
dataSource.setMaxIdle(2);
dataSource.setMaxActive(20);
dataSource.setMaxWait(1000);
dataSource.setInitialSize(2); //
dataSource.setValidationQuery("SELECT 1");
dataSource.setRemoveAbandoned(true);
dataSource.setTestWhileIdle(true);
dataSource.setTimeBetweenEvictionRunsMillis(30000);
dataSource.setNumTestsPerEvictionRun(30);
dataSource.setMinEvictableIdleTimeMillis(1800000);
return dataSource;
}

spring 中的配置

 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:springMVCmybatis/com/my/mapper/*.xml" />
<!-- <property name="typeAliasesPackage" value="com.my.model"/> --> </bean>

 springboot配置sqlSessionFactoryBean,对应上面的sping的SqlSessionFactoryBean类

@Bean(name = "sqlSessionFactoryBean")
public SqlSessionFactoryBean myGetSqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // mapperLocations
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try {
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*Mapper.xml"));
} catch (IOException e) {
log.info("sqlSessionFactoryBean的setMapperLocations有问题");
e.printStackTrace();
} // dataSource sqlSessionFactoryBean.setDataSource(dataSource); // SqlSessionFactory sessionFactory = sqlSessionFactoryBean.getObject(); return sqlSessionFactoryBean; }

 spring中的配置

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="springMVCmybatis" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>

 springboot中的配置

package com.my.myconfigure;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration; //<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
//<property name="basePackage" value="springMVCmybatis" />
//<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
//
//</bean> @Configuration
// 这个注释是需要在加载MybatisDbConfigure.class之后再加载MapperScanConfig这个类
@AutoConfigureAfter(MybatisDbConfigure.class)
public class MapperScanConfig { public MapperScannerConfigurer myGetMapperScannerConfigurer() {
MapperScannerConfigurer myMapperScannerConfigurer = new MapperScannerConfigurer(); myMapperScannerConfigurer.setBasePackage("com.my.dao");
myMapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
return myMapperScannerConfigurer;
} }

 

结论:springboot是通过@Configuration来标注自定义配置,配置中使用@Bean来添加类作用与spring配置中的.xml文件作用一样,两者都是容器的作用。

关于这部分配置已经上传到我的github上,感兴趣或者不懂得,可以登录查看。

  

springboot自定义配置文件的更多相关文章

  1. springboot + 自定义配置文件读取

    新建一个配置文件 src\main\resources\resources\config.properties #自定义配置文件 #System Encoding #File Upload Temp ...

  2. SpringBoot(二): SpringBoot属性配置文件 SpringBoot多环境配置文件 SpringBoot自定义配置文件

    1.属性配置文件 一共分为两种,一种是键值对的properties属性配置文件,一种是yaml格式的配置文件 properties配置: 2.多环境配置文件 当我们的项目中有多套配置文件 比如开发的配 ...

  3. SpringBoot 自定义配置文件不会自动提示问题

    参阅:https://www.jianshu.com/p/ec3f0b0371e6

  4. Springboot读取配置文件及自定义配置文件

    1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...

  5. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

  6. SpringBoot之加载自定义配置文件

    SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定. J ...

  7. Springboot 之 自定义配置文件及读取配置文件

    本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...

  8. Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)

    注意:配置文件中的字符串不要有下划线 .配置中  key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED      =  ABAP_AS_WITH_P ...

  9. Springboot读取自定义配置文件的几种方法

    一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...

随机推荐

  1. 隐藏Android下的虚拟按键

    要隐藏Android下的虚拟按键,可通过如下办法操作 adb root adb remount adb shell ls -al /system/build.prop   (查看文件权限) -rw-r ...

  2. linux install jupyter notebook

    install sudo pip install jupyter notebook start sudo jupyter notebook 一般,文件目录默认在你启动的位置.你可以在notebook里 ...

  3. Jquery的树插件jqxTreeGrid的使用小结(实现基本的增删查改操作)

    一.引入相应的js <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" t ...

  4. MongoDB 自动分片 auto sharding

    MongoDB部署实验系列文章 MongoDB做为NoSQL数据库,最近几年持续升温,越来越多的企业都开始尝试用MongoDB代替原有Database做一些事情.MongoDB也在集群,分片,复制上也 ...

  5. C语言源字符集与执行字符集

    参考: http://blog.csdn.net/yucan1001/article/details/7188267   http://blog.csdn.net/dbzhang800/article ...

  6. 一些使用的linux库

    1.curl网络库 apt-get install libcurl4-openssl-dev sudo apt-get install curl 2.jsoncpp库 sudo apt-get ins ...

  7. Java Thread系列(五)synchronized

    Java Thread系列(五)synchronized synchronized锁重入 关键字 synchronized 拥有锁重入的功能,也就是在使用 synchronized 时,当线程等到一个 ...

  8. qt学习(一) qt安装

    QT5现在安装已经方便了许多 相比QT4 可以一路点击到底 无需额外的太多的操作. http://download.qt.io/official_releases/ 下载 1 windows下可以选择 ...

  9. Eclipse下初用lucene

    lucene是apache的一个开源项目,一个开放源代码的全文检索引擎工具包. 1. 首先下载lucene,下载地址来自<lucene实战>第2版(页面加载比较忙,等~) http://w ...

  10. HBase & thrift & C++编程

    目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 1 2.3. 启动参数 2 3. hbase.thrift 2 3. ...