弹簧特性

1.概述

本教程将展示如何通过XML或Java配置在Spring中设置和使用属性

在Spring 3.1之前 ,将新的属性文件添加到Spring并使用属性值并不像它那样灵活和健壮。 从Spring 3.1开始 ,新的EnvironmentPropertySource抽象大大简化了此过程。

2.通过XML名称空间注册属性

使用XML,可以通过以下命名空间元素使Spring访问新的属性文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.2.xsd">

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

</beans>

foo.properties文件应放在/ src / main / resources下,以便在运行时可在类路径上使用。

多个

如果在Spring上下文中存在多个<property-placeholder>元素 ,则应遵循一些最佳实践:

  • 需要指定order属性来固定Spring处理这些订单的顺序
  • 所有属性占位符减去最后一个(最高顺序 )应具有ignore-unresolvable =“ true”,以允许解析机制在上下文中传递给其他对象而不会引发异常

3.通过Java注释注册属性

Spring 3.1还引入了新的@PropertySource批注 ,作为将属性源添加到环境的便捷机制。 该注释将与基于Java的配置和@Configuration注释一起使用:

@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {

   @Bean
   public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
   }
}

与使用XML名称空间元素相反,Java @PropertySource批注不会自动向Spring注册PropertySourcesPlaceholderConfigurer 。 相反,必须在配置中显式定义Bean,以使属性解析机制正常工作。 此意外行为背后的原因是设计使然,并对此问题进行了记录

4.使用属性

在Spring 3.1中添加的较旧的PropertyPlaceholderConfigurer和新的PropertySourcesPlaceholderConfigurer都可以在bean定义属性值和@Value批注中解析$ {…}占位符

例如,要使用@Value注释注入属性:

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

还可以指定属性的默认

@Value( "${jdbc.url:aDefaultUrl}" )
private String jdbcUrl;

在Spring XML配置中使用属性:

<bean id="dataSource">
  <property name="url" value="${jdbc.url}" />
</bean>

最后,通过新的环境API获取属性:

@Autowired
private Environment env;
...
dataSource.setUrl(env.getProperty("jdbc.url"));

一个非常重要的警告是,使用<property-placeholder> 不会将属性公开给Spring Environment –这意味着检索这样的值将不起作用–它将返回null

env.getProperty("key.something")

4.1属性搜索优先级

默认情况下,在Spring 3.1中,本地属性在所有环境属性源(包括属性文件)之后排在最后。 可以通过PropertySourcesPlaceholderConfigurerlocalOverride属性来覆盖此行为,可以将其设置为true以允许本地属性覆盖文件属性。

在Spring 3.0及更低版本中,旧的PropertyPlaceholderConfigurer也尝试在手动定义的源以及系统属性中查找属性。 还可以通过配置程序的systemPropertiesMode属性自定义查找优先级:

  • 从不 –从不检查系统属性
  • 备用 (默认)–检查系统属性,如果在指定的属性文件中无法解析
  • 覆盖 –在尝试指定的属性文件之前,请先检查系统属性。 这允许系统属性覆盖任何其他属性源。

最后,请注意,如果在通过@PropertySource定义的两个或多个文件中定义了属性,则最后一个定义将获胜并覆盖之前的定义 。 这使得确切的属性值难以预测,因此,如果覆盖很重要,则可以使用PropertySource API。

5.幕后–Spring配置

5.1。 在Spring 3.1之前

Spring 3.1引入了使用注释定义属性源的便捷选项–但在此之前,必须使用XML Configuration。

<context:property-placeholder> XML元素自动在Spring上下文中注册一个新的PropertyPlaceholderConfigurer bean 。 为了向后兼容,如果XSD架构尚未升级为指向新的3.1 XSD版本,则在Spring 3.1中也是如此。

5.2。 在Spring 3.1之后

从Spring 3.1开始,XML <context:property-placeholder>将不再注册旧的PropertyPlaceholderConfigurer,而是新注册的PropertySourcesPlaceholderConfigurer 。 创建此替换类是​​为了更灵活并更好地与新引入的Environment and PropertySource机制进行交互。

对于使用Spring 3.1或更高版本的应用程序,应将其视为标准。

6.在Spring 3.0中使用Raw Bean进行配置–

除了将属性放入Spring(注释和XML名称空间)的便捷方法之外,还可以手动定义和注册属性配置bean。 使用PropertyPlaceholderConfigurer使我们可以完全控制配置,但缺点是过于冗长,并且在大多数情况下是不必要的。

6.1。 Java配置
@Bean
public static PropertyPlaceholderConfigurer properties(){
  PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  Resource[] resources = new ClassPathResource[ ]
    { new ClassPathResource( "foo.properties" ) };
  ppc.setLocations( resources );
  ppc.setIgnoreUnresolvablePlaceholders( true );
  return ppc;
}
6.2。 XML配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <value>classpath:foo.properties</value>
    </list>
  </property>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

7.在Spring 3.1中使用Raw Bean进行配置–

同样,在Spring 3.1中,也可以手动配置新的PropertySourcesPlaceholderConfigurer

7.1。 Java配置
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
  PropertySourcesPlaceholderConfigurer pspc =
    new PropertySourcesPlaceholderConfigurer();
  Resource[] resources = new ClassPathResource[ ]
    { new ClassPathResource( "foo.properties" ) };
  pspc.setLocations( resources );
  pspc.setIgnoreUnresolvablePlaceholders( true );
  return pspc;
}
7.2。 XML配置
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  <property name="location">
    <list>
      <value>classpath:foo.properties</value>
    </list>
  </property>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

8.结论

本文展示了几个在Spring中使用属性和属性文件的示例 ,并讨论了旧的Spring 3.0选项以及Spring 3.1中引入的对属性的新支持。

可以在github项目中找到所有注册属性文件和使用属性值的示例的实现–这是一个基于Eclipse的项目,因此应该很容易直接导入和运行。

参考:来自bakgung博客的JCG合作伙伴 Eugen Paraschiv 提供的Spring属性

翻译自: https://www.javacodegeeks.com/2012/02/properties-with-spring.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值