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

忘是亡心i 2022-06-13 05:22 290阅读 0赞

转载地址:http://blog.csdn.net/lee0723/article/details/48715827

对于属性配置,一般采用的是键值对的形式,如:
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. <**context:annotation-config />**
  9. <**context:component-scan base-package=”com.*“></context:component-scan**>
  10. <**bean** id=”setting”
  11. class=”org.springframework.beans.factory.config.PropertiesFactoryBean”>
  12. <**property name=”locations”>**
  13. <**list**>
  14. <**value**>classpath:*.properties</**value**>
  15. <**value**>file:config/*.conf</**value**>
  16. </**list**>
  17. </**property**>
  18. <**property name=”fileEncoding” value=”UTF-8”></property**>
  19. </**bean**>
  20. </**beans**>

7.新建一个获取属性配置文件属性的类ConfigProperty.java:

[java] view plain copy

  1. package com.lee.property.test;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * config.properties文件映射类
  6. *
  7. *
  8. */
  9. @Component(“configProperty”)
  10. public class ConfigProperty {
  11. /** 作者名字 */
  12. @Value(“#{setting[author_name]}“)
  13. private String authorName;
  14. /** 项目信息 */
  15. @Value(“#{setting[project_info]}“)
  16. private String projectInfo;
  17. public String getAuthorName() {
  18. return authorName;
  19. }
  20. public void setAuthorName(String authorName) {
  21. this.authorName = authorName;
  22. }
  23. public String getProjectInfo() {
  24. return projectInfo;
  25. }
  26. public void setProjectInfo(String projectInfo) {
  27. this.projectInfo = projectInfo;
  28. }
  29. }

8.新建测试类:

[java] view plain copy

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations={ “classpath:applicationContext.xml”})
  3. public class ConfigPropertyTest {
  4. @Resource(name = “configProperty”)
  5. private ConfigProperty configProperty;
  6. /**
  7. * 测试Spring注解获取properties文件的值
  8. */
  9. @Test
  10. public void test() {
  11. System.out.println(configProperty.getAuthorName());
  12. System.out.println(configProperty.getProjectInfo());
  13. }
  14. }

总结:

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

[java] view plain copy

  1. @Value(“${beanid[‘properties_key’]}“)

2.使用

[html] view plain copy

  1. <**property name=”fileEncoding” value=”UTF-8”></property**>

主要为了解决属性文件中value为中文时乱码的问题。

发表评论

表情:
评论列表 (有 0 条评论,290人围观)

还没有评论,来说两句吧...

相关阅读