Spring Boot之配置

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);

        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值