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. iOS项目开发实战——plist数组解析

    plist数据是苹果公司创造的数据格式,基于XML,因为在iOS,Mac系统中操作plist很方便,所以我们经常会用到.在iOS项目中.系统会自己主动生成一个Info.plist文件,里面存放了iOS ...

  2. Nginx系列三 内存池的设计

    Nginx的高性能的是用非常多细节来保证,epoll下的多路io异步通知.阶段细分化的异步事件驱动,那么在内存管理这一块也是用了非常大心血.上一篇我们讲到了slab分配器,我们能够能够看到那是对共享内 ...

  3. jQuery操作Form表单元素

    Web开发中常常须要操作表单,form表单元素有select.checkbox.radio.textarea.button.file.text.hidden.password等. 当中checkbox ...

  4. 小贝_mysql数据库备份与恢复

    mysql数据库备份与恢复 简要:        一.数据库备份        二.数据库恢复 一.数据库备份 1.备份简单说明 : 系统执行中,增量备份与总体备份 例: 每周日总体备份一次,周一到周 ...

  5. seajs载入流程图

    近期读seajs源代码,整理出了主要逻辑的流程图(注意:是逻辑示意图).感兴趣的朋友能够看看,欢迎批评指正~ http://www.gliffy.com/go/publish/image/607216 ...

  6. 【iOS进阶】UIWebview加载搜狐视频,自动跳回客户端 问题解决

    UIWebview加载搜狐视频,自动跳回搜狐客户端 问题解决 当我们用UIWebview(iOS端)加载网页视频的时候,会发现,当真机上有搜狐客户端的时候,会自动跳转到搜狐客户端进行播放,这样的体验对 ...

  7. 百度Fex webuploader.js上传大文件失败

    项目上用百度webuploader.js上传文件,option选项里面已经设置单个文件大小,但是上传低于此阈值的文件时仍然不成功. 我现在的理解是,框架是将文件post到后台服务器端的.. 百度发现是 ...

  8. (5)在tomcat运行自己的javaweb项目

    A:在MyEclipse下方的Servers栏中启动服务器,运行项目: 1,选中项目所在的tomcat服务器 2,点击“启动按钮”,见下图 3,启动以后,看控制台输出日志: B:从服务器按钮启动: 1 ...

  9. sabaki and leelazero

    https://tieba.baidu.com/p/5462772621?see_lz=1 http://zero.sjeng.org/ https://www.jianshu.com/p/a4ba1 ...

  10. AutoIT: 处理GridView控件的一些折中方法

    一般情况下,Gridview是无法通过AutoIT Window Info来获取控件信息的,但是可以有折中的办法对Gridview中的字段赋值. #include<array.au3> $ ...