If you do not want to use the default application.properties but want to put the property file outside the jar package, you can use the following two methods:
Only full paths can be set. Because when Java -jar runs the jar package, the classpath cannot be specified (whether through parameters or environment variables, the set classpath will be overwritten).
Method 1: Pass the command line parameter to specify spring.config.location
java -jar -Dspring.config.location=D:/zTest/config/config1.properties springbootrestdemo-0.0.1-SNAPSHOT.jar
You can also use spring.config.location to specify the path, so that you will find application-{profile}.properties in this path.
You can also specify the path with spring.config.location , and then specify the configuration file name with spring.config.name .
You can separate multiple paths and names by commas
Method 2: Use @PropertySource annotation.
@SpringBootApplication@PropertySource(value={"file:D:/zTest/config/config1.properties"})public class SpringbootrestdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootrestdemoApplication.class, args); }}Let's take a look at the Spring Boot configuration file and log file outside the jar
1. Exclude files when setting up package jar
<resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>*.properties</exclude> <exclude>logback.xml</exclude> </excludes> </resource></resources>
2. Pass the parameters to specify the location when starting
java -jar xxx.jar --spring.config.location=D:/springconfig/ --logging.config=D:/springconfig/logback.xml
The default location of springboot to find configuration files is as follows
// Note the order is from least to most specific (last one wins)private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:
Summarize
The above is the Spring Boot introduced by the editor. Put the configuration files and log files outside the jar. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!