您的位置:首页 > 编程语言 > Java开发

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

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

1.首先新建一个properties文件。名为:upload.properties

path=D:\\upload1\\

2.在spring-mvc.xml配置文件中配置一个propertiesFactoryBean
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:/upload.properties"></property>
</bean>

<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="*"></context:component-scan>
</beans>3.在需要注入properties指定key的value值得地方写上@Value注解。通常是写在类的属性上,即对类的属性进行注入
例如这里是对MyUploadUtil类中的path属性进行注入,实现能灵活改变上传文件保存的目录

package util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class MyUploadUtil {
/*
* 注入字符串,#{}为spel语言,其中uploadProperties,是xml配置文件中注入properties文件的bean id,
* path为properties文件的其中一个key ,也可以通过下边的set方法注入
*/

@Value("#{uploadProperties[path]}")//@Value("#{uploadProperties.path}")
private String path;

//获取文件名后缀
public String getExtension(MultipartFile file){
return FilenameUtils.getExtension(file.getOriginalFilename());
}
//创建新的文件名
public String createNewName(MultipartFile file){
return UUID.randomUUID().toString()+"."+getExtension(file);
}
//保存
@SuppressWarnings("finally")
public String upload(MultipartFile file){
String newName=null;
try {
newName=createNewName(file);
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path,newName));
} catch (IOException e) {
e.printStackTrace();
}finally {
return newName;
}
}
}


总结:
1.使用org.springframework.beans.factory.config.PropertiesFactoryBean获取属性的方式是:
 
   @Value("#{beanid[properties_key]}") 

     @Value("#{beanid.properties_key}") 
  例如上文提到的
   
    @Value("#{uploadProperties[path]}")

        @Value("#{uploadProperties.path}")

有时可能还会碰到编码问题,就要给PropertiesFactoryBean再注入一个FileEncoding的属性,

<bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:/upload.properties"></property>
<property name="fileEncoding" value="utf-8"></property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