设为首页 加入收藏

TOP

Spring 中无处不在的 Properties(一)
2018-02-13 12:56:15 】 浏览:495
Tags:Spring 无处不在 Properties

对 Spring 里面的 Properties 不理解的开发者可能会觉得有点乱,主要是因为配置方式很多种,使用方式也很多种。

本文不是原理分析、源码分析文章,只是希望可以帮助读者更好地理解和使用 Spring Properties。

Properties 的使用

本文的读者都是使用过 Spring 的,先来看看 Properties 是怎么使用的,Spring 中常用的有以下几种使用方式:

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

如果是 Spring Boot 的 application.properties 注册的,那也是可以的。

Properties 配置

前面我们说了怎么使用我们配置的 Properties,那么该怎么配置呢?Spring 提供了很多种配置方式。

1. 通过 xml 配置

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

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

Spring Boot 相关

Spring Boot 真的是好东西,开箱即用的感觉实在是太好了。这里简单介绍下相关的内容。

快速生成一个 Spring B

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇如何优雅的设计 Java 异常 下一篇Java NIO:Buffer、Channel 和 Se..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目