关键词:
前言:如果你一点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文件作用一样,两者都是容器的作用。
微服务之springboot自定义配置application.properties文件
配置的文件的格式springboot可以识别两种格式的配置文件,分别是yml和properties文件。我们可以将application.properties文件换成application.yml,这两个文件都可以被SpringBoot自动识别并加载,但是如果是自定义的配置文件,就最好还是使... 查看详情
springboot读取自定义配置文件
自定义配置文件my-config.properties1bfxy.title=bfxy2bfxy.name=hellospringboot!3bfxy.attr=12345 配置文件类appconfig.java1importorg.springframework.beans.factory.annotation.Autowired;2importorg.springfram 查看详情
springboot自定义配置文件
前言:如果你一点spring的基础没有,建议你不要学习springboot,至少先有一个spring的项目经验或者自己搭建过spring的项目再学习springboot,这样你会发现在spring中搞不懂的,在springboot中得到一些答案。springboot的原则是“约定... 查看详情
java框架之springboot三:springboot自定义配置一
java框架之Springboot三:SpringBoot自定义配置一私有化配置文件刚才我们介绍了在主配置文件汇中配置对应的文件,如果我们想要自定义配置文件该怎么处理呢?现在就要给大家介绍我们的@PropertySource注解。@PropertyS... 查看详情
springboot自定义配置文件数据源
参考技术ASpringBoot支持动态的读取文件,留下的扩展接口org.springframework.boot.env.EnvironmentPostProcessor。这个接口是spring包下的,使用这个进行配置文件的集中管理,而不需要每个项目都去配置配置文件。这种方法也是springboot框架留... 查看详情
springboot系列配置文件详解
...定义数据配置1.通过prefix2.通过@value注解获取 引言:Springboot有一个全局配置文件,这个配置文件默认是properties文件,就是application.properties文件,其实还有一种文件 查看详情
在springboot下读取自定义properties配置文件的方法
SpringBoot工程默认读取application.properties配置文件。如果需要自定义properties文件,如何读取呢?一、在resource中新建.properties文件在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下。如图remote.properties... 查看详情
springboot获取配置文件的自定义参数
1、在application.properties中自定义参数spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8spring.datasource.username=rootsprin 查看详情
springboot引用自定义配置文件
依赖jar<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.0.5.RELEASE</version> 查看详情
springboot自定义配置
有时候需要自己定义一些配置,比如SpringBoot没有提供Druid连接池的配置,需要我们自己写配置。 配置文件#指定数据源类型为Druidspring.datasource.type=com.alibaba.druid.pool.DruidDataSource##########druid连接池配置#########spring.datasource.dr... 查看详情
springboot自定义配置
如果我们使用IntelliJIDEA进行新建工程,那么可以在项目中发现一个属性文件application.yml 我们可以在这个文件中修改配置比如tomcat 默认的端口是8080我们可以修改端口为 8088 server:port:8088tomcat:uri-encoding:UTF-8ma... 查看详情
java框架之springboot三:springboot自定义配置一
java框架之Springboot三:SpringBoot自定义配置一私有化配置文件刚才我们介绍了在主配置文件汇中配置对应的文件,如果我们想要自定义配置文件该怎么处理呢?现在就要给大家介绍我们的@PropertySource注解。@PropertyS... 查看详情
springboot中自定义properties文件配置参数并带有输入提示
1.创建配置类在项目中创建一个参数映射类如下@ConfigurationProperties(prefix="user.info")publicclassMyProperties{privateStringname;privateIntegerage;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name= 查看详情
springboot手写自定义配置
阅读之前建议先看springboot自定义配置应用详解demo@ConfigurationProperties(prefix="xulehuang.redis"//yml或者properties文件里面属性的前缀,前缀加具体属性名就对应RabbitProperties的属性名,值自然而然也会取到)@Configuratio... 查看详情
自定义jar配置文件问题?
java开发的简单工具类jar包,没有spring等任何框架,在springboot中引入使用,怎样灵活读取springboot的配置文件?或者说在以下情况下怎样都能读取到spingboot的配置文件:1、application.properties;2、application.yml;3、springboot打成jar包启... 查看详情
[springboot2019-10-23]自定义springboot自动配置
1.创建springboot3个关键类1.1属性读取类:用于从配置文件中读取配置信息packagecom.shijt.springbootdemo.jdbc;importorg.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix="mysql.jdbc")pu 查看详情
(c)springboot读取自定义配置文件时乱码解决办法
这是入门的第三天了,从简单的hellospring开始,已经慢慢接近web的样子。接下来当然是读取简单的对象属性了。于是按照网上各位大神教的,简单写了个controller,如上一篇,其他配置不需要做任何改动。@RestControllerpublicclassUserCon... 查看详情
springboot中的配置文件详解(ymlproperties全局配置和自定义配置有趣的banner图配置)(代码片段)
...配置文件全局配置文件能够对一些默认配置值进行修改。SpringBoot使用一个application.properties或者application.yaml的文件作为全局配置文件,该文件存放在src/main/resource目录或者类路径的/config,一般会选择resource目录。接下来,将针对... 查看详情