B\S备忘录11——新技能AOP get√

来源:互联网 发布:windows管理员权限cmd 编辑:程序博客网 时间:2024/05/16 10:46

  当我第一次知道要结合AOP的时候,其实我是,是拒绝的,我跟组长讲,我拒绝,因为,其实我,根本没看过AOP。组长跟我讲,看看就会了,项目很高很大很上……看了一天AOP之后呢,程序DUANG~~~后来我也知道他们是简单的,是简单的Demo。我现在呢,每天还是看AOP,看了很多AOP,程序,DUANG~~DUANG~~DUANG~~工作流更加抽象,因为我,用AOP。

  好的,编不下去了。

  都没什么时间去编这些乱七八糟的玩意了,话说我们搞了这么长时间的工作流,最终也是要与AOP结合起来,这么一看顺便就学了AOP呗。

  首先看看我度娘是怎么说的:AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

  恩,看起来就是不容易懂,经过我这一点点时间的学习,AOP这只能有一个大概的了解,那我就继续胡说八道了。所谓的AOP其实就是把一些需要经常使用的方法抽象出来,然后人为的去设置到底在哪去用这些方法。

  怎么样?是不是跟我们一直在做的事情一样,以前怎么使用这种方法呢?还是SQLHelper的例子,我们抽象出来了好多的方法,然后在D层不断地使用它。AOP则是直接把这个调用D层方法的代码,抽出来,放到一个切面中,之后设置在哪里切,那么这个方法就能自动执行了。

  那么这个切面是什么呢?切面我的理解很简单,在哪切,切了之后做什么,这些东西的集合就是切面,不管对不对的,先这么认为了- -

  好的接下来就是怎么用了。这个目前在只学习了一点点,只能使用看着别人的例子做一个。首先是要先引用Spring.AOP,之后用的依然是已经封装好的东西。目前只看了一部分根据对象名称作为切入点的方式,其实就是搜索,看看有没有叫这个的对象,然后设置要拦截的方法名,如果匹配成功那么就能执行切面中的方法。

 public class AfterAdvice : IAfterReturningAdvice    {        public void AfterReturning(object returnValue, System.Reflection.MethodInfo method, object[] args, object target)        {            //Console.WriteLine("开始:  " + method.Name);<span style="white-space:pre"></span>    //工作流的内容            WFEntity wfEntity = new WFEntity();//这里应为读取缓存中的实体            wfEntity.CurrentUser = "lj";            wfEntity.ProcessKey = "BasicSystemWorkFlow";            wfEntity.FormURL = "../StudentChange/Check";            wfEntity.KeyId = "78fd4178-3b2a-4a94-b88a-51f5679724d0";            wfEntity.Flag = true;            string currentUser = wfEntity.CurrentUser;            string processKey = wfEntity.ProcessKey;            string formURL = wfEntity.FormURL;            string strAssigner = wfEntity.StrAssigner;            Guid keyId = new Guid(wfEntity.KeyId);            string strApprovalResult = wfEntity.StrApprovalResult;            string strApprovalContent = wfEntity.StrApprovalContent;            WFPCaseFrade wfpCaseFrade = new WFPCaseFrade();            if (wfEntity.Flag == false) //判断工作流是否已经启动了            {                wfEntity.Flag = true;                //发起流程                if (currentUser == "") currentUser = "DeaultUser";                if (strAssigner == "") strAssigner = "DeaultUser";                WFPCase WfProcess = new WFPCase(processKey, currentUser, strAssigner, formURL);                //启动流程                WfProcess.CreateAndRun();            }            else            {                if (currentUser == "" || currentUser == null) currentUser = "DeaultUser";                if (strAssigner == "" || strAssigner == null) strAssigner = "DeaultUser";                WFPCase WfProcess = new WFPCase(processKey, currentUser, strAssigner, formURL);                WfProcess.SetApproveInfo(strApprovalResult, strApprovalContent);                //WfProcess.InDic["IsApproved"] = "true";                //委托                WfProcess.InDic.Add("entity", wfEntity.Entity);                WfProcess.Handler = wfEntity.Handler;                //启动流程                WfProcess.RunInstance(keyId);            }        }
  这个就是目前工作流的与AOP的结合情况,只是个例子,就和这看吧。其实一点关系都没有,下面这一大堆的代码就是刚才说的,匹配到符合条件的方法之后,执行的内容。而最重要的是这个类要实现接口,IAfterRunningAdvice,实现之后就成了一个可以在匹配到方法之后执行的类。

  那么是用什么来进行拦截对象名方法名设置的呢?这个还是要靠配置文件。

<objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop">      <description>配置实现AOP</description>      <!--对象名称匹配-->      <object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">        <property name="ObjectNames">          <list>            <value>*Service</value>          </list>        </property>        <property name="InterceptorNames">          <list>            <value>afterAdvisor</value>          </list>        </property>      </object>      <!--拦截使用的方法-->      <object id="afterAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">        <property name="Advice" ref="afterAdvice"/>        <property name="MappedNames">          <list>            <!--方法名成的匹配-->            <value>*</value>          </list>        </property>      </object>      <!--加载  外部方法-->      <object id="afterAdvice" type="Common.AfterAdvice, Common"/>      <!--<object id="beforeAdvice" type="Common.BeforeAdvice, Common"/>-->      <!--配置 要拦截的类-->      <object id="categoryService" type="Service.ProductService, Service"/>      <object id="productService" type="Service.ProductService, Service"/>    </objects>
  应该能看到了吧,设置的部分都是“*字符串”这样就是所有带这个字符的都要拦截,和我们常用的搜索方式是一样的。

  没时间深究好多,只能浅尝辄止,顺便快来人阻止我胡说八道- -

0 0
原创粉丝点击