c#扩展SPRING,配置里有不存在的业务系统xml导致不能启动问题,把配置文件里有重复的服务ID找出来并写入到日志

本文介绍如何在Spring框架中实现懒加载配置,并通过具体示例代码展示了自定义Spring上下文处理器的方法,同时提供了配置app.config文件的具体步骤。

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



1:引用spring.aop,spring.core

 

2:编写类文件继承ContextHandler,代码如下

 public static log4net.ILog log = LogHelper.GetInstance();
        protected override string GetContextName(object configContext, System.Xml.XmlElement contextElement)
        {
            return base.GetContextName(configContext, contextElement);
        }
        protected override bool AutoRegisterWithContextRegistry
        {
            get
            {
                return base.AutoRegisterWithContextRegistry;
            }
        }

        protected override Spring.Context.IApplicationContext InstantiateContext(Spring.Context.IApplicationContext parentContext, object configContext, string contextName, Type contextType, bool caseSensitive, IList<string> resources)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
            List<string> verifyResources = new List<string>();
            if (directoryInfo != null && directoryInfo.Exists)
            {
                if (resources != null)
                {
                    for (int i = 0; i < resources.Count; i++)
                    {
                        FileInfo[] files = directoryInfo.GetFiles(resources[i], SearchOption.AllDirectories);
                        if (files != null && files.Length > 0)
                        {
                            verifyResources.Add(resources[i]);
                        }
                        else
                        {
                            log.Warn("配置节配置的文档不全:" + resources[i] + "文件不存在");
                        }
                    }
                    //读取配置文件中的所有节的信息
                    GetConentName(verifyResources);
                }
            }
            return base.InstantiateContext(parentContext, configContext, contextName, contextType, caseSensitive, verifyResources);
        }

        private void GetConentName(List<string> getresources)
        {
            Dictionary<string, string> getAllContentName = new Dictionary<string, string>();
            List<ReportConten> contents = new List<ReportConten>();
            if (getresources.Count >= 1)
            {
                for (int i = 0; i < getresources.Count; i++)
                {
                    XmlDocument xml = new XmlDocument();            //读取xml文件           
                    xml.Load(getresources[i]);
                    foreach (XmlNode node in xml.ChildNodes)
                    {
                        if (node.Name == "objects")
                        {
                            foreach (XmlNode itemNode in node.ChildNodes)
                            {
                                if (itemNode.Name == "object")
                                {
                                    string name = (itemNode as XmlElement).GetAttribute("id");
                                    if (getAllContentName.ContainsKey(name))
                                    {
                                        if (!string.IsNullOrWhiteSpace(name))
                                        {
                                            if (name != "ContextInfo" && name != "ContextManager")
                                            {
                                                ReportConten re = new ReportConten();
                                                re.OldXmlName = getresources[i];
                                                re.OldXmlConten = name;
                                                re.XmlName = getAllContentName[name].ToString();
                                                re.XmlConten = name;
                                                contents.Add(re);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        getAllContentName.Add(name, getresources[i]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            JuestRepeatContentId(contents);
        }

        private void JuestRepeatContentId(List<ReportConten> re)
        {
            foreach (var item in re)
            {
                StringBuilder FullName = new StringBuilder();
                FullName.Append("*****************" + "\r\n");
                FullName.Append(item.OldXmlName + "中的" + item.OldXmlConten + "与");
                FullName.Append(item.XmlName + "中的" + item.XmlConten + "名称一致了,请修改" + "\r\n");
                FullName.Append("*****************");
                log.Warn(FullName);
            }
        }

 

3:在你的工程app.config的文件配置中的xml每一个<object

>节都要进行懒加载的配置lazy-init="true"


4:在你的APP.CONFIG的文件中添加如SPRING的配置,例如    <sectionGroup name="spring">
      <section name="context" type="IShiTong.Project.Common.STSpringContextHandler, IShiTong.Project.Common" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core" />
    </sectionGroup>

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值