spring中 context:property-placeholder 导入多个独立的 .properties配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件?

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代 PropertyPlaceholderConfigurer了)。

换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。
拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer, 因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个 PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。

咋解决呢?

通配符解决

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

_________________________________________________________________________________________

spring 的properties解析

来源: http://www.cnblogs.com/beiyeren/p/3488871.html

一般使用PropertyPlaceholderConfigurer来替换占位符,例如:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
      <value>classpath:com/foo/strategy.properties</value>
  </property>
  <property name="properties">
      <value>custom.strategy.class=com.foo.DefaultStrategy</value>
  </property>
</bean>

spring 2.5之后,可以使用

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

其本质是注册了一个PropertyPlaceholderConfigurer(3.1之前)或者是PropertySourcesPlaceholderConfigurer(3.1之后)

Tip:


PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。

而PropertySourcesPlaceholderConfigurer在此基础上会和Environment and PropertySource配合更好。

另外需要注意以下几点

1、在PropertyPlaceholderBeanDefinitionParser的父类中shouldGenerateId返回true,即默认会为每一个bean生成一个唯一的名字; 如果使用了两个<context:property-placeholder则注册了两个PropertySourcesPlaceholderConfigurer Bean;所以不是覆盖(而且bean如果同名是后边的bean定义覆盖前边的); 
2、PropertySourcesPlaceholderConfigurer本质是一个BeanFactoryPostProcessor,spring实施时如果发现这个bean实现了Ordered,则按照顺序执行;默认无序; 
3、此时如果给<context:property-placeholder加order属性,则会反应出顺序,值越小优先级越高即越早执行; 
比如 
   <context:property-placeholder order="2" location="classpath*:conf/conf_a.properties"/>  
   <context:property-placeholder order="1" location="classpath*:conf/conf_b.properties"/> 
此时会先扫描order=‘1‘ 的,如果没有扫描order=‘2‘的 
4、默认情况下ignore-unresolvable;即如果没找到的情况是否抛出异常。默认false:即抛出异常; 
<context:property-placeholder location="classpath*:conf/conf_a.properties" ignore-unresolvable="false"/>

原文地址:https://www.cnblogs.com/hfultrastrong/p/8427587.html

时间: 2024-08-29 05:22:32

spring中 context:property-placeholder 导入多个独立的 .properties配置文件的相关文章

spring中 context:property-placeholder 导入多个独立的配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceh

spring中context:property-placeholder解析

1.替代常量 应用场所:1在开发阶段我们连接数据库时的连接url,username,password,driverClass等 2分布式应用中client端访问server端所用的server地址,port,service等 2.参数需经常变动 在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况.  能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息 spring3中提供了一种简便的方式就是context

spring中context:property-placeholder/元素 转载

spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的server地址,port,service等 c.配置文件的位置 2.而这些参数在不同阶段之间又往往需要改变 比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况. 期望:能不能有一

如题,properties配置文件在项目中是经常用到的,那么读取properties配置文件的方法有哪些呢?

方法一:可以通过java.util.Properties类的load()方法 1 InputStreamin=lnewBufferedInputStream(newFileInputStream(name)); 2 Propertiesp=newProperties(); 3 p.load(in); 方法二:利用spring来读取properties配置文件org.springframework.beans.factory.support.PropertiesBeanDefinitionRead

spring中context:property-placeholder/元素

http://blog.sina.com.cn/s/blog_4550f3ca0100ubmt.html 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的server地址,port,service等 c.配置文件的位置 2.而这些参数在不同阶段之间又往往需要改变 比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况

关于spring中&lt;context:annotation-config/&gt;配置

在spring的配置文件中,如果我们一个一个地配置bean会非常麻烦,因此我们可以使用注解的方式 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean: 使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean 类似地,使用@Resource.@PostConstruct.@PreDestroy等注解就必须声明 CommonAnnotatio

使用Spring中的PropertyPlaceholderConfigurer读取文件

目录 一. 简介 二. XML 方式 三. Java 编码方式 一. 简介 大型项目中,我们往往会对我们的系统的配置信息进行统一管理,一般做法是将配置信息配置与一个cfg.properties 的文件中,然后在我们系统初始化的时候,系统自动读取 cfg.properties 配置文件中的 key value(键值对),然后对我们系统进行定制的初始化. 那么一般情况下,我们使用 的 java.util.Properties, 也就是 java 自带的.往往有一个问题是,每一次加载的时候,我们都需要

spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are property names wrapped with ${...},as an exampl,you can resolve the constructor arguments for a BlankDisc in xml like this : <bean id="sgtPeppers&qu

通过Spring配置文件中bean中的property赋值

基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用:可以把以前写dbc的东西写进去 2.如果属性的类型不是基本类型或String ,可以使用引用的方式为对象赋值(bean中property中的ref) 扩展-以此方式可以把数据库的连接值给实现类赋值 3.集合属性的赋值,注意要集合要初始化.基本数据类型不用初始化的原因就是它默认初始化(不常用) 4.