Spring 中关于Properties配置和取值的那些事

这里写图片描述

引言

在平时项目开发中如果有些参数经常需要修改,或者后期可能需要修改,我们需要将这部分数据设计为可配置化;因此我们把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源代码,这样更加方便。
那么我们在Spring中如何实现那?Spring中关与properties的配置方式很多种,使用方式也很多种,希望我的文章对大家有所帮助!

一.原理分析

 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFactoryPostProcessor 接口的一个实现。允许将上下文(配置文件)中的属性值放在另一个单独的标准 JavaProperties 文件中去。在 XML 文件中用类似 EL 表达式的 ${key} 替换指定的 properties 文件中的值。这样的话,以后更改参数时,我们直接替换 properties 文件中的值就可以了。

二.配置文件

#sys.properties文件配置:
user=root  
password=root  

三.Properties 配置

1. 通过 xml 配置

1.下面这个是最常用的配置方式了,很多项目都是这么写的:

<context:property-placeholder location="classpath:sys.properties" />

2. 通过 @PropertySource 配置

前面的通过 xml 配置非常常用,但是如果你也有一种要消灭所有 xml 配置文件的冲动的话,你应该使用以下方式:


@PropertySource("classpath:sys.properties")
@Configuration
public class JavaDoopConfig {

}

注意一点,@PropertySource 在这里必须搭配 @Configuration 来使用,具体不展开说了。

3. PropertyPlaceholderConfigurer

如果读者见过这个,也不必觉得奇怪,在 Spring 3.1 之前,经常就是这么使用的:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:sys.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
      <!-- 这里可以配置一些属性 -->
</bean>

java configuration 的版本:

@Bean
public PropertyPlaceholderConfigurer propertiess() {
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")};
    ppc.setLocations(resources);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    return ppc;
}

4. PropertySourcesPlaceholderConfigurer

到了 Spring 3.1 的时候,引入了 PropertySourcesPlaceholderConfigurer,这是一个新的类,注意看和之前的 PropertyPlaceholderConfigurer 在名字上多了一个 Sources,所属的包也不一样,它在 Spring-Context 包中。
在配置上倒是没有什么区别:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:sys.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <!-- 这里可以配置一些属性 -->
</bean>

java configuration 的版本:

@Bean
public PropertySourcesPlaceholderConfigurer properties() {
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")};
    pspc.setLocations(resources);
    pspc.setIgnoreUnresolvablePlaceholders(true);
    return pspc;
}

四.Properties 的使用

1. 在 xml 配置文件中使用

即自动替换 ${} 里面的值。

<bean id="xxx" class="com.javadoop.Xxx">
      <property name="url" value="${javadoop.jdbc.url}" />
</bean>

2.通过 @Value 注入使用

@Value("${javadoop.jdbc.url}")
private String url;

3.通过 Environment 获取

此法有需要注意的地方。并不是所有的配置方式都支持通过 Environment 接口来获取属性值,亲测只有使用注解 @PropertySource 的时候可以用,否则会得到 null,至于怎么配置,下面马上就会说。

@Autowired
private Environment env;

public String getUrl() {
    return env.getProperty("javadoop.jdbc.url");
}

4.本文件引用

# 应用名称
spring.application.name = api-eureka
# 应用端口
server.port = 9001

# Eureka Server主机名
eureka.instance.hostname = localhost
# 不注册到Eureka Service
eureka.client.register-with-eureka = false
eureka.client.fetch-registry = false
# Eureka Server地址
eureka.client.serviceUrl.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值