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>