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

本文介绍了如何利用Spring的PropertiesFactoryBean来管理属性配置文件。通过创建upload.properties文件并注入到MyUploadUtil类中,可以灵活地改变文件上传的保存路径。使用方式包括@Value注解结合beanid和properties_key来获取属性值,同时解决可能出现的编码问题,可以通过设置FileEncoding属性来避免。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于属性配置,一般采用的是键值对的形式,如:
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>


好的,关于spring配置文件加载properties文件,可以使用以下几种方式: 1. 使用PropertyPlaceholderConfigurer属性占位符 可以在配置文件中使用${}占位符来引用属性值,同时需要在配置文件中引入对应的*.properties文件。在Spring容器启动时,会通过PropertyPlaceholderConfigurer将properties配置文件中的键值对装载到Spring的环境变量中,供${}占位符使用。 例如: ``` <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/config/app.properties</value> </list> </property> </bean> <bean id="user" class="com.example.User"> <property name="name" value="${user.name}"/> <property name="age" value="${user.age}"/> </bean> ``` 在上面的例子中,将classpath:/config/app.properties中的键值对装载到Spring的环境变量中,供${}占位符使用。 2. 使用util命名空间的PropertiesFactoryBean 可以在Spring配置文件中使用util命名空间的PropertiesFactoryBean来装载properties文件中的属性,并且使用${}占位符引用这些属性值。 例如: ``` <util:properties id="appConfig" location="classpath:/config/app.properties"/> <bean id="user" class="com.example.User"> <property name="name" value="${user.name}" /> <property name="age" value="${user.age}" /> </bean> ``` 在上面的例子中,通过util:properties装载classpath:/config/app.properties中的属性。在User bean中使用${}占位符引用属性值。 希望这些方法能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值