Preface : If you're a little bit spring There is no basis for , I suggest you don't study springboot, At least one spring Project experience or self built spring Project relearning springboot, So you'll find that in spring What I don't understand in English , stay springboot Get some answers in .springboot The principle of “ Convention over configuration ”, So it's using springboot If there's a problem when you're on the road , There's no foundation , It's very difficult to solve the problem .
The goal is : take spring Configuration in the container of : Configuration of database , The timer configuration is converted to springboot in , Realization spring And springbooot Configuration and docking of .
Database configuration conversion :
spring The configuration of the database connection in is as follows
<!-- Database instance -->
<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}" />
<!-- Initialize connection size -->
<property name="initialSize" value="10" />
<!-- Maximum number of connection pools -->
<property name="maxActive" value="1000" />
<!-- Connection pool maximum idle -->
<property name="maxIdle" value="30" />
<!-- Connection pool minimum idle -->
<property name="minIdle" value="10" />
<!-- Get connection maximum wait time -->
<property name="maxWait" value="2000" />
</bean>
pringboot Configuration in
@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 Configuration in
<!-- spring and MyBatis Perfect integration , Unwanted mybatis Configuration map file -->
<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 To configure sqlSessionFactoryBean, Corresponding to the above sping Of SqlSessionFactoryBean class
.
@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 Of setMapperLocations There is a problem ");
e.printStackTrace();
} // dataSource sqlSessionFactoryBean.setDataSource(dataSource); // SqlSessionFactory sessionFactory = sqlSessionFactoryBean.getObject(); return sqlSessionFactoryBean; }
spring Configuration in
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="springMVCmybatis" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
springboot Configuration in
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
// This annotation needs to be loaded MybatisDbConfigure.class And then load MapperScanConfig This class
@AutoConfigureAfter(MybatisDbConfigure.class)
public class MapperScanConfig { public MapperScannerConfigurer myGetMapperScannerConfigurer() {
MapperScannerConfigurer myMapperScannerConfigurer = new MapperScannerConfigurer(); myMapperScannerConfigurer.setBasePackage("com.my.dao");
myMapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
return myMapperScannerConfigurer;
} }
Conclusion :springboot It's through @Configuration To mark the custom configuration , Use... In configuration @Bean To add class functions and spring The configuration of the .xml Files work the same way , Both are the function of the container .
About this part of the configuration has been uploaded to my github On , Interested in or not understanding , You can log in to view .
springboot More articles about custom profiles
- springboot + Custom profile read
Create a new profile src\main\resources\resources\config.properties # Custom configuration files #System Encoding #File Upload Temp ...
- SpringBoot( Two ): SpringBoot Properties profile SpringBoot Multi environment profile SpringBoot Custom configuration files
1. Properties profile There are two kinds , One is the key value pair properties Properties profile , One is yaml Format of the configuration file properties To configure : 2. Multi environment profile When we have multiple sets of configuration files in our project For example, the development of matching ...
- Springboot Read configuration files and custom configuration files
1. establish maven engineering , stay pom Add dependencies to the file <parent> <groupId>org.springframework.boot</groupId> <a ...
- springboot Read custom profile node
Today I share with you the reading of custom configuration information : There are plans for blogging in the near future , Write separately springboot Aspects and springcloud Articles on , because springboot There are a lot of expected chapters , such cloud My article needs ...
- SpringBoot To load a custom configuration file
SpringBoot The default load profile name is :application.properties and application.yml, If you need to use a custom profile , Through @PropertySource Annotation assignment . J ...
- Springboot And Custom profile and read profile
This article is from [ Knowledge forest ] Read core configuration file The core profile refers to the resources In the root directory application.properties or application.yml The configuration file , There are two ways to read these two configuration files ...
- Springboot And Custom profile and read profile note : Do not underline strings in the configuration file . Configuration in progress key You can't underline ,value Sure ( The pit of underline , For two days .. In memory of )
Be careful : Do not underline strings in the configuration file . Configuration in progress key You can't underline ,value Sure FALSE . Examples that cannot be read : mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
- Springboot Several ways to read custom configuration files
One . Read core configuration file The core profile refers to the resources In the root directory application.properties or application.yml The configuration file , There are two ways to read these two configuration files , It's all pretty simple . ...
- SpringBoot Read custom profile
Custom configuration files my-config.properties bfxy.title=bfxy bfxy.name=hello spring boot! bfxy.attr=12345 Profile class appc ...
Random recommendation
- JsonProperties Application of model return
When using springMvc+Mybatis In the framework of . The database has been built , The database and the entity to be returned share one model. Everything is in order , It looks beautiful . The return code is as follows @RequestMapping(&qu ...
- jQuery Element traversal and element filtering
jQuery Traversal, traversal down jQuery Ergodic up ergodic jQuery Traversal of the same level jQuery Filtering of traversal
- php use mysql Function error
It's probably just because you forgot to pretend mysql Expand apt-get install php5-mysql
- c++ map Remove elements
typedef std::map<std::string,float> StringFloatMap; StringFloatMap col1; StringFloatMap::itera ...
- Python Standard library 04 file management ( part os package ,shutil package )
author :Vamei Source :http://www.cnblogs.com/vamei Welcome to reprint , Please also retain this statement . thank you ! Under the operating system , Users can manage files by operating system commands , Reference resources linux Document management related orders ...
- php export csv
public function doworks(){ // Output Excel The file header , Can handle user.csv Change it to the file name you want header('Content-Type: appli ...
- git Use (4)-git The principle of the work area of the buffer and the command of modifying and deleting
Article reprinted from :http://blog.csdn.net/think2me/article/details/39056379 The blogger said that without my permission , Shall not be reproduced , Then don't post it , Make a note of the key parts 1. The staging area is G ...
- Excel Paste the content into DataGridView, DataGridView Paste the Excel
void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Ke ...
- POJ 1904 King's Quest Strong connected components + Bipartite graph augmentation decision
http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html The God said it very well #include <iostream> #inc ...
- BZOJ 3672: [Noi2014] Ticket purchase ( The tree chain splits + Line segment tree + convex hull )
s Make prefixes and ( To the root ), dp(i) = min(dp(j) + (s(i)-s(j))*p(i)+q(i)). We all do the chain situation ... Is to use the stack to maintain a lower convex hull , It's a stack of bullets when it's inserted , Query on convex hull / ...