springboot自訂設定檔

來源:互聯網
上載者:User

標籤:使用   cto   com   基礎   ebe   property   and   mvc   comm   

  前言:如果你一點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的sqlSessionFactory。

@Bean(name = "sqlSessionFactoryBean")public SqlSessionFactoryBean myGetSqlSessionFactory(DataSource dataSource) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();// mapperLocationsResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();try {sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*Mapper.xml"));} catch (IOException e) {log.info("sqlSessionFactoryBean的setMapperLocations有問題");e.printStackTrace();}// dataSourcesqlSessionFactoryBean.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自訂設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.