设为首页 加入收藏

TOP

SpringBoot 2.x (4):配置文件与单元测试(一)
2019-09-02 23:19:08 】 浏览:19
Tags:SpringBoot 2.x 配置 文件 单元 测试

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&quo
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇客户端负载均衡 - Ribbon 下一篇Spring方法级别数据校验:@Valida..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目