目录
springboot打包与运行
springboot自带tomcat,打包方式为jar包,当一个springboot项目做完之后如何打包。
第一步:引入maven打包插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
第二步:打开maven管理,找到Lifecycle,点击package开始打包
第三步:在tartget目录下找到生成的jar包
右键jar包,Open in--->Explorer快速打开所属文件夹
第四步:在搜索栏中打开cmd窗口
第五步:输入命令java -jar demo4-0.0.1-SNAPSHOT.jar
运行成功
如果执行命令出现以下情况,将maven插件修改如下
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.springboot.Demo4Application</mainClass>
<!--<skip>true</skip>-->
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Window端口被占用
通常我们都设置端口为8080或者其他,当项目发行后出现端口被占用,或者idea没有启动,cmd窗口也没有启动,但是8080的端口却被占用时,我们可以采用相关的端口命令来管理端口。
#查询端口
netstat -ano
#查询指定端口
netstat -ano |findstr "端口号"
#根据进程PID查询进程任务
tasklist |findstr "进程PID"
#根据PID杀死任务(结束进程)
taskkill /F /PID "进程PID"
#根据进程名称杀死任务(结束进程)
taskkill -f -t -im "进程名称"
以上命令均在cmd窗口执行
临时属性设置
可以在cmd窗口中更改项目的配置,比如更改端口号(--server.port=80)
官方文档中有说明属性加载的优先顺序
main函数中的args参数
在idea中,找到Program arguments,给其传值,就是给main函数的args参数传值
不妨仔细想想,我们可以自己写一个args数组,给数组写入配置的值即可。
配置文件分类
springboot中4级配置文件
1级:file:config/application.yml【最高】
2级:file:application.yml
3级:classpath:config/application.yml
4级:classpath:application.yml【最低】
作用:
properties的优先级比yml高
自定义配置文件
通过启动参数加载配置文件(无需书写配置文件扩展名)
按照路径来加载配置文件(--spring.config.location=classpath:ABC/abc.yml)
多环境开发
spring:
profiles: #启动指定环境
active: pro
---
spring:
profiles: pro #设置生产环境
server:
port: 8081 #生产环境具体参数设定
---
spring:
profiles: dev #设置开发环境
server:
port: 8082 #开发环境具体参数设定
---
spring:
profiles: test #设置测试环境
server:
port: 8083 #测试环境具体参数设定
已过时
spring:
profiles:
推荐用下面语法
spring:
profiles:
active: pro
---
server:
port: 8081
spring:
config:
activate:
on-profile: pro
---
server:
port: 8082
spring:
config:
activate:
on-profile: dev
---
server:
port: 8083
spring:
config:
activate:
on-profile: test
以上方式会暴露配置,可以采用多配置文件格式(在类路径下创建多个yml文件)
主启动配置文件application.yml
环境分类配置文件application-pro.yml
环境分类配置文件application-dev.yml
环境分类配置文件application-test.yml
主配置文件中设置公共配置(全局)
环境分类配置文件中常用于设置冲突属性(局部)
properties配置多环境开发仅支持多文件方式
多环境开发配置书写---include
创建四个yml文件
application-dev.yml
application-devDB.yml
application-devMVC.yml
application-devTest.yml
使用include属性,在激活指定环境的情况下,同时加载多个环境
从spring2.4开始使用group代替include属性,降低了配置书写量
使用group书写定义多种主环境与子环境的包含关系
maven与springboot多环境兼容
在pom.xml中设置多环境属性
<profiles>
<profile>
<id>my_dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>my_pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
</profiles>
你想使用dev,就在dev下面配置以下标签(pro环境亦是如此)
<activation>
<activeByDefault>true</activeByDefault>
</activation>
在application.yml中配置
日志
手动获取日志对象
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class MyController {
//创建记录日志的对象
private static final Logger log = LoggerFactory.getLogger(MyController.class);
@GetMapping
public String log(){
log.debug("debug");
log.info("info...");
log.warn("warn...");
log.error("error...");
return "logger...";
}
}
想要查看debug级别,在application配置文件中配置debug: true即可
使用lombok获取日志对象
在pom.xml文件中引入lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
在类上加上@Slf4j注解
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/books")
public class MyController {
//创建记录日志的对象
//private static final Logger log = LoggerFactory.getLogger(MyController.class);
@GetMapping
public String log(){
log.debug("debug");
log.info("info...");
log.warn("warn...");
log.error("error...");
return "logger...";
}
}
设置日志组,控制指定包对应的日志输出级别,也可以直接控制指定包对应的日志输出级别
控制日志输出格式
- %d:日期
- %m:消息
- %n:换行
- ……
设置日志文件
其他详细日志配置