springboot自定义配置文件
前言:如果你一点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自定义配置文件的更多相关文章
- springboot + 自定义配置文件读取
新建一个配置文件 src\main\resources\resources\config.properties #自定义配置文件 #System Encoding #File Upload Temp ...
- SpringBoot(二): SpringBoot属性配置文件 SpringBoot多环境配置文件 SpringBoot自定义配置文件
1.属性配置文件 一共分为两种,一种是键值对的properties属性配置文件,一种是yaml格式的配置文件 properties配置: 2.多环境配置文件 当我们的项目中有多套配置文件 比如开发的配 ...
- SpringBoot 自定义配置文件不会自动提示问题
参阅:https://www.jianshu.com/p/ec3f0b0371e6
- Springboot读取配置文件及自定义配置文件
1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
- SpringBoot之加载自定义配置文件
SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定. J ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
随机推荐
- 报错提示:mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in..的处理方式
PHP操作数据库的时候如果出现报错: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in.. ...
- UX术语详解:任务流,用户流,流程图以及其它全新术语
以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 用户体验拥有一长串专业的术语和可交付内容.当在线查看UX相关职位描述时,所罗列的这类术语更是 ...
- etl业务说明图
- c11时间库一个小例子
#pragma once #include <chrono> #include <string> #include <iostream> #include < ...
- cxf的一些使用说明
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agree ...
- medusa爆破路由
medusa –M http -h 192.168.10.1 -u admin -P /usr/share/wfuzz/ wordlist/fuzzdb/wordlists-user-passwd/p ...
- word 写博客,直接上传
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- 获取当前的window 以及设置其rootViewController
AppDelegate *app = [[UIApplication sharedApplication] delegate]; app.window.rootViewCont ...
- ZOJ2201 No Brainer 2017-04-16 19:21 54人阅读 评论(0) 收藏
No Brainer Time Limit: 2 Seconds Memory Limit: 65536 KB Zombies love to eat brains. Yum. Input ...
- 8.使用Exists监控ZNode的三大Change事件
一. zookeeper是一个分布式的协调程序(所有程序都是通过订阅它来相互感知) 1. tcp(长链接) + watcher server ->client client ->ser ...