1.通过nuget搜索springaop 获取包并下载
2.先创建类和接口
3.创建好之后在webconfig中配置
依次找到configuration--》configSections 节点 如果没有则创建 注意 configSections 必须在configuration节点的第一个,否者报错。
然后加入如下节点信息:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<!--<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />-->
</sectionGroup>
然后在 configuration 节点下加入如下节点信息,无所谓位子
<spring>
<context>
<resource uri="file://~/config/objects.xml"/>
</context>
<!--<objects>
<object id="Alice" type="Person"></object>
</objects>-->
</spring>
这里注意:resource节点是指向自创XML文件地址 前面file://表示是文件 后面是项目相对路径 ,各自配置路径不一致,名字不一致请自行修改。然后在该文件下配置如下代码:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="Example" type="Core.BLL.CoreService,Core.BLL" singleton="false"></object>
</objects>
第一行表示文件信息,创建时自动创建,不用管,后面为标准配置信息,name为唯一标识,相当于主键,不可重复。type分2个部分,前一部分表示类的完整调用路径,后一部分表示程序集,singleton表示类是否为单例。
OK,配置结束,接下来调用就很简单了
先应用 using Spring.Context.Support;
然后 IExample exp = ContextRegistry.GetContext().GetObject("Example") as IExample;
exp.GetInt();
这个是我做的例子。
PS :注意XML文件属性需要调整为始终复制。