zoukankan      html  css  js  c++  java
  • 一个Spring的配置管理接口

    我同事写了个Java版的spring的配置文件管理的类,可以灵活的控制Spring配置文件的加载,尤其是能很好的照顾到工程和测试单元,感觉很好用,于是就移植到.NET上来。

    SystemConfig类,该类负责配置文件的管理,可根据项目的具体需求略做修改

    View Code
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Xml;

    public class SystemConfig
    {
    private const string SYSTEM_CONFIG_XML = "SystemConfig.xml";

    private static IList<String> springxmllist;
    private static StringDictionary dblist;

    private static void init()
    {
    XmlDocument xmlDoc;
    try
    {
    xmlDoc
    = new XmlDocument();
    string path = AppDomain.CurrentDomain.BaseDirectory;
    xmlDoc.Load(path
    + SYSTEM_CONFIG_XML);
    XmlNode xmlNodes
    = xmlDoc.SelectSingleNode("systemconfig/spring-config");
    XmlNodeList nodelist
    = xmlNodes.SelectNodes("//spring-xml");

    // spring配置文件列表
    try
    {
    springxmllist
    = new List<string>();
    foreach (XmlElement el in nodelist) //读元素值
    {
    springxmllist.Add(el.InnerText.Trim());
    Console.WriteLine(el.InnerText.Trim());
    }
    }
    catch (Exception ex1491)
    {
    Console.WriteLine(ex1491.StackTrace);
    }

    // 数据库列表
    try
    {
    dblist
    = new StringDictionary();
    xmlNodes
    = xmlDoc.SelectSingleNode("systemconfig/dblist");
    nodelist
    = xmlNodes.SelectNodes("//dbid");
    foreach (XmlElement el in nodelist)
    {
    dblist.Add(el.InnerText.Trim(), el.Attributes[
    "name"].InnerText);
    Console.WriteLine(el.InnerText
    + el.Attributes["name"].InnerText);
    }
    }
    catch (Exception ex1492)
    {
    Console.WriteLine(ex1492.StackTrace);
    }

    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.StackTrace);
    }
    }

    public static IList<String> getSpringxmllist()
    {
    if (springxmllist == null)
    {
    init();
    }
    return springxmllist;
    }

    public void setSpringxmllist(IList<String> springxmllist)
    {
    SystemConfig.springxmllist
    = springxmllist;
    }

    public static StringDictionary getDblist()
    {
    if (dblist == null)
    {
    init();
    }
    return dblist;
    }

    public static void setDblist(StringDictionary dblist)
    {
    SystemConfig.dblist
    = dblist;
    }

    }

    SpringContext类,该类负责加载Spring配置文件

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spring.Context;
    using Spring.Context.Support;
    public class SpringContext {
    private static IApplicationContext ctx = null;

    private SpringContext() { /* 单例模式,防止反复加载 */ }

    static public IApplicationContext getApplicationContext() {
    if (ctx == null) {
    // 从配置文件SystemConfig.xml中加载spring配置文件
    int xmlCount = SystemConfig.getSpringxmllist().Count;
    string[] springxml = new string[xmlCount];
    for (int i = 0; i < xmlCount; i++)
    springxml[i]
    = SystemConfig.getSpringxmllist()[i];
    ctx
    = new XmlApplicationContext(springxml);
    }
    return ctx;
    }
    }

    SystemConfig.xml文件,一个参考样本

    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    <systemconfig>
    <spring-config>
    <spring-xml>data.xml</spring-xml>
    <spring-xml>applicationContext-resources.xml</spring-xml>
    <spring-xml>applicationContext-transactions.xml</spring-xml>
    <spring-xml>applicationContext-dao.xml</spring-xml>
    </spring-config>
    <dblist>
    <dbid name="测试数据库">11</dbid>
    <dbid name="xx1数据库">10</dbid>
    <dbid name="xx2数据库">31</dbid>
    <dbid name="xx3数据库">51</dbid>
    </dblist>
    </systemconfig>

  • 相关阅读:
    Java的基本数据类型及其封装类
    牛客寒假3-A |dp走格子
    牛客寒假1-I nico和niconiconi| 字符串 线性dp
    牛客寒假2-C算概率| 概率dp
    牛客寒假6-B重排列| dp计数
    牛客寒假6-B图| 统计环中结点个数,计算最大链长
    三分法 求最大值最小|牛客寒假5-B牛牛战队的比赛地
    P3382|三分法模板
    思维题(取模)| CF#615Div3 D.MEX maximizing
    树的直径| CF#615Div3 F. Three Paths on a Tree
  • 原文地址:https://www.cnblogs.com/rockythd/p/2085510.html
Copyright © 2011-2022 走看看