SpringBoot的配置文件有默认的application.properties

还可以使用YAML

区别:

application.properties示例:
server.port=8090
server.session-timeout=30
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8

application.yml示例:
server:
   port: 8090  
   session-timeout: 30
   tomcat.max-threads: 0
   tomcat.uri-encoding: UTF-8

两种方式都优于SSM的XML配置形式,个人更偏向于使用properties文件

SpringBoot默认的配置项:

这里不多写了,可以去Spring官网查找

也可以参考一位大佬的博客:

https://www.cnblogs.com/SimpleWu/p/10025314.html

如何在代码中读取配置文件中的自定义信息呢?

比如:

file.path=D:\\temp\\images\\

想要在代码中获取这个值的话:

    @Value("${file.path}")
private String FILE_PATH;

将配置文件映射到实体类:

第一种方式:手动给实体类属性注入

配置文件:

test.name=springboot
test.domain=www.dreamtech.org

实体类:

package org.dreamtech.springboot.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class ServerSettings {
@Value("${test.name}")
private String name;
@Value("${test.domain}")
private String domain; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDomain() {
return domain;
} public void setDomain(String domain) {
this.domain = domain;
} }

Controller:

    @Autowired
private ServerSettings serverSettings; @RequestMapping("/test")
@ResponseBody
private ServerSettings test() {
return serverSettings;
}

第二种方式:根据前缀自动注入

实体类:

package org.dreamtech.springboot.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "test")
public class ServerSettings {
private String name;
private String domain; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDomain() {
return domain;
} public void setDomain(String domain) {
this.domain = domain;
} }

注意:如果使用了前缀的方式,那么不能再使用@Value注解了!

下面进行SpringBoot测试学习:

普通的单元测试:

首先导入依赖:通常SpringBoot新项目带有这个依赖,如果没有,自行导入即可

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

在test目录下新建测试类:

@Test用于测试;@Before测试前运行;@After测试后运行

package org.dreamtech.springboot;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import junit.framework.TestCase; @RunWith(SpringRunner.class)
@SpringBootTest(classes = { SpringbootApplication.class })
public class SpringBootTestDemo {
@Test
public void testOne() {
System.out.println("hello world!");
TestCase.assertEquals(1, 1);
} @Before
public void testBefore() {
System.out.println("Before");
} @After
public void testAfter() {
System.out.println("After");
}
}

对API接口进行测试:使用MockMVC

MockMVC相当于就是一个HTTP客户端,模拟用户使用浏览器进行操作

写一个接口进行测试:

    @RequestMapping("/test")
@ResponseBody
private String test() {
return "test";
}

测试类:

package org.dreamtech.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import junit.framework.TestCase; @RunWith(SpringRunner.class)
@SpringBootTest(classes = { SpringbootApplication.class })
@AutoConfigureMockMvc
public class MockMvcTest {
@Autowired
private MockMvc mockMvc; @Test
public void apiTest() throws Exception {
// 以GET方式访问/test路径,如果返回是200状态码说明调用成功
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/test"))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
TestCase.assertEquals(mvcResult.getResponse().getStatus(), 200);
}
}

最后记录一点有趣的东西:

启动的时候显示SpringBoot多单调啊,不如找一些好看的!

在classpath下面写一个banner.txt:

////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
////////////////////////////////////////////////////////////////////
Spring Boot Version: ${spring-boot.version}

SpringBoot 2.x (4):配置文件与单元测试的更多相关文章

  1. springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试

    包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...

  2. SpringBoot第二篇:配置文件详解一

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10837594.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   Sprin ...

  3. Springboot学习:核心配置文件

    核心配置文件介绍 SpringBoot使用一个全局配置文件,配置文件名是固定的 application.properties application.yml 配置文件的作用:修改SpringBoot自 ...

  4. 从SpringBoot源码分析 配置文件的加载原理和优先级

    本文从SpringBoot源码分析 配置文件的加载原理和配置文件的优先级     跟入源码之前,先提一个问题:   SpringBoot 既可以加载指定目录下的配置文件获取配置项,也可以通过启动参数( ...

  5. springboot整合H2内存数据库,实现单元测试与数据库无关性

    一.新建spring boot工程 新建工程的时候,需要加入JPA,H2依赖 二.工程结构   pom文件依赖如下: <?xml version="1.0" encoding ...

  6. springboot学习二:配置文件配置

    springboot默认读取application*.properties #######spring配置####### spring.profiles.active=dev //引入开发配置文件 a ...

  7. springboot下整合各种配置文件

    本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等  以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...

  8. SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)

    实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图: 这种配置多环境的方法在SSM框架中 ...

  9. JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理

    转载 https://www.cnblogs.com/xieyupeng/p/9664104.html @Value获取值和@ConfigurationProperties获取值比较   @Confi ...

随机推荐

  1. 搜索学术论文訪问google的能用的几个IP地址

    google搜索引擎打不开时的解决的方法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过測试可用的IP,用来在不能域名訪问google的时候进行訪问 更新一个最新的. ...

  2. mac 使用命令行,对远程服务器进行文件更新

    目的:更新服务器文件A 1.远程传输文件 A.zip 在本地A文件的父级文件夹下执行 scp ./A.zip 远程服务器用户名@远程服务器IP:/要放置的文件夹目录/ 然后要输入服务器登陆密码,进行文 ...

  3. POJ3761 Bubble Sort

    对1~n组成的序列进行冒泡排序,一共进行了k趟,问有几个符合题意的序列. 注意:这里指每一趟是指交换当前相邻的全部逆序对,比如:2 1 4 3进行一趟交换就是1 2 3 4 假设我们细心观察.就会发现 ...

  4. Unity构造函数注入代码示例

    Unity构造函数注入代码示例 如果使用 Unity 实例化一个类,该类的构造函数依赖一个或多个其他类,则 Unity 会为构造函数自动创建参数中指定的被依赖的类的实例.例如,下面的代码展示了一个名为 ...

  5. 最新Bootstrap手册

    http://www.jqhtml.com/bootstraps-syntaxhigh/index.html

  6. Django的缓存,序列化,ORM操作的性能

    1,缓存:把数据先保存在某个地方,下次再读取的时候不用再去原位置读取

  7. XML简单介绍及举例

    可扩展标记语言(eXtensibleMarkup Language,简称XML).是一种标记语言.标记指计算机所能理解的信息符号.通过此种标记,计算机之间能够处理包括各种信息的文章等. 怎样定义这些标 ...

  8. RFC 在OA中创建PR

    创建PR:BAPI_REQUISITION_CREATE BAPI_PR_CREATE                 "Create Enjoy Purchase Requisistion ...

  9. 有关使用HTTP协议传输二进制文件

    HTTP协议是基于字符(ASCII)的,当Content-Type项为text/xml,则内容是文本格式:当二进制格式时,Content-Type项为image/gif,就是了.例如,浏览器请求一张图 ...

  10. lmbench andlmbench 移植测试

    /*********************************************************************** * lmbench andlmbench 移植测试 * ...