ConfigurationProperties默认是从全局配置文件中获取值
@PropertySource:
加载指定的配置文件,也就是自家创建一个xxx.propertites,在实体类上家注解
@PropertySource(value={“classpath:xxx.propertites”})
@ImportResource:
导入Spring的配置文件,让配置文件的内容生效.因为我的自己创建编写的配置文件springboot不能自动识别,
如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="HelloService" class="com.springexample.springboot01quick.Service.HelloService"/>
</beans>
要想被识别,必须在主程序类上加注解:
@ImportResource(locations = {"classpath:bean.xml"})
也可创建配置类:(springboot推荐)
profile
给springboot添加不同的环境支持,在开发中,又测试,开发等不同的环境。如在springboot项目中创建多个配置文件,如:
多profiles文件的方式
application-dev.xml开发使用
application-dov.xml测试使用
调试程序,系统默认使用aplication.properties,在配置文件中指定激活文件,spring.profiles.active=dev
YAML多文档方式
在YAML配置文件中,
server:
port: 8080
spring:
profiles:
active: prod
激活prod的profiles
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: prod
命令行参数
在配置文件中指定
spring.profiles.active=dev
—分隔不同的文档块
项目打包后在命令行启动
java -jar xxx.jar --spring.profiles.active=dev;
虚拟机参数
-Dspring.profiles.active=dev
#配置文件加载位置
file: ./config/
file: ./
classpath: /config/
classpath: /
从高到低。
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;
java -jar xxx.jar --spring.config.location=/home/cloudlandboy/application.yaml