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. CSS 相对|绝对(relative/absolute)定位系列(一)

    一.有话要说 以前写内容基本上都是:眼睛一亮——哟呵,这个不错,写!然后去古人所说的茅房里蹲会儿,就有写作的思路了.但是,构思相对/绝对(relative/absolute)定位系列却有好些时日,考虑 ...

  2. AWK教程

    1. IBM:GAWK入门:AWK语言基础 2. Unix AWK使用手册 3. 台湾中研院计算中心ASPAC计划之AWK程序介绍 4. Study-area之AWK 5. AWK学习笔记——酷勤 持 ...

  3. 2016/05/25 empty() 与 isset()的区别

    对于初学php的人来说,empty()和和isset()用法的区别是很难搞清楚的,他们的用法的差别不仔细去琢磨的话确实很难弄清楚. 先说一下他们的共同点: 都可以判定一个变量是否为空: 都返回bool ...

  4. java8--Mysql数据库与JDBC编程(java疯狂讲义3复习笔记)

    JDBC:java database connectivity 数据库基本命令: 启动 show databases; create database [if not exists] 数据库名; do ...

  5. IT江湖--这个冬天注定横尸遍野(多数人技术迟迟无进阶,多半是懒的原因。勤是必须的)

    今年江湖大事繁起,又至寒冬,冻的不仅是温度,更是人心. 这两天上班途中看到多个公众号和媒体发了很多 "XXX公司裁员50%" 等等诸如此类的文章,也真是撼动人心.寒冬,比以往来的更 ...

  6. Lightoj 1023 - Discovering Permutations

    1023 - Discovering Permutations    PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory L ...

  7. NDK编程中如何在C文件中打印调试信息

      1,在Android.mk文件中加上 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog LOCAL_PATH := $(call my-dir)include ...

  8. CentOS7.2 设置GRUB2引导界面分辨率

    最近在学习OS引导启动,GRUB2的学习材料也不少,主要还看官方手册清晰些. 公司里办公机的多启动用的ubuntu的界面,还挺炫酷的.之前看其他博客网文里看到可以设置grub2的分辨率,我拿CentO ...

  9. 谁动了我的cpu——oprofile使用札记

    引言 cpu无端占用高?应用程序响应慢?苦于没有分析的工具? oprofile利用cpu硬件层面提供的性能计数器(performance counter),通过计数采样,帮助我们从进程.函数.代码层面 ...

  10. jsDoc 使用及配置!

    原文地址:http://www.cnblogs.com/hxling/archive/2012/11/27/2791067.html jsDoc 说白了就是帮助你生成JS的文档,但有个前提,就是你编写 ...