我需要读取 JAR 外部的 application.properties 值和其他属性文件。在IDE中开发时,我将application.properties和其他属性文件放置在与src相同的根目录中。在IDE中运行时,一切正常。但是,当我制作一个 JAR 并将其与必要的属性文件一起放置在单独的文件夹中时,它无法读取相同的内容。我相信,我已经非常接近它了。需要帮助来解决我可能错的地方。分享必要的代码片段。
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class) // Load external properties
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
// Fetch the port from external properties
String port = System.getProperty("server.port", "8081");
// openLandingPageInBrowser("http://localhost:" + port);
}
}
我尝试读取 application.properties 的文件
@Configuration
@ConfigurationProperties(prefix = "application")
public class AppProperties {
private String port;
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
我读取其他 .properties 文件的文件
@Service
public class DropdownPropertiesService {
@Value("${dropdown1.file}")
private String dropdown1File;
@Value("${dropdown2.file}")
private String dropdown2File;
}
application.properties 文件
spring.application.name=demo2
server.port=8084
dropdown1.file=./dropdown1.properties
dropdown2.file=./dropdown2.properties
demo-spring-boot-props
├── pom.xml
├── config1
│ └── application.properties
├── config2
│ └── application.properties
├── dropdown1.properties
├── dropdown2.properties
└── src
└── main
├── java
│ └── com
│ └── example
│ ├── AppConfig.java
│ ├── DemoApplication.java
│ └── HelloController.java
└── resources
└── application.properties
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-spring-boot-props</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-spring-boot-props</name>
<description>demo-spring-boot-props project for Spring Boot</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.application.name=demo
# greeting=Hello
greeting=${GREETING:Hello}
默认,模板。
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
测试班
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
AppConfig appConfig;
//Inject greeting property from application.properties
// If GREETING is set in the environment variable, this value will be used first; otherwise, the greeting value in application.properties will be used.
@Value("${greeting:NotDefine}")
private String greeting;
@Value("${dropdown1.file:NotDefine}")
private String dropdown1File;
@Value("${dropdown2.file:NotDefine}")
private String dropdown2File;
// Create a GET request to receive parameters
@GetMapping("/hello")
public String sayHello(@RequestParam(value = "msg", defaultValue = "World") String msg) {
return String.format("%s, %s!", greeting, msg);
}
@GetMapping("/getFile1")
public String getFile1() {
return dropdown1File;
}
@GetMapping("/getFile2")
public String getFile2() {
return dropdown2File;
}
@GetMapping("/dump")
public String deump() {
return appConfig.dump();
}
}
package com.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
//@PropertySource("classpath:config1.properties")
//@PropertySource("classpath:config2.properties")
//@PropertySource("classpath:${dropdown1.file:NotDefine}")
//@PropertySource("classpath:${dropdown2.file:NotDefine}")
// ERR java.io.FileNotFoundException: class path resource [dropdown1.properties] cannot be opened because it does not exist
@PropertySource("file:${dropdown1.file:NotDefine}")
@PropertySource("file:${dropdown2.file:NotDefine}")
public class AppConfig {
@Value("${AAA1:NotDefine}")
private String aaa1;
@Value("${BBB1:NotDefine}")
private String bbb1;
@Value("${CCC1:NotDefine}")
private String ccc1;
@Value("${AAA2:NotDefine}")
private String aaa2;
@Value("${BBB2:NotDefine}")
private String bbb2;
@Value("${CCC2:NotDefine}")
private String ccc2;
@Value("${AAA3:NotDefine}")
private String aaa3;
@Value("${BBB3:NotDefine}")
private String bbb3;
@Value("${CCC3:NotDefine}")
private String ccc3;
public String dump() {
String sb = "aaa1 = " + aaa1 + "\n" +
"bbb1 = " + bbb1 + "\n" +
"ccc1 = " + ccc1 + "\n" +
"aaa2 = " + aaa2 + "\n" +
"bbb2 = " + bbb2 + "\n" +
"ccc2 = " + ccc2 + "\n" +
"aaa3 = " + aaa3 + "\n" +
"bbb3 = " + bbb3 + "\n" +
"ccc3 = " + ccc3 + "\n";
return sb;
}
}
dropdown1.file=./dropdown1.properties
dropdown2.file=./dropdown2.properties
AAA1=XXXa1
BBB1=XXXb1
CCC1=XXXc1
AAA2=YYYa2
BBB2=YYYb2
CCC2=YYYc2
mvn clean package
Linux
export SPRING_CONFIG_LOCATION=classpath:/application.properties,optional:file:`pwd`/config1/application.properties,optional:file:`pwd`/config2/application.properties
java -jar target/app.jar
您可以通过设置环境变量来配置application.properties:
SPRING_CONFIG_LOCATION
我配置了3个application.properties。
app.jar/application.properties
阅读者 clsspath
classpath:/application.properties
file:`pwd`/config1/application.properties
file:`pwd`/config2/application.properties
demo-spring-boot-props
├── target
│ └── app.jar/application.properties (classpath)
├── config1
│ └── application.properties
└── config2
└── application.properties
curl http://localhost:8080/hello?msg=Spring
>>> Hello, Spring!
curl http://localhost:8080/hello
>>> Hello, World!
greeting
GREETING
停止 Spring Boot 应用程序(Ctrl + C)
Linux
export GREETING=XYZ
java -jar target/app.jar
curl http://localhost:8080/hello?msg=Spring
>>> XYZ, Spring!
curl http://localhost:8080/hello
>>> XYZ, World!
greeting属性可以从application.perties中读取或者通过环境变量GREETING设置。
# application.properties
greeting=${GREETING:Hello}
curl http://localhost:8080/getFile1
>>> ./dropdown1.properties
curl http://localhost:8080/getFile2
>>> ./dropdown2.properties
HelloController.java读取dropdown1.file和dropdown2.file。
@Value("${dropdown1.file:NotDefine}")
private String dropdown1File;
@Value("${dropdown2.file:NotDefine}")
private String dropdown2File;
但是 dropdown1.file 和 dropdown2.file 属性的设置从何而来?
还记得这个吗?
export SPRING_CONFIG_LOCATION=classpath:/application.properties,optional:file:`pwd`/config1/application.properties,optional:file:`pwd`/config2/application.properties
config1/application.properties 和 config2/application.properties 提供
dropdown1.file
和 dropdown2.file
config
# config1/application.properties
dropdown1.file=./dropdown1.properties
config2/application.properties
dropdown2.file=./dropdown2.properties
demo-spring-boot-props
├── dropdown1.properties
└── dropdown2.properties
curl http://localhost:8080/dump
aaa1 = XXXa1
bbb1 = XXXb1
ccc1 = XXXc1
aaa2 = YYYa2
bbb2 = YYYb2
ccc2 = YYYc2
aaa3 = NotDefine
bbb3 = NotDefine
ccc3 = NotDefine
停止 Spring Boot 应用程序(Ctrl + C)
通过spring应用程序参数更改服务器端口。
java -jar target/app.jar --server.port=8888
curl http://localhost:8888/hello?msg=Spring
>>> XYZ, Spring!
(1) Spring Boot应用程序的属性可以从多个来源读取。
(2) Spring Boot 的 application.properties 也来自多个来源。