北京展览网站建设,网站开始开发阶段的主要流程,saas建站平台源码,仿卢松松博客网站源码1.概述 本教程将展示如何通过XML或Java配置在Spring中设置和使用属性 。 在Spring 3.1之前 #xff0c;将新的属性文件添加到Spring并使用属性值并不像它那样灵活和健壮。 从Spring 3.1开始 #xff0c;新的Environment和PropertySource抽象大大简化了此过程。 2.通过XML名… 1.概述 本教程将展示如何通过XML或Java配置在Spring中设置和使用属性 。 在Spring 3.1之前 将新的属性文件添加到Spring并使用属性值并不像它那样灵活和健壮。 从Spring 3.1开始 新的Environment和PropertySource抽象大大简化了此过程。 2.通过XML名称空间注册属性 使用XML可以通过以下命名空间元素使Spring访问新的属性文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdcontext:property-placeholder locationclasspath: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 {Beanpublic 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 iddataSourceproperty nameurl 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中本地属性在所有环境属性源包括属性文件之后排在最后。 可以通过PropertySourcesPlaceholderConfigurer的localOverride属性来覆盖此行为可以将其设置为true以允许本地属性覆盖文件属性。 在Spring 3.0及更低版本中旧的PropertyPlaceholderConfigurer也尝试在手动定义的源以及系统属性中查找属性。 还可以通过配置程序的systemPropertiesMode属性自定义查找优先级 从不 –从不检查系统属性 备用 默认–检查系统属性如果在指定的属性文件中无法解析 覆盖 –在尝试指定的属性文件之前请先检查系统属性。 这允许系统属性覆盖任何其他属性源。 最后请注意如果在通过PropertySource定义的两个或多个文件中定义了属性则最后一个定义将获胜并覆盖之前的定义 。 这使得确切的属性值难以预测因此如果覆盖很重要则可以使用PropertySource API。 5.幕后–Spring配置 5.1。 在Spring 3.1之前 Spring 3.1引入了使用注释定义属性源的便捷选项–但在此之前必须使用XML Configuration。 contextproperty-placeholder XML元素自动在Spring上下文中注册一个新的PropertyPlaceholderConfigurer bean 。 为了向后兼容如果XSD架构尚未升级为指向新的3.1 XSD版本则在Spring 3.1中也是如此。 5.2。 在Spring 3.1之后 从Spring 3.1开始XML contextproperty-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 classorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationslistvalueclasspath:foo.properties/value/list/propertyproperty nameignoreUnresolvablePlaceholders valuetrue/
/bean7.在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 classorg.springframework.context.support.PropertySourcesPlaceholderConfigurerproperty namelocationlistvalueclasspath:foo.properties/value/list/propertyproperty nameignoreUnresolvablePlaceholders valuetrue/
/bean8.结论 本文展示了几个在Spring中使用属性和属性文件的示例 并讨论了旧的Spring 3.0选项以及Spring 3.1中引入的对属性的新支持。 可以在github项目中找到所有注册属性文件和使用属性值的示例的实现–这是一个基于Eclipse的项目因此应该很容易直接导入和运行。 参考来自bakgung博客的JCG合作伙伴 Eugen Paraschiv 提供的Spring属性 。 翻译自: https://www.javacodegeeks.com/2012/02/properties-with-spring.html