特别说明:
1. [推荐] 可以通过 --参数 方法, 设定SpringBoot的参数, 比如:java -jar abc.jar --server.port=9000java -jar abc.jar --spring.profiles.active=prod2. 可以通过 -D参数或 JAVA_OPTS/JAVA_TOOL_OPTIONS 操作环境变量, 设定SpringBoot参数, 比如:java -Dserver.port=9000 -jar abc.jar --spring.profiles.active=prod ===========================SpringBoot 参数文件相关的几个参数===========================-------------------
spring.config.name 参数-------------------spring.config.name 参数指定参数文件名, 不能加路径和文件扩展名.spring.config.name 参数缺省取值是 application, $ java -jar myproject.jar --spring.config.name=myproject $ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties -------------------spring.config.location 参数-------------------spring.config.location 参数既可传入目录, 也可以传入具体文件名, 甚至可以传入多个目录或文件(以逗号分割). 如果参数是目录的话, 目录应以 / 结尾, spring 将在指定的目录中查找 spring.config.name 对应的文件. $ java -jar myproject.jar --spring.config.location=your/config/dir/$ java -jar myproject.jar --spring.config.location=classpath:job1.properties,classpath:job2.propertiesspring.config.location 参数如果缺省, SpringBoot将从下面四个路径搜索参数文件, 搜索的顺序是:
file:./config/ 索索jar程序所在目录的config 子目录file:./ 搜索与 jar 程序的同目录下的配置文件classpath:/config/ 搜索jar 包内部 classes/config 目录下的配置文件classpath:/ 搜索jar 包内部 classes 目录下的配置文件需要说明的是: 一旦指定了spring.config.location 参数, SpringBoot将不再搜索上面4个目录.-------------------
spring.config.additional-location 参数-------------------上面已经提及, 一旦指定了spring.config.location 参数, SpringBoot将仅仅搜索参数设定的目录, 但如果指定的是spring.config.additional-location 参数, SpringBoot先搜索参数设定目录, 然后再搜索上面4个目录.
特别说明:
1. 一旦指定了spring.config.location 参数, SpringBoot将不再搜索上面4个目录.2. 即使是在一个目录中找到了参数文件, SpringBoot仍会其他目录中搜索并加载参数文件. SpringBoot是按照参数项级做override, 而不是参数文件级的override. 3. 如果spring.config.location 参数指定的是文件, SpringBoot将不会加载profile-specific变体文件, 如果spring.config.location 参数指定的是目录, SpringBoot仍会搜索并加载profile-specific变体文件. 4. 开发的时候我们通常会将application.properties放到 /src/main/resources/ 下, 在打包成executable war/jar后, 该参数文件会被自动转移到classpath:/ 下, 所以该文件会搜索并加载的.5. application.properties 属性不能是中文, springBoot 是以 iso-8859编码来读取的, 如果属性值一定要有中文, 可以放到application.yml 文件中. 6. SpringBoot参数解析器非常智能, 不管参数名是按照驼峰写法, 还是按照下划线写法, 还是按照连接符写法, 都能自动和Bean中的属性关联起来. 即参数中 server.servlet.contextPath 和 server.servlet.context_path 和 server.servlet.context-path 写法其实都是等价的.===========================
参数文件内的变量 placeholder===========================application.properties的变量placeholderapp.name=MyAppapp.description=${app.name} is a Spring Boot application ===========================参数文件的 profile 参数===========================# 以下是 application.properties 文件内容. # 其中 spring.profiles.active 参数或是写死, 或是通过命令行传入. spring.profiles.active=devspring.profiles.default=defaultenvironments.dev.url=http://dev.example.comenvironments.dev.name=Developer Setupenvironments.prod.url=http://another.example.comenvironments.prod.name=My Cool App ===========================针对不同环境的 profile 参数文件===========================不同profile的参数文件命名规范是 application-{profile}.properties. 不管指定了什么样的环境profile, SpringBoot 总是先加载 application.properties, 然后再加载指定的profile参数文件, 如果没有找到指定的profile参数文件, 还可以加载一个default参数文件. # 以下是 application.properties 文件内容. # 其中 spring.profiles.active 参数或是写死, 或是通过命令行传入. spring.profiles.active=devspring.profiles.default=default# 以下是 application-prod.properties 文件内容
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/dbspring.datasource.username=rootspring.datasource.password=root# 以下是 application-dev.properties 文件内容
spring.datasource.driver-class-name=org.h2.Driverspring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1spring.datasource.username=saspring.datasource.password=sa