1.创建特性
[AttributeUsage(AttributeTargets.Method)]
public class TestAttribute:Attribute
{
}
2.在Service中所要拦截的方法上加入属性
[Test]
public Image GetTest()
{
}
3.编写拦截的扩展方法
using AopAlliance.Intercept
namespace Test
{
public class TestInterceptor:IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
using (new TestFunc()) //扩展方法
{
return invocation.Proceed();
}
}
}
}
4.service.xml 中配置关联
<object id="TestService" type="Spring.Aop.Framework.ProxyFactoryObject" >
<property name="Target">
<object type="Service.TestService.Service">
<property name="testDao" ref="testDao" /> //SERVICE注入的DAO
</object>
</property>
<property name="InterceptorNames">
<!--<idref local="IdentityPersonateAttributedAdvisor" />-->
<list>
<value>TestAttributedAdvisor</value>
</list>
</property>
</object>
<!--<object id="TestAttributedAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
<property name="Advice" ref="testAspect"/>
<property name="Attribute" value="Attribute.TestAttribute, Attribute" />
</object> 这种方法也可以-->
5.专门拦截的XML
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<!--线程伪装-->
<object id="testAspect" type="Attribute.TestInterceptor,Attribute" >
</object>
<object id="TestAttributedAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
<property name="Advice" ref="testAspect"/>
<property name="Attribute" value="Attribute.TestAttribute, Attribute" />
</object>
</objects>
======================================================================
方法2 针对具体SERVICE下的方法
<object id="CaseDurationPointcut" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
<!--<property name="pattern" value="GCDF.RecruitWorkflow.Service.CaseServiceProxy.*"/>-->
<!--pattern属性为拦截表达式。Service.*的意思是,拦截Service命名空间下(包括子空间)的所有类。