首页 > 代码库 > Spring读取配置文件

Spring读取配置文件

说明:

一般项目中都会有读取配置文件的需求,可以使用property文件、xml文件等方式。这里利用spring可读取对象的特性,写了一个读取属性的例子。

demo演示了项目常用的两种属性第一是key 、value关系的map对象(类似property文件)、列表对象list


java对象

package eway.flight.utils;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

/**
 *
 * @author yzp
 */
@Repository
public class SystemConfig {
    
    private Map valueMap;
    private List userids;
    

    public List getUserids() {
		return userids;
	}

	public void setUserids(List userids) {
		this.userids = userids;
	}

	public Map getValueMap() {
        return valueMap;
    }

    public void setValueMap(Map valueMap) {
        this.valueMap = valueMap;
    }
    
    public Object getValue(String key){
        if (this.getValueMap().containsKey(key)){
            return this.getValueMap().get(key);
        }else{
            return "";
        }
    }
    
    private List searchConfigList;

    public List getSearchConfigList() {
        return searchConfigList;
    }

    public void setSearchConfigList(List searchConfigList) {
        this.searchConfigList = searchConfigList;
    }

    private Map sendRangeConfig;

    public Map getSendRangeConfig() {
        return sendRangeConfig;
    }

    public void setSendRangeConfig(Map sendRangeConfig) {
        this.sendRangeConfig = sendRangeConfig;
    }
            
    private List syncUserConfigList;

    public List getSyncUserConfigList() {
        return syncUserConfigList;
    }

    public void setSyncUserConfigList(List syncUserConfigList) {
        this.syncUserConfigList = syncUserConfigList;
    }
    
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
    <bean id="SystemConfig" class="eway.flight.utils.SystemConfig">
        <property name="valueMap">
            <map>
                <entry key="mail_message_template">
                    <value>您有一封新邮件!发信人:{from};发送时间:{time};标题:{title}。</value>
                </entry>
                <entry key="schedule_module_id">
                    <value>68</value>
                </entry>
                <entry key="schedule_prompt_message_template">
                    <value>您的个人日程提醒 : {title} 将在 {time} 开始!。</value>
                </entry>
                <entry key="pm25_import_file_path">
                    <value>c:\pm25.txt</value>
                </entry>
            </map>
        </property>
        <property name="userids">
			<bean class="org.springframework.beans.factory.config.ListFactoryBean">
				<property name="targetListClass">
					<value>java.util.ArrayList</value>
				</property>
				<property name="sourceList">
					<list>
						<value>usr1</value>
						<value>usr2</value>
						<value>usr3</value>
					</list>
				</property>
			</bean>
		</property>      
    </bean>
</beans>