绝大多数据源结合云顶娱乐送6元救济官网金:,

作者:云顶集团线路检测
package com.shuqi;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.List;import java.util.Map;@Component@ConfigurationProperties(prefix="db")@Datapublic class DbConf { private String username; private String password; private List<String> ipList; private Map<String,String> map; private Child child; @Data public static class Child{ private String address; private String postcode; }}

在那之中有叁个ActiveProfiles的注释,当在那之中写“test”就能找到application-test.yml的文本作为配置文件,那么属性也自然取的是个中的习性。借使是dev也是一致的道理。运维一下看看结果hello test。然后将ActiveProfiles切换到dev,见到结果hello dev。通过这种测验切换情形测量检验是还是不是一级福利。即测了功能也测了蒙受。

翻开源码打断点调节和测量检验发掘如下新闻

 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>

云顶娱乐送6元救济官网金,对接上篇文章SpringBoot基础教程——与Mybatis的组合,大家研商了与Mybatis的构成,然则在实质上职业中,大家操作的也许不是一个数据库,也正是说咱们有希望决定多个数据源,那几个又该怎么计划呢?其实也是各有长短,总来说之正是一句话,把各种数据库的配备分隔绝,不要让他俩相互影响就行了。请看上面的演示

配置文件中有健康属性的注入,List的注入,Map的流入,对象属性的注入,想想常规的流入也正是这个了。大家是怎么接收这个属性的吧?

spring-boot-starter-test里面含有的spring boot 测验框架中要求的内容,还包蕴我们的老朋友junit等。看下大家的事务代码

云顶娱乐送6元救济官网金 1image

本节项目源码

下节将的故事情节是:SpringBoot基础教程——与Swagger2的构成

通过@ConfigurationProperties(prefix="db")讲解开启了质量注入文件。那一个类会在铺排文件中寻找前缀是db的习性,除去db的前缀之后的品质就能够根据类中定义的名目注入进来。注意一定是个Bean,所以加上了@Component疏解。小同伴们能够试一下。

绝大多数据源结合云顶娱乐送6元救济官网金:,SpringBoot基础教程。下节将的剧情是:SpringBoot基础教程——与rabbitmq的构成

云顶娱乐送6元救济官网金 2image

下节将的剧情是:SpringBoot基础教程——与Mybatis的三结合

