前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring.NET的AOP怎么玩

Spring.NET的AOP怎么玩

作者头像
用户1216676
发布2018-01-24 15:38:18
7780
发布2018-01-24 15:38:18
举报
文章被收录于专栏:熊二哥熊二哥

之前公司一直不让使用第三方组件,因此AOP方面的组建一直不能使用,很多面向切面的应用只能通过自己写一些GenericMethod的泛型方法来解决,有一些呆板。由于公司已经开始全面转Java,因此架构组放开了第三方组件的使用,这儿将对Spring.NET进行一个基础的学习。该项目虽然有1年都没有更新了(也反映了.NET品台热度的下降),但可以为未来使用JAVA最一定的铺垫,因此还是决定干了。

Spring.NET文档及官方地址:http://www.springframework.net/documentation.html

版本选择:1.3.2,创建日期为20110801.蛋蛋的忧伤。

Spring AOP基本原理:使用代理模式实现

这部分主要涉及两部分的内容,一种是通过代码添加Advices,一种是通过配置,推荐后者。

  • 应用建议(Applying advice):应用于类中所有方法,粒度太粗。 1 public class ConsoleLoggingAroundAdvice : IMethodInterceptor 2 { 3 public object Invoke(IMethodInvocation invocation) 4 { 5 Console.Out.WriteLine("Advice executing; calling the advised method..."); 6 object returnValue = invocation.Proceed(); 7 Console.Out.WriteLine("Advice executed; advised method returned " + returnValue); 8 return returnValue; 9 } 10 } 11 12 public interface ICommand 13 { 14 object Execute(object context); 15 } 16 17 public class ServiceCommand : ICommand 18 { 19 public object Execute(object context) 20 { 21 Console.Out.WriteLine("Service implementation : [{0}]", context); 22 return null; 23 } 24 } 25 26 [TestMethod] 27 public void TestMethod1() 28 { 29 ProxyFactory factory = new ProxyFactory(new ServiceCommand()); 30 factory.AddAdvice(new ConsoleLoggingAroundAdvice()); 31 ICommand command = (ICommand)factory.GetProxy(); 32 command.Execute("This is the argument"); 33 //ICommand command = (ICommand)ctx["myServiceObject"]; 34 //command.Execute("This is the argument"); 35 }

Using Pointcuts应用切入点:可以控制为方法级别的粒度,实际中最常用,这儿介绍配置的方式。

代码语言:javascript
复制
<object id="consoleLoggingAroundAdvice"
type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor">
<property name="pattern" value="Do"/>
<property name="advice">
<object type="Bjork.BaseService.BL.ConsoleLoggingAroundAdvice"/>
</property>
</object>
<object id="myServiceObject"
type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target">
<object id="myServiceObjectTarget"
type="Bjork.BaseService.BL.ServiceCommand"/>
</property>
<property name="interceptorNames">
<list>
<value>consoleLoggingAroundAdvice</value>
</list>
</property>
</object>
  • 接下来介绍其他的拦截器

Before advice

IMethodBeforeAdvice

Before(MethodInfo method, object[] args, object target)

After advice

IAfterReturningAdvice

AfterReturning(object returnValue, MethodInfo method, object[] args, object target)

Throws advice

IThrowsAdvice

AfterThrowing(Exception ex)

Around advice

IMethodInterceptor

Invoke(IMethodInvocation invocation)

  • Layering advice层次化建议(组合使用interceptor)
代码语言:javascript
复制
 1 //代码部分
 2 ProxyFactory factory = new ProxyFactory(new ServiceCommand());
 3 factory.AddAdvice(new ConsoleLoggingBeforeAdvice());
 4 factory.AddAdvice(new ConsoleLoggingAfterAdvice());
 5 factory.AddAdvice(new ConsoleLoggingThrowsAdvice());
 6 factory.AddAdvice(new ConsoleLoggingAroundAdvice());
 7 ICommand command = (ICommand) factory.GetProxy();
 8 command.Execute();
 9 
10  //配置部分
11 <object id="throwsAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingThrowsAdvice"/>
12 <object id="afterAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingAfterAdvice"/>
13 <object id="beforeAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingBeforeAdvice"/>
14 <object id="aroundAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingAroundAdvice"/>
15 <object id="myServiceObject"
16 type="Spring.Aop.Framework.ProxyFactoryObject">
17 <property name="target">
18 <object id="myServiceObjectTarget"
19 type="Spring.Examples.AopQuickStart.ServiceCommand"/>
20 </property>
21 <property name="interceptorNames">
22 <list>
23 <value>throwsAdvice</value>
24 <value>afterAdvice</value>
25 <value>beforeAdvice</value>
26 <value>aroundAdvice</value>
27 </list>
28 </property>
29 </object>

AOP的使用场景:缓存[Caching],性能监控,重试规则。

这部分内容就到此为止,还有其他事宜,这个暂时就不使用了,确实存在适用性上的问题。不像JAVA Spring一样的整合使用,确实使用性下降很多,比如不是所有的场景都适合使用容器,且会增加系统的复杂程度。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-11-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档