JAVA加载外部配置文件(springboot项目将配置文件拿出来)

问题描述

我们打包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,问题解决!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值