码迷,mamicode.com
首页 > 编程语言 > 详细

Spring读取配置文件

时间:2014-08-19 10:56:34      阅读:172      评论:0      收藏:0      [点我收藏+]

标签: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>




Spring读取配置文件,布布扣,bubuko.com

Spring读取配置文件

标签:spring   读取配置   

原文地址:http://blog.csdn.net/metecyu/article/details/38678381

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!