1.在项目中引入Spring.Aop.dll Spring.core.dll和antlr.runtime.dll
2.app.config(web.config)中加入以下代码,请注意注释
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
<!--resource uri="file://~/spring.config"/--><!--这里使用独立配置文件-->
</context>
<objects xmlns="http://www.springframework.net">
<object id="aroundAdvice" type="SpringLib.Lib.AroundAdvice,SpringLib" />
<object id="myCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="SpringLib.Lib.ServiceCommand,SpringLib" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvice</value>
<!--<value>throwsAdvice</value>-->
</list>
</property>
</object>
</objects>
</spring>
使用独立配置文件时的示例代码(spring.config)
<objects xmlns="http://www.springframework.net">
<object id="aroundAdvice" type="SpringLib.Lib.AroundAdvice,SpringLib" />
<object id="myCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="SpringLib.Lib.ServiceCommand,SpringLib" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvice</value>
<!--<value>throwsAdvice</value>-->
</list>
</property>
</object>
</objects>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
<!--resource uri="file://~/spring.config"/--><!--这里使用独立配置文件-->
</context>
<objects xmlns="http://www.springframework.net">
<object id="aroundAdvice" type="SpringLib.Lib.AroundAdvice,SpringLib" />
<object id="myCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="SpringLib.Lib.ServiceCommand,SpringLib" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvice</value>
<!--<value>throwsAdvice</value>-->
</list>
</property>
</object>
</objects>
</spring>
使用独立配置文件时的示例代码(spring.config)
<objects xmlns="http://www.springframework.net">
<object id="aroundAdvice" type="SpringLib.Lib.AroundAdvice,SpringLib" />
<object id="myCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="SpringLib.Lib.ServiceCommand,SpringLib" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvice</value>
<!--<value>throwsAdvice</value>-->
</list>
</property>
</object>
</objects>
3.建立一个独立的Class Library 工程,建立以下示例文件
//AroundAdvice.cs
using System;
using AopAlliance.Intercept;
namespace SpringLib.Lib
{
public class AroundAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
System.Windows.Forms.MessageBox.Show("bengin.");
object returnValue = invocation.Proceed();
System.Windows.Forms.MessageBox.Show("end.");
return returnValue;
}
}
}
//--------------------------------
//ICommand.cs
namespace SpringLib.Lib
{
public interface ICommand
{
bool IsUndoCapable { get; }
void Execute();
void UnExecute();
}
}
//----------------------------------
//ServiceCommand.cs
using System;
namespace SpringLib.Lib
{
public class ServiceCommand : ICommand
{
#region ICommand Members
public bool IsUndoCapable
{
get { return true; }
}
public void Execute()
{
System.Windows.Forms.MessageBox.Show("Execute...");
}
public void UnExecute()
{
System.Windows.Forms.MessageBox.Show("UnExecute()...");
}
#endregion
}
}
using System;
using AopAlliance.Intercept;
namespace SpringLib.Lib
{
public class AroundAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
System.Windows.Forms.MessageBox.Show("bengin.");
object returnValue = invocation.Proceed();
System.Windows.Forms.MessageBox.Show("end.");
return returnValue;
}
}
}
//--------------------------------
//ICommand.cs
namespace SpringLib.Lib
{
public interface ICommand
{
bool IsUndoCapable { get; }
void Execute();
void UnExecute();
}
}
//----------------------------------
//ServiceCommand.cs
using System;
namespace SpringLib.Lib
{
public class ServiceCommand : ICommand
{
#region ICommand Members
public bool IsUndoCapable
{
get { return true; }
}
public void Execute()
{
System.Windows.Forms.MessageBox.Show("Execute...");
}
public void UnExecute()
{
System.Windows.Forms.MessageBox.Show("UnExecute()...");
}
#endregion
}
}