spring管理属性配置文件properties——使用PropertiesFactoryBean

来源:互联网 发布:如何评价secrets 知乎 编辑:程序博客网 时间:2024/06/02 05:48

 

对于属性配置,一般采用的是键值对的形式,如:
key=value
属性配置文件一般使用的是XXX.properties,当然有时候为了避免eclipse把properties文件转码,放到服务器上认不出中文,可以采用XXX.conf的形式管理属性配置。
spring对于属性文件有自己的管理方式,通过spring的管理,可以直接使用@Value的方式直接得到属性值。
先使用org.springframework.beans.factory.config.PropertiesFactoryBean对属性配置文件进行管理。

1.新建一个java project,命名spring_test;

2.导入jar包:

[plain] view plain copy
  1. aopalliance-1.0.jar  
  2. commons-logging-1.1.1.jar  
  3. org.springframework.test-3.1.0.RELEASE.jar  
  4. spring-aop-3.1.1.RELEASE.jar  
  5. spring-asm-3.1.1.RELEASE.jar  
  6. spring-beans-3.1.1.RELEASE.jar  
  7. spring-context-3.1.1.RELEASE.jar  
  8. spring-context-support-3.1.1.RELEASE.jar  
  9. spring-core-3.1.1.RELEASE.jar  
  10. spring-expression-3.1.1.RELEASE.jar  
3.在src下新建一个config.propertites:
[plain] view plain copy
  1. author_name=lee  

4.新建一个文件夹config;

5.新建一个app.conf:

[plain] view plain copy
  1. project_info=项目  
6.新建一个spring配置文件applicationContext.xml:
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  6.     http://www.springframework.org/schema/context   
  7.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  8.   
  9.     <context:annotation-config />  
  10.     <context:component-scan base-package="com.*"></context:component-scan>  
  11.   
  12.     <!-- 使用注解注入properties中的值 -->  
  13.     <bean id="setting"  
  14.         class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  15.         <property name="locations">  
  16.             <list>  
  17.                 <value>classpath:*.properties</value>  
  18.                 <value>file:config/*.conf</value>  
  19.             </list>  
  20.         </property>  
  21.         <!-- 设置编码格式 -->  
  22.         <property name="fileEncoding" value="UTF-8"></property>  
  23.     </bean>  
  24. </beans>  
7.新建一个获取属性配置文件属性的类ConfigProperty.java:
[java] view plain copy
  1. package com.lee.property.test;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. /** 
  7.  * config.properties文件映射类 
  8.  *  
  9.  * 
  10.  */  
  11. @Component("configProperty")  
  12. public class ConfigProperty {  
  13.   
  14.     /** 作者名字 */  
  15.     @Value("#{setting[author_name]}")  
  16.     private String authorName;  
  17.     /** 项目信息 */  
  18.     @Value("#{setting[project_info]}")  
  19.     private String projectInfo;  
  20.   
  21.     public String getAuthorName() {  
  22.         return authorName;  
  23.     }  
  24.   
  25.     public void setAuthorName(String authorName) {  
  26.         this.authorName = authorName;  
  27.     }  
  28.   
  29.     public String getProjectInfo() {  
  30.         return projectInfo;  
  31.     }  
  32.   
  33.     public void setProjectInfo(String projectInfo) {  
  34.         this.projectInfo = projectInfo;  
  35.     }  
  36.   
  37. }  
8.新建测试类:
[java] view plain copy
  1. @RunWith(SpringJUnit4ClassRunner.class)   
  2. @ContextConfiguration(locations={"classpath:applicationContext.xml"})  
  3. public class ConfigPropertyTest {  
  4.    
  5.     @Resource(name = "configProperty")  
  6.     private ConfigProperty configProperty;  
  7.        
  8.     /** 
  9.      * 测试Spring注解获取properties文件的值 
  10.      */  
  11.     @Test  
  12.     public void test() {  
  13.         System.out.println(configProperty.getAuthorName());  
  14.         System.out.println(configProperty.getProjectInfo());  
  15.     }  
  16.    
  17. }  

总结:

1.使用org.springframework.beans.factory.config.PropertiesFactoryBean获取属性的方式是:

[java] view plain copy
  1. @Value("${beanid['properties_key']}")  
2.使用
[html] view plain copy
  1. <!-- 设置编码格式 -->  
  2. <property name="fileEncoding" value="UTF-8"></property>  
主要为了解决属性文件中value为中文时乱码的问题。
阅读全文
0 0
原创粉丝点击