Spring读取配置文件「建议收藏」

Spring读取配置文件

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

说明:

通常情况下,项目将有读取配置文件的需求,可以用于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>


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116714.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号