application-dev.yml application-devDb.yml application-devRedis.yml
application-test.yml application-testDb.yml application-testRedis.yml
application-pre.yml application-preDb.yml application-preRedis.yml
application-prod.yml application-prodDb.yml application-prodRedis.yml
在yml文件中引入其他yml文件
spring: profiles: include: devDb,devRedis通过启动参数-Dspring.profiles.active=test来决定配置文件的使用
对于单个值的获取很方便,支持SpEL 表达式
@Value("${datasource.url}") private String dbUrl;这个主要针对有特殊意义的多个值放在一起,在使用的时候可以直接当做一个Bean来使用
@Component @ConfigurationProperties(prefix = "datasource") public class DbProperties implements Serializable { private String type; private String url; private String username; private String password; private String driverClassName; //省略get,set方法 }这个只能针对properties,
@Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"classpath:person.properties"}, encoding = "UTF-8") public class OtherProperties implements Serializable { private String name; private Integer age; //省略get,set方法 }首先需要配置相关的Bean,然后再入口类上面引用
<?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-3.0.xsd"> <bean id="testComponent" class="org.ghost.springboot.demo.common.component.TestComponent" init-method="init" destroy-method="destory"> <property name="name" value="测试Hello"/> </bean> </beans> @ImportResource(value = {"classpath:other.xml"}) @SpringBootApplication(scanBasePackages = "org.ghost.springboot.demo") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }源码位置: https://gitee.com/ceclar123/spring-boot-demo/tree/master/ch07 https://gitee.com/ceclar123/spring-boot-demo/tree/master/ch08