springboot自定义配置文件
前言:如果你一点spring的基础没有,建议你不要学习springboot,至少先有一个spring的项目经验或者自己搭建过spring的项目再学习springboot,这样你会发现在spring中搞不懂的,在springboot中得到一些答案。springboot的原则是“约定大于配置”,所以在使用springboot的时候如果出现问题,没有一点基础,解决问题就很困难。
目标:将spring的容器中的配置:数据库的配置,定时器的配置转换到springboot中,实现spring与springbooot的配置对接。
数据库的配置转换:
spring中数据库连接的配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<br><!--数据库实例--> <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中的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@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 中的配置
1
2
3
4
5
6
7
|
<!-- 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类
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@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中的配置
1
2
3
4
5
|
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basePackage" value= "springMVCmybatis" /> <property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" /> </bean> |
springboot中的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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 <br> // 这个注释是需要在加载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文件作用一样,两者都是容器的作用。