springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布
一、单元测试
生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit、Hamcrest、Mockito,没有的手动加上。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test
</artifactId> </dependency>
添加测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests { private MockMvc mvc; @Before
public void setUp(){
mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
} @Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}
二、修改访问端口和路径
1、修改端口号
- 使用properties文件方式:
在src/main/resoutces目录下创建:application.properties,添加如下配置即可修改端口号:
server.port=8088
- 使用yml文件方式:
在src/main/resoutces目录下创建:application.yml,添加如下配置即可修改端口号:
server:
port:8088
2、修改项目访问路径
使用properties文件方式:
在application.properties,添加如下配置即可修改项目访问路径:
server.servlet.context-path=/springboot-demo
- 使用yml文件方式:
在application.yml,追加如下配置即可修改项目访问路径:
server:
port:8088
servlet:
context-path: /springboot-demo
3、修改默认容器
在之前默认使用的 WEB 容器是 Tomcat 容器,实际上在 SpringBoot 里面如果用户有需要也可以将容器更换为 jetty 容器,如果 现在要想使用这个容器,则只需要追加一些依赖即可:
1、移除默认容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2、添加jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
三、加载读取多个配置文件
1、application.properties 和 application.yml的优先级
那么这个时候将优先进行application.properties配置文件的加载,如果现在两个配置项的作用冲突了,则以 properties 为主,如果不冲突,则以存在的为主。
2、多配置文件支持(yml和property)
格式必须是application-xxx.properties或者是application-xxx.yml命
在application.properties或application.yml中引入,有两个用法:
1、直接引入
创建application-message.properties,内容如下:
code=-message
content=消息不存在
在application.properties或application.yml中引入,多个文件用逗号隔开
spring.profiles.include=message
注入到bean中
@Component
public class Message{
@Value("${code}")
private String code; @Value("${content}")
private String content; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
使用:
package com.example.demo.controller; import com.example.demo.Property.BaseMsg;
import com.example.demo.Property.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Properties; @RestController
public class HelloWorldController { @Autowired
private Message message; @RequestMapping("/message")
public String message() {
System.out.println(message.getCode());
return "message:"+message.getCode();
}
}
测试,访问:http://localhost:8082/message

2、特定环境激活
spring.profiles.active=devMsg
或通过启动参数激活,在启动命令中添加
-Dspring.profiles.active="devMsg"
@Profile表示只有当spring.profiles.active激活配置一致时才会注入,用法示例:
@Component
@Profile("testMsg")
public class TestMsg extends BaseMsg{
@Value("${msg.code}")
private String msgCode;
public String getMsgCode() {
return msgCode;
}
public void setMsgCode(String msgCode) {
this.msgCode = msgCode;
}
}
四、springboot多环境配置
多环境配置有两种方式,一种配置在同一个文件中,另一种时时配置在多个文件中。
1、配置在多个文件中
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
如:spring.profiles.active=test就会加载application-test.properties配置文件内容
或
执行java -jar xxx.jar --spring.profiles.active=test,也就是测试环境的配置(test)
2、配置同一个文件中
spring:
profiles:
active: pro
server:
servlet:
context-path: /springboot-demo
---
#开发环境配置
spring:
profiles: dev
server:
port:
---
#测试环境配置
spring:
profiles: test
server:
port:
---
#生产环境配置
spring:
profiles: pro
server:
port:
通过spring.profiles.active: pro或者java -jar xxx.jar --spring.profiles.active=pro动态切换环境,---分隔符,分开后相当于单独一个文件,---前是公共配置。System.getProperty()方法获取系统变量和启动参数。
五、打包发布
1、打包
- 运行maven package,如果项目有改动需要先运行maven clean
- 打包完,target下面会有项目jar包,demo-0.0.1-SNAPSHOT.jar
2、运行
拷贝demo-0.0.1-SNAPSHOT.jar到指定目录,运行
java -jar demo-0.0.-SNAPSHOT.jar
访问:http://192.168.1.100:8081/hello
springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布的更多相关文章
- Servlet3.0注解配置访问路径和urlParttern配置
一.Servlet用注解配置访问路径 二.IDEA的tomcat相关配置 其中,第一点的配置文件,直接在IDEA的可视化操作界面修改就可以改掉配置文件中内容: 三.urlParttern配置 其中,* ...
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...
- Spring boot 默认静态资源路径与手动配置访问路径
在application.propertis中配置 ##端口号server.port=8081 ##默认前缀spring.mvc.view.prefix=/## 响应页面默认后缀spring.mvc. ...
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 ...
- spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)
项目hello, 在src/main/java下面建一个目录: charpter2 一.xml配置访问路径 web.xml <web-app> <display-name>Ar ...
- SpringCloud系列三:SpringSecurity 安全访问(配置安全验证、服务消费端处理、无状态 Session 配置、定义公共安全配置程序类)
1.概念:SpringSecurity 安全访问 2.具体内容 所有的 Rest 服务最终都是暴露在公网上的,也就是说如果你的 Rest 服务属于一些你自己公司的私人业务,这样的结果会直接 导致你信息 ...
- SpringBoot系列三:SpringBoot自定义Starter
在前面两章 SpringBoot入门 .SpringBoot自动配置原理 的学习后,我们对如何创建一个 SpringBoot 项目.SpringBoot 的运行原理以及自动配置等都有了一定的了解.如果 ...
- SPRING-BOOT系列之SpringBoot快速入门
今天 , 正式来介绍SpringBoot快速入门 : 可以去如类似 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/refer ...
- SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)
实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图: 这种配置多环境的方法在SSM框架中 ...
随机推荐
- 安装Helm
一:1.下载helm-v2.10.0-linux-amd64.tar.gz 地址:https://github.com/kubernetes/helm/releases2,解压缩 tar -zxvf ...
- CRT&EXCRT 中国剩余定理及其扩展
前言: 中国剩余定理又名孙子定理.因孙子二字歧义,常以段子形式广泛流传. 中国剩余定理并不是很好理解,我也理解了很多次. CRT 中国剩余定理 中国剩余定理,就是一个解同余方程组的算法. 求满足n个条 ...
- 关于vue里页面的缓存
keep-alive是vue内置的一个组件,可以使被它包含的组件处于保留状态,或避免被重新渲染. 用法: 运行结果描述: input输入框内,路由切换输入框内部的内容不会发生改变. 在keep-ali ...
- 跟我一起学习vue2(学习工程目录)[三]
查看生成的my-project的工程目录 首先看 build是最终发布的代码存放位置. 我查看了一下目录,里面都是Js文件 config目录里面主要是配置目录,包括端口号.如果开的项目多,可以进入in ...
- 编译RocketMQ
目前最新版本为 官方没有提供可执行的安装包,只提供了maven项目,去GitHub下载 点击Download ZIP 就可以了,如果你安装了版本控制工具 SVN 或者 GIT 就可以使用它上边的 UR ...
- linux command ------ ls
-rw-r--r-- [d]: content [-]: file [l]: link file [b]: interface device for storage in a device file ...
- python备份网站,并删除指定日期文件
#!/usr/bin/python# Filename: backup_ver1.pyimport osimport timeimport datetime# 1. The files and dir ...
- jackson用法
ObjectMapper mapper=new ObjectMapper(); //3.调用mapper的writeValueAsString()方法把一个对象或集合转为json字符串 Custome ...
- CENTOS 升级Nodejs 到最新版本
1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过 uname -a ...
- 启动eclipse弹出提示Version 1.7.0_79 of the JVM is not suitable for this product. Version: 1.8 or greater is required怎样解决
启动eclipse时弹出如下弹出框: 解决办法: 在eclipse安装目录下找到eclipse.ini文件,并在 -vmargs-Dosgi.requiredJavaVersion=1.8 前面加上 ...