Spring Boot 应用的外部配置资源,这些配置资源能够与代码相互配合,避免硬编码 方式,提供应用数据或行为变化的灵活性。
类型
Properties 文件
YAML 文件
环境变量
Java 系统属性
命令行
加载顺序
热加载
测试
命令行
Servlet参数(ServletConfig,ServletContext)
JNDI
先系统属性,再环境变量
application-{profile}.properties(先外后内)
application.properties(先外后内)
@PeopertySource
SpringApplication.setDefaultProperties
配置引用
XML文件
<?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-4.3.xsd" profile="test">
<bean id="huangxiaoming" class="com.segmentfault.springbootlesson18.domain.Person" >
<property name="id" value="2" />
<property name="name" value="黄" />
<property name="age" value="18" />
</bean>
</beans>
@ImportResource(locations = {"context.xml"})
Annotation
@Value("${person.name:特殊}")
private String name;
Java Code
public class PersonController implements EnvironmentAware {
@Override
public void setEnvironment(Environment environment) {
this.age = environment.getProperty("person.age", Integer.class);
}
}
Profiles
Spring Profiles 提供一种区隔应用配置的方式,让应用配置仅在某种特定的“环境”可用。
XML文件(beans中配置 profile = "prod")
<?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-4.3.xsd" profile="prod">
<bean id="liudehua" class="com.segmentfault.springbootlesson18.domain.Person" >
<property name="id" value="3" />
<property name="name" value="刘德华" />
<property name="age" value="18" />
</bean>
</beans>
Annotation(@Profile("prod"))
@Configuration
public class PersonConfiguration {
@Bean
@Profile("prod")
public Person zhangxueyou() {
Person person = new Person();
return person;
}
@Bean
@Profile("test")
public Person zhangkai() {
Person person = new Person();
return person;
}
}
Java Code(application.setAdditionalProfiles("prod"))
mutablePropertySources.addFirst(propertySource)
@SpringBootApplication
@ImportResource(locations = {
"META-INF/spring/context.xml",
"META-INF/spring/context-test.xml",
"META-INF/spring/context-prod.xml"
})
public class SpringBootLesson18Application {
public static void main(String[] args) {
SpringApplication application =
new SpringApplication(SpringBootLesson18Application.class);
application.setAdditionalProfiles("prod");
application.run(args);
}
}
核心API
环境相关
public interface Environment extends PropertyResolver {
String[] getActiveProfiles();
String[] getDefaultProfiles();
boolean acceptsProfiles(String... profiles);
}
配置相关(实现EnvironmentAware接口)
@Override
public void setEnvironment(Environment environment) {
// 获取当前Profiles
System.err.println("当前激活的Profiles : " + Arrays.asList(environment.getActiveProfiles()));
if (environment instanceof ConfigurableEnvironment) {
ConfigurableEnvironment env = ConfigurableEnvironment.class.cast(environment);
MutablePropertySources mutablePropertySources = env.getPropertySources();
Map<String, Object> source = new HashMap<>();
source.put("server.port", 1234);
PropertySource propertySource = new MapPropertySource("java-code", source);
mutablePropertySources.addFirst(propertySource);
}
}