问题描述
我们打包springboot项目为jar的时候,application.yml文件一般会打包进jar里面,有时需要修改配置就需要vi这个jar,这样操作每次都需要查找配置文件,较为麻烦。
我们可以将application.yml或者application-prod.yml等配置文件拿出来放到jar外面,这样也方便修改配置。
创建springboot项目
创建springboot项目,项目结构如下
AAController.java类中代码如下
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AAController {
@RequestMapping("/aaa")
public String test(){
return "aaa";
}
}
App.java类中代码如下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SpringApplication.run(App.class);
}
}
application.yml代码如下
server:
port: 80
pom.xml代码如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LoadOuterConfigTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LoadOuterConfigTest</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 如果没有该项配置,则devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
打包运行的日志如下
Hello World!
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2022-09-21 14:39:13.273 INFO 24232 --- [ main] org.example.App : Starting App v1.0-SNAPSHOT on 2tanchuanhai with PID 24232 (D:\java\test\LoadOuterConfigTest-1.0-SNAPSHOT.jar started by Admin in D:\java\test)
2022-09-21 14:39:13.275 INFO 24232 --- [ main] org.example.App : No active profile set, falling back to default profiles: default
2022-09-21 14:39:14.597 INFO 24232 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2022-09-21 14:39:14.614 INFO 24232 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-21 14:39:14.614 INFO 24232 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2022-09-21 14:39:14.696 INFO 24232 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-09-21 14:39:14.696 INFO 24232 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1365 ms
2022-09-21 14:39:14.866 INFO 24232 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-09-21 14:39:15.021 INFO 24232 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path ''
2022-09-21 14:39:15.031 INFO 24232 --- [ main] org.example.App : Started App in 2.281 seconds (JVM running for 2.656)
可以看到此时端口号为80端口
解决办法
在jar的同级目录中创建目录config
将application.yml放入config目录中
修改application.yml中的配置
server:
port: 800
重启项目后日志打印如下
Hello World!
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2022-09-21 14:45:49.493 INFO 17328 --- [ main] org.example.App : Starting App v1.0-SNAPSHOT on 2tanchuanhai with PID 17328 (D:\java\test\LoadOuterConfigTest-1.0-SNAPSHOT.jar started by Admin in D:\java\test)
2022-09-21 14:45:49.495 INFO 17328 --- [ main] org.example.App : No active profile set, falling back to default profiles: default
2022-09-21 14:45:50.791 INFO 17328 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 800 (http)
2022-09-21 14:45:50.808 INFO 17328 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-21 14:45:50.808 INFO 17328 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2022-09-21 14:45:50.888 INFO 17328 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-09-21 14:45:50.888 INFO 17328 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1348 ms
2022-09-21 14:45:51.057 INFO 17328 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-09-21 14:45:51.219 INFO 17328 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 800 (http) with context path ''
2022-09-21 14:45:51.227 INFO 17328 --- [ main] org.example.App : Started App in 2.259 seconds (JVM running for 2.64)
此时端口号变成了800
OK,问题解决!