/** * pajk.com Inc. * Copyright  2014-2015 All Rights Reserved. */package com.shuqi;import com.alibaba.druid.filter.Filter;import com.alibaba.druid.filter.stat.StatFilter;import com.alibaba.druid.pool.DruidDataSource;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.Resource;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import org.springframework.transaction.support.TransactionTemplate;import javax.sql.DataSource;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Properties;@Configurationpublic class DalConfig { @Configuration @MapperScan(basePackages = "com.shuqi.dal.mapper.user",sqlSessionFactoryRef = "userSqlSessionFactory") @EnableTransactionManagement static class UserDalConfig{ @Value(value = "classpath:mybatis/sqlmap/user/*.xml") private Resource[] mapperLocations; @Value(value = "classpath:mybatis/mybatis-config.xml") private Resource configLocation; @Bean public SqlSessionFactoryBean userSqlSessionFactory(DruidDataSource userDataSource) { SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setMapperLocations(mapperLocations); ssfb.setConfigLocation(configLocation); ssfb.setDataSource(userDataSource); return ssfb; } @Bean(initMethod = "init", destroyMethod = "close") public DruidDataSource userDataSource(@Value("${ds.user.jdbc.url}") String url, @Value("${ds.user.jdbc.username}") String username, @Value("${ds.user.jdbc.password}") String password) throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl; druidDataSource.setUsername; druidDataSource.setPassword; druidDataSource.setMaxActive; druidDataSource.setInitialSize; druidDataSource.setMaxWait;//60s druidDataSource.setMinIdle; druidDataSource.setTimeBetweenEvictionRunsMillis; druidDataSource.setMinEvictableIdleTimeMillis; druidDataSource.setValidationQuery("select 1"); druidDataSource.setTestWhileIdle; druidDataSource.setTestOnBorrow; druidDataSource.setTestOnReturn; druidDataSource.setPoolPreparedStatements; druidDataSource.setMaxPoolPreparedStatementPerConnectionSize; druidDataSource.setFilters; Properties properties = new Properties(); properties.put("config.decrypt", "false"); druidDataSource.setConnectProperties(properties); StatFilter statFilter = new StatFilter(); statFilter.setSlowSqlMillis;//10s。。慢 statFilter.setMergeSql; statFilter.setLogSlowSql; List<Filter> filterList = new ArrayList<Filter>(); filterList.add(statFilter); druidDataSource.setProxyFilters(filterList); return druidDataSource; } @Bean public PlatformTransactionManager userTransactionManager(DataSource userDataSource) throws SQLException { return new DataSourceTransactionManager(userDataSource); } @Bean public TransactionTemplate userTransactionTemplate(PlatformTransactionManager userTransactionManager) { return new TransactionTemplate(userTransactionManager); } } @Configuration @MapperScan(basePackages = "com.shuqi.dal.mapper.log",sqlSessionFactoryRef = "logSqlSessionFactory") @EnableTransactionManagement static class LogDalConfig{ @Value(value = "classpath:mybatis/sqlmap/log/*.xml") private Resource[] mapperLocations; @Value(value = "classpath:mybatis/mybatis-config.xml") private Resource configLocation; @Bean public SqlSessionFactoryBean logSqlSessionFactory(DruidDataSource logDataSource) { SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setMapperLocations(mapperLocations); ssfb.setConfigLocation(configLocation); ssfb.setDataSource(logDataSource); return ssfb; } @Bean(initMethod = "init", destroyMethod = "close") public DruidDataSource logDataSource(@Value("${ds.log.jdbc.url}") String url, @Value("${ds.log.jdbc.username}") String username, @Value("${ds.log.jdbc.password}") String password) throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl; druidDataSource.setUsername; druidDataSource.setPassword; druidDataSource.setMaxActive; druidDataSource.setInitialSize; druidDataSource.setMaxWait;//60s druidDataSource.setMinIdle; druidDataSource.setTimeBetweenEvictionRunsMillis; druidDataSource.setMinEvictableIdleTimeMillis; druidDataSource.setValidationQuery("select 1"); druidDataSource.setTestWhileIdle; druidDataSource.setTestOnBorrow; druidDataSource.setTestOnReturn; druidDataSource.setPoolPreparedStatements; druidDataSource.setMaxPoolPreparedStatementPerConnectionSize; druidDataSource.setFilters; Properties properties = new Properties(); properties.put("config.decrypt", "false"); druidDataSource.setConnectProperties(properties); StatFilter statFilter = new StatFilter(); statFilter.setSlowSqlMillis;//10s。。慢 statFilter.setMergeSql; statFilter.setLogSlowSql; List<Filter> filterList = new ArrayList<Filter>(); filterList.add(statFilter); druidDataSource.setProxyFilters(filterList); return druidDataSource; } @Bean public PlatformTransactionManager logTransactionManager(DataSource logDataSource) throws SQLException { return new DataSourceTransactionManager(logDataSource); } @Bean public TransactionTemplate logTransactionTemplate(PlatformTransactionManager logTransactionManager) { return new TransactionTemplate(logTransactionManager); } }}

spring 能够透过xml的配备完毕属性的注入。spring boot 的合法提倡使用yml文件配置属性,一种是可以透过@Value注解注入,别的一中便是经过@ConfigurationProperties,完毕将属性映射到类中,然后选取类别选拔。明天我们来看一下。先看下配置文件呢

其间的操作是透过运营时情状中获取env这一个天性的值,然后通过controller中的hello暴流露去。功效相比不难。看下我们测量试验包中的resources目录下的从头到尾的经过,满含多个内容application-dev.yml与application-test.yml。他们中间的不相同正是中间的env的属性值区别。在application-dev.yml中的文件内容是env: dev。在application-test.yml中的文件内容是env: test。东西都绸缪好了,看下大家的测量检验方法

导致了从GetMapping注明上就无可奈何获取到U本田CR-VL消息,进而就应时而生了注册handler的时候,UOdysseyL音信不全,最终的handlerMap新闻如下图

spring boot 今后如此流行,差不离能够其余微服务的“底座”,必然是有它的道理的。个中最根本的二个原因正是对接方便。基本上jar包一引进,加一个讲明,就足以了,你就全体了spring boot的兵不血刃的效率了。别的叁个缘由正是扩展spring,详细的情况请看自身的别的一篇小说小编眼中的spring boot,不做累述。上边大家看看是spring boot 的运转时多造福。已经知晓的,请移步,不适合你们了。这一次教程使用的spring boot 的版本是1.3.5.RELEASE。

本节项目源码

本文由云顶集团线路检测发布,转载请注明来源

关键词: