Spring 读取properties文件key+value方式 妖狐艹你老母 2021-06-24 14:37 644阅读 0赞 1、 spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里,可以方便的通过spring获取properties文件中的key和value。 有如下几种方式: 1)PropertyPlaceholderConfigurer类进行文件信息配置: PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,能够对<bean/>中的属性值进行外在化管理。 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>userinfo.properties</value> </list> </property> </bean> <bean name="userInfo" class="test.UserInfo"> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> 2)PreferencesPlaceholderConfigurer类进行配置: 注意:PreferencesPlaceholderConfigurer是PropertyPlaceholderConfigurer的子类; <bean id="preferencesConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="location"> <value>config.properties</value> </property> </bean> 3) 使用PropertiesFactoryBean+PreferencesPlaceholderConfigurer方式: <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>*.properties</value> </list> </property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean> 4)使用context命名空间下的标签:(推荐使用这种方式) 首先 在applicationContext.xml中加入命名空间: <beans xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd> 然后使用下面标签即可: <context:property-placeholder location="classpath*:resources/*.properties" /> 注意:spring容器中最多只能定义一个context:property-placeholder。 5)使用util命名空间下的标签: 首先在applicationContext.xml中加入命名空间: <beans xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> </beans> 然后使用如下标签即可: <util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" /> 注:<util:>在使用上和上面4中使用方式有些不同,但 PreferencesPlaceholderConfigurer,PreferencesPlaceholderConfigurer和<context:/>标签在使用上是等价的。 2、 `<context:property-placeholder>` 标签具体使用: 它提供了一种优雅的外在化参数配置的方式,不过该标签在 [spring][] 配置文件中只能存在一份!!! 众所周知,Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个`org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer`的Bean就会停止对剩余`PropertyPlaceholderConfigurer`的扫描,即只能存在一个实例! <context:property-placeholder location="" file-encoding="" ignore-resource-not-found="" ignore-unresolvable="" properties-ref="" local-override="" system-properties-mode="" order="" /> 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 location:表示属性文件位置,多个之间通过如逗号/分号等分隔; file-encoding:文件编码; ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常 ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常 properties-ref:本地[Java][].util.Properties配置 local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性 system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment NEVER:只查找properties-ref、location; order:当配置多个`<context:property-placeholder/>`时的查找顺序 使用注意: 1.location中的加载文件的顺序 如果location中有多个文件: `classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties` 将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值 举例来说: default.properties文件中有个属性名userId,其对应的值为-1 default2.properties文件中也有一个属性名userId,其对应的值为-2 default3.properties文件中特有一个属性名userId,其对于那个的值为-3 default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2 所以需要避免不同属性文件中的属性名称重名 2.ignore-resource-not-found和ignore-unresolvable两个属性是类似的作用,推荐配对使用 如果location中的文件指向了一个不存在的文件,那么也极有可能意味着有属性无法解析(虽然存在其他属性文件中存在重名,但是这个是应该避免的) 所以当ignore-resource-not-found设为true时,ignore-unresolvable也必须设为true,其实当ignore-unresolvable设为true时,ignore-resource-not-found的值true或false,并不影响异常的抛出 如果设置为ture,后属性值无法解析成功,将赋值为$\{属性名\} 不推荐将ignore-resource-not-found和ignore-unresolvable的值设置为ture,默认为false,可以有效避免程序运行异常 3.properties-ref属性 引入其他方式引入的属性文件 <bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:default2.properties</value> </list> </property> </bean> 1 2 3 4 5 6 7 1 2 3 4 5 6 7 该属性需要local-override配合使用,只有当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性) 4.local-override属性 当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性) 5.sytem-properties-mode属性 不同的sytem-properties-mode属性定义了不同的查找顺序 Environment环境:包括JDK环境,系统环境变量,Sevlet环境,Spring环境等,是Spring在3.1之后抽象的一个表示环境配置 在local-override属性值为false,sytem-properties-mode属性值为ENVIRONMENT或OVRRIDE时,查找顺序是location,然后是environment或者System.getProperty(),System.getenv()(Spring 3.1 之前) 即现加载location指向的属性文件,再加之environment指向的环境,当environment环境中存在和location指向的属性文件中同名的属性,则该属性的值将被修改,取决于environment环境中的值 如果sytem-properties-mode属性值为NEVER,则只查询location指向的属性文件 当local-override属性值为true时,最后将加载properties-ref指向的文件,如遇到同名的,该同名属性值将取决于properties-ref指向的文件中的值 所以,最终程序中获取的值将是一个综合作用后的值,一般情况下建议sytem-properties-mode属性值为NEVER避免ENVIRONMENT环境中的不可控 [spring]: http://lib.csdn.net/base/javaee [Java]: http://lib.csdn.net/base/javase
还没有评论,来说两句吧...