springboot默认的配置文件
默认情况下,新建一个springboot项目,只有 /src/main/resources/application.properties 下一个配置文件,其实一个有4个地方可以放配置文件,分别是
/src/main/resources/application.properties
spring.application.name=/src/main/resources/application.properties - code
/src/main/resources/config/application.properties
spring.application.name=/src/main/resources/config/application.properties - code
/application.properties
spring.application.name=/application.properties - code
/config/application.properties
spring.application.name=/config/application.properties - code
配置文件的优先级
这四个文件优先级逐级递增+合并,意思是只要后面的文件里有和前面相同的配置,就覆盖掉,没有的就补充上。(yml/yaml文件同理,这里以默认的properties举例)
所以说,这四个文件都有spring.application.name
应该以/config/application.properties的为准,测试一下,没问题。
// 测试代码
@SpringBootApplication
public class YmlTestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(YmlTestApplication.class, args);
String property = run.getEnvironment().getProperty("spring.application.name");
System.out.println("property: " + property);
}
}
// 控制台日志
property: /config/application.properties - code
可以逐级把优先级高的文件删除,看看是不是配置变了,我这里就不详细测试了。继续
打包jar 部署测试
然后我们打包成jar包,启动看看
D:\Code\xxxx\ymlTest\target>C:\Users\Admin\.jdks\graalvm-jdk-17.0.11\bin\java.exe -jar ymlTest-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.4.0)
2024-12-09T11:39:41.515+08:00 INFO 32200 --- [/resources/config/application.properties - code] [ main] com.example.ymltest.YmlTestApplication : Starting YmlTestApplication v0.0.1-SNAPSHOT using Java 17.0.11 with PID 32200 (D:\Code\xxx\ymlTest\target\ymlTest-0.0.1-SNAPSHOT.jar started by Admin in D:\Code\xxx\ymlTest\target)
2024-12-09T11:39:41.518+08:00 INFO 32200 --- [/resources/config/application.properties - code] [ main] com.example.ymltest.YmlTestApplication : No active profile set, falling back to 1 default profile: "default"
2024-12-09T11:39:41.973+08:00 INFO 32200 --- [/resources/config/application.properties - code] [ main] com.example.ymltest.YmlTestApplication : Started YmlTestApplication in 0.779 seconds (process running for 1.252)
property: /resources/config/application.properties - code
看最后一行打印的其实是resources/config/下的配置,为什么不是跟目录下的呢?
解压后jar包后看看
D:.
├─BOOT-INF
│ │ classpath.idx
│ │ layers.idx
│ │
│ ├─classes
│ │ │ application.properties
│ │ │
│ │ ├─com
│ │ │ └─example
│ │ │ └─ymltest
│ │ │ YmlTestApplication.class
│ │ │
│ │ └─config
│ │ application.properties
│ │
│ └─lib
│ jakarta.annotation-api-2.1.1.jar
│ ......
classes下的application.properties
和config/application.properties
分别对应代码里的/src/main/resources/application.properties
和 /src/main/resources/config/application.properties
根目录下的两个配置文件其实没有打包下来,我们在jar包同目录下创建两个文件测试一下
先创建一个 application.properties
内容里后缀改成 outer
spring.application.name=/application.properties - outer
重新启动 查看日志,可以看到生效了,输出了 /application.properties - outer
:: Spring Boot :: (v3.4.0)
2024-12-09T13:18:45.155+08:00 INFO 34504 --- [/application.properties - outer] [ main] com.example.ymltest.YmlTestApplication : Starting YmlTestApplication v0.0.1-SNAPSHOT using Java 17.0.11 with PID 34504 (D:\Code\xxx\ymlTest\target\ymlTest-0.0.1-SNAPSHOT.jar started by Admin in D:\Code\xxx\ymlTest\target)
2024-12-09T13:18:45.159+08:00 INFO 34504 --- [/application.properties - outer] [ main] com.example.ymltest.YmlTestApplication : No active profile set, falling back to 1 default profile: "default"
2024-12-09T13:18:45.587+08:00 INFO 34504 --- [/application.properties - outer] [ main] com.example.ymltest.YmlTestApplication : Started YmlTestApplication in 0.766 seconds (process running for 1.214)
property: /application.properties - outer
再创建一个config目录,里面复制一个application.properties,内容如下
spring.application.name=/config/application.properties - outer
再次启动,查看日志也是正常生效的了
:: Spring Boot :: (v3.4.0)
2024-12-09T13:21:16.946+08:00 INFO 47920 --- [/config/application.properties - outer] [ main] com.example.ymltest.YmlTestApplication : Starting YmlTestApplication v0.0.1-SNAPSHOT using Java 17.0.11 with PID 47920 (D:\Code\xxx\ymlTest\target\ymlTest-0.0.1-SNAPSHOT.jar started by Admin in D:\Code\xxx\ymlTest\target)
2024-12-09T13:21:16.949+08:00 INFO 47920 --- [/config/application.properties - outer] [ main] com.example.ymltest.YmlTestApplication : No active profile set, falling back to 1 default profile: "default"
2024-12-09T13:21:17.377+08:00 INFO 47920 --- [/config/application.properties - outer] [ main] com.example.ymltest.YmlTestApplication : Started YmlTestApplication in 0.747 seconds (process running for 1.204)
property: /config/application.properties - outer
总结一下
代码里的/src/main/resources/application.properties
和 /src/main/resources/config/application.properties
会打包在jar里,启动时作为默认配置,如果要修改的话,可以在jar同目录创建一个application.properties
或者config/application.properties
替换。而不用重新打包。
(个人的)最佳实践
- 开发环境下,根目录下的application.properties 和 config/application.properties 其实没什么用处,因为打包不进去。
- /src/main/resources/下的配置选一个就好了,直接用application.properties就好了,当然用另一个也行
- 如果打包时候直接把他打包到jar包外面不就好了吗,改起来也方便,省得每次自己拷贝
增加下面的maven打包配置,作用是将 src/main/resources 下的yml和properties拷贝到输出目录,也就是target目录,这样再打包(install)时候,就会出现两个文件。
<build>
<plugins>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Maven Resources Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.yml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>