基于Spring的开发框架,旨在简化配置快速开发,是新一代web开发框架。下面介绍一下常用的几个功能:

1、Spring单元测试

针对DAO层

(1) @RunWith(Spring.class),表示要在Spring环境中做测试, 于是就可以使用@Autowired等注解了,

(2) @SpringBootTest(classes=TestBeanConfiguration.class),表示可以使用SpringBoot环境了,这样可以用@Autowired注解,把@Mapper注释过的Mapper对象引入.为了引入外部依赖的Bean对象,需要指定classes=TestBeanConfiguration.class.

注意: 之所以要指定classes=TestBeanConfiguration.class,这时因为Product这个类,并没有使用@Component等注解注入IOC容器.

(3) @Transactional注解来开启事务

(4) @Rollback(true)表示开启回滚数据功能,默认为True.

@RunWith(SpringRunner.class)
@SpringBootTest(classes=TestBeanConfiguration.class)
@Transactional
@Rollback(true)
public class ProductMapperTest { @Autowired
private ProductMapper mapper; @Autowired
private Product product; @Before
public void setUp(){
product.setPname("持续交付");
product.setType("书籍");
product.setPrice(69d);
} @Test
public void testAdd() {
Assert.assertEquals(Integer.valueOf(1), mapper.add(product));
} }

针对Service层

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class ProductControllerTest { @Autowired
private TestRestTemplate template; @Test
public void test() {
String body = template.getForObject("/ms/product/1", String.class);
Assert.assertEquals("success", body);
} }

针对Controller

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class ProductControllerTest { @Autowired
private TestRestTemplate template; @Test
public void test() {
String body = template.getForObject("/ms/product/1", String.class);
Assert.assertEquals("success", body);
} }

2、Springboot配置文件

@Configuration
@EnableAutoConfiguration //自动加载配置信息
@ComponentScan("com.kelly")//使包路径下带有注解的类可以使用@Autowired自动注入
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}

我们使用@Value("${属性名}")来注入application.yml/application.properties的配置值

当我们使用自定义配置文件的时候,只需要:

@Component
@ConfigurationProperties(prefix="defineTest")
@PropertySource("classpath:define.properties")
public class DefineEntity { private String pname; private String password; public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

在使用的时候注入DefineEntity Bean即可。

注意:如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。

3、多环境配置文件

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

application-dev.properties:开发环境,

application-test.properties:测试环境

application-prod.properties:正式环境

下面介绍三种配置文件的加载方式:

1)直接在启动类上使用@PropertySource注解来加载不同的配置文件

2)修改spring.profiles.active属性

3)执行命令行

   如上面介绍所讲的一样,我们可以使用命令行,比如执行java -jar xxx.jar --spring.profiles.active=test

通过上面,三种不同执行方式,我们也可以发现配置文件的优先级,优先级高的配置或覆盖优先级低的配置,同时公共配置属性放到application.properties,私有的配置属性放在application-{profile}.properties文件中。

微服务深入浅出(1)-- SpringBoot的更多相关文章

  1. 十分钟搭建微服务框架(SpringBoot +Dubbo+Docker+Jenkins源码)

    本文将以原理+实战的方式,首先对“微服务”相关的概念进行知识点扫盲,然后开始手把手教你搭建这一整套的微服务系统. 这套微服务框架能干啥? 这套系统搭建完之后,那可就厉害了: 微服务架构 你的整个应用程 ...

  2. 微服务框架中springboot启动的一个问题

    微服务中,采用的是springboot构建单个项目,其中一个项目user启动过程中总是启动补起来,相关的地方都没有错,始终启动不起来,而且要命的是控制台不打印日志,日志级别是debug级别的,但是打印 ...

  3. 基于LadybugFlow的微服务编排(1.SpringBoot集成)

    前言 前面的系列文章里,介绍了ladybugflow的业务可视化的设计以及常见场景的使用方法. 感谢大家对项目的关注. 本篇文章介绍一下基于ladybugflow的微服务编排场景及使用方法. 1. 业 ...

  4. 微服务深入浅出(7)-- 网关路由Zuul

    Zuul用于构建边界服务,致力于动态路由,过滤,监控,弹性伸缩和安全等方向. 1.Zuul+Ribbon+Eureka结合,可以实现智能路由和负载均衡 2.网关将所有服务的API接口统一聚合统一暴露 ...

  5. 微服务深入浅出(11)-- SpringBoot整合Docker

    添加Dockerfile 在目录src/main/resources目录下店家Dockerfile文件: From java MAINTAINER "Eric"<eric.l ...

  6. 微服务-开发框架之springboot by 大雄daysn

    目录 一.关于springboot 二.springboot的实践 2.1发布一个rest的api 2.2端点 2.3健康检查 2.4远程监控 一.关于springboot 由来:spring1.0- ...

  7. 微服务深入浅出(10)-- Docker

    概念 1.Docker引擎 一个运行在服务器上的后台进程 2.Docker客户端 分为两种:CLI和RestAPI,与Docker引擎交互 3.Docker镜像 类似于我们使用的光盘,将程序打包到Do ...

  8. 微服务深入浅出(9)-- Nginx

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,处理请求是异步非阻塞的,多个连接(万级别)可以对应一个进程.而Apache是同步多进程模型,一个连接对 ...

  9. 微服务深入浅出(8)-- 配置中心Spring Cloud Config

    Config Server从本地读取配置文件 将所有的配置文件统一写带Config Server过程的目录下,Config Server暴露Http API接口,Config Client调用Conf ...

随机推荐

  1. JMeter性能测试基础 (3) - 使用参数文件做搜索引擎性能对比

    本篇文章主要对如何在JMeter中进行URL的参数进行配置进行介绍,通过CSV文件配置参数数据,对baidu.sogou.haosou进行搜索性能对比测试. 1.建立测试计划.线程组,并在线程组下添加 ...

  2. jQuery 版本选择与常见插件库总结

    在日常的开发中jQuery作为一个流行多年的轻量级 JavaScript 库,使用十分的普遍,主要源于它的便捷性和实用性非常高. 在此总结一些关于jQuery版本的区别和选择的建议,以及一些常见插件库 ...

  3. JavaScript 稀奇的js语法

    function c(expression) { console.log(expression); } c(-0); // -0 c(-0 === +0); // true c((-0).toStri ...

  4. 先验算法(Apriori algorithm) - 机器学习算法

    Apriori is an algorithm for frequent item set mining and association rule learning over transactiona ...

  5. 【uoj#142】【UER #5】万圣节的南瓜灯 乱搞+并查集

    题目描述 给出一张 $n\times m$ 的网格图,两个格子之间有一条双向边,当且仅当它们相邻,即在网格图中有一条公共边. 特殊地,对于 $1\le x\le n​$ ,$(x,1)​$ 和 $(x ...

  6. BZOJ3083 遥远的国度(树链剖分+线段树)

    考虑暴力树剖.那么修改路径和查询子树最小值非常简单. 对于换根当然不能真的给他转一下,我们只记录当前根是哪个.对于查询,如果查询点不在当前根到原根的路径上,显然换根是对答案没有影响的:如果是当前根,答 ...

  7. vue element 新增、编辑类Dialog公用函数

    调用 <el-button type="primary" class="my-button" size="small" :loadin ...

  8. 【刷题】HDU 3435 A new Graph Game

    Problem Description An undirected graph is a graph in which the nodes are connected by undirected ar ...

  9. WinForm查询大数据界面假死,使用异步调用解决

    用DataGridView无分页绑定一个几千条数据的查询,查询的时候界面直接卡死十几秒,用户体验非常不好,因此用异步操作解决界面卡死的问题原本场景:点击[查询]后,界面直接卡死优化场景:点击[查询]后 ...

  10. 【纪中集训2019.3.13】fft

    题意: 描述 一共有\(n+m\)道题,其中\(n\)道答案是\(A\),\(m\)道答案是\(B\): 你事先知道\(n和m\),问在最优情况下的期望答错次数,对\(998244353\)取模: 范 ...