荣耀彩票代理

IT技术互动交流平台

AOP面向切面编程简介

发布日期:2016-12-22 20:36:26

荣耀彩票代理AOPZHEIGECIXIANGXINDAJIADOUMEIYOUJIECHUTAIDUOGUO,DANSHISHIJISHANGNIMENYIJINGYOUSUOJIECHULE,JIUZAISHEJIMOSHIZHONG。AOPSUOYONGDESIXIANGQISHIHESHEJIMOSHISHIYIYANGDE,JIZAIBUXIUGAIYUANDAIMADEQINGKUANGXIATONGYIZENGJIAHUOZHEXIUGAIGONGNENG。HAIYOU,AOPDADUOYONGZAIspringLIMIAN,DANSHIBENWENSUOXIEDEZHISHIZAIMVCZHONGDEYINGYONG,YAOZHUYI。


一、简介

SUOWEIAOP(Aspect Oriented ProgrammingDESUOXIE)YIWEI

MIANXIANGQIEMIANDEBIANCHENG,TONGGUOYUBIANYIFANGSHIHEYUNXINGQIDONGTAIDAILISHIXIANCHENGXUGONGNENGDETONGYIWEIHUDEYIZHONGJISHU。AOPSHIOOPDEYANXU,SHIRUANJIANKAIFAZHONGDEYIGEREDIAN,YESHISpringKUANGJIAZHONGDEYIGEZHONGYAONEIRONG,SHIHANSHUSHIBIANCHENGDEYIZHONGYANSHENGFANXING。LIYONGAOPKEYIDUIYEWULUOJIDEGEGEBUFENJINXINGGELI,CONGERSHIDEYEWULUOJIGEBUFENZHIJIANDEOUHEDUJIANGDI,TIGAOCHENGXUDEKEZHONGYONGXING,TONGSHITIGAOLEKAIFADEXIAOLV。

YISHANGSHIBAIDUBAIKEDEGUANFANGJIESHI,DANSHIZAIWOKANLAI,QISHIAOPGENGDUODESHIYIZHONGSIXIANG,YIZHONGKEYIDONGYIFAERTONGQUANSHENDESIXIANG,AOPQISHIMIANDUIDEGENGDUOSHIYIZHONGTONGYIDEGONGNENGHUOZHEDIAOYONGGUOCHENGLAIJINXINGBIANXIEDEYIZHONGCHENGXUTI,CONGTADEDIYIGECIAspect(QIEMIAN)YEJIUSHIZHIDEYIGEFANGMIAN,YEKEYILEJIEDAOZHEIZHONGFANGSHIYEJIUSHIZHENDUIYIGEFANGMIANDESHIXIAN,ZHEIYIDIANQISHIHEmvcZHONGDEglobalWENJIANHENXIANG,YESHISpringKUANGJIAZHONGDEYIGEZHONGYAONEIRONG,SHIHANSHUSHIBIANCHENGDEYIZHONGYANSHENGFANXING。LIYONGAOPKEYIDUIYEWULUOJIDEGEGEBUFENJINXINGGELI,CONGERSHIDEYEWULUOJIGEBUFENZHIJIANDEOUHEDUJIANGDI,TIGAOCHENGXUDEKEZHONGYONGXING,TONGSHITIGAOLEKAIFADEXIAOLV。

QICIJIUSHITADEYUBIANYIXINGZHIKEYIHENHAODECHULIYIXIECUOWUHUOZHESHIYUXIANPANDUANYIXIEQINGKUANG,ZHEIYEDAOZHILETAZAISHEJIDESHIHOUZAIPANDUANQUANXIAN,TONGYISHUCHUMOUGEDONGXIFANGMIANBIANDEBIJIAOCHANGJIAN。“KEYITONGGUOYUBIANYIFANGSHIHEYUNXINGQIDONGTAIDAILISHIXIANZAIBUXIUGAIYUANDAIMADEQINGKUANGXIAJICHENGXUDONGTAITONGYITIANJIAGONGNENGDEYIZHONGJISHU。AOPSHIJISHIGoFSHEJIMOSHIDEYANXU,SHEJIMOSHIZIZIBUJUANZHUIQIUDESHIDIAOYONGZHEHEBEIDIAOYONGZHEZHIJIANDEJIEOU,TIGAODAIMADELINGHUOXINGHEKEKUOZHANXING,AOPKEYISHUOYESHIZHEIZHONGMUBIAODEYIZHONGSHIXIAN。”SHANGMIANZHEIJUHUAYESHIDUIYUAOPDEHENHAODEQUANSHI。

二、在MVC中的实现

SHUOLEZHEIMEDUOLE,RANGWOMENLAISHIZHANYIXIABA,SHOUXIANWOMENXUYAOZAIvsXINJIANYIGEmvcDEXIANGMU,XUANZEMVC3HUOZHESHI4DOUOK,RANHOUJIANLIYIGEcontroller,MINGMINGSUIYI,RANHOUJIANLITADESHITU,ZAISHITUSHANGXIESHANGZUIJIANDANDEHelloWorld。

 

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <a>Hello! World</a>
    </div>
</body>
</html>


 

YOUTUSHIWOCHUANGJIANHAODEMVCJIEJUEFANGANHETIANJIAHAODEcontrollerHAIYOUview,ZAIviewZHONGXIERUSHANGMIANDEDAIMAYIXIANSHIHello!World。

荣耀彩票代理YUNXINGZHIHOU(BUHUIYUNXINGDEZIXINGBAIDU)SHIBUSHIJIUXIANSHICHULEYIGEHelloWorldDEZIYANGZAIWANGYESHANGNI?

HAO,JIEXIALAIWOMENKAISHIXINJIANAOPDEYIGEWENJIANRANHOUQUSHIYONG,SHOUXIANZAIBENXIANGMUZHONGXINTIANJIAYIGELEI,MINGZIJIAOFilterPublic,ZAIYINYONGDEDIFANGTIANJIAusing System.Web.MvcZHEIGEYINYONG,RANHOURANGZHEIGELEIJICHENGYUActionFilterAttribute,DAJIAYAOZHUYIActionZHEIGECI,SHUOMINGZHEIGEDONGXISHIJIYUDONGZUODE。

RANHOUWOMENXIERUYIXIADAIMA:

 

        public string Message { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            filterContext.HttpContext.Response.Write("Action执行之前" + Message + "<br />");
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            filterContext.HttpContext.Response.Write("Action执行之后" + Message + "<br />");
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
            filterContext.HttpContext.Response.Write("返回Result之前" + Message + "<br />");
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
            filterContext.HttpContext.Response.Write("返回Result之后" + Message + "<br />");
        }

这个里面就是Filter触发的各种动作,然后我们在controller里面的默认方法做以下修改:

 

 

        [FilterPublic(Message = "Action")]
        public ActionResult Index()
        {
            HttpContext.Response.Write("Action正在执行···<br />");
            return Content("正在返回Result···<br />");
        }

然后在controller的class类上面也添加一句[FilterPublic(Message = "Controller")],然后运行,会出现什么结果呢?

 

 

Action执行之前Action
Action正在执行···
Action执行之后Action
返回Result之前Action
正在返回Result···
返回Result之后Action

 

可以看到controller里面的方法在执行之前会去先执行以下我们设置的FilterPublic里面的代码,而且随着里面动作发生时间不同也可以看出了都是哪个方法被触发了。

荣耀彩票代理DANSHIZAIcontrollerSHANGMIANXIEDEJIYUcontrollerDEFANGFAQUEMEIYOUCHUFA,ZHEISHIWEISHENMENI?

QISHIZHEIGEWENTIHENJIANDAN,JIUSHIWOMENZAISHEZHIWOMENDEAOPCHENGXUDESHIHOUMEIYOUJINXINGCANSHUDESHEZHI,MEIYOURANGfilterJINXINGDIEJIADEYUNXING,ZHEIGESHIHOUWOMENZHIYAOZAIXIEDEFilterPublicLEIDESHANGMIANJIASHANG:[AttributeUsage(AttributeTargets.All, AllowMultiple = true)],JIUKEYIRANGNISHEZHIDEGEZHONGfilterHUOZHEDIEJIADEfilterJINXINGCHUFALE,ZHEIGESHIHOUWOMENZAIYUNXINGYIXIASHISHI:

 

Action执行之前Controller
Action执行之前Action
Action正在执行···
Action执行之后Action
Action执行之后Controller

返回Result之前Controller
返回Result之前Action
正在返回Result···
返回Result之后Action
返回Result之后Controller

这样一来,在controller上的Filter也被触发了,所以我们使用AOP的方法只需要在我们自己的写的方法或者页面默认的加载方法上面写上一个filter类的标签就可以了。

 

NEIME,RUGUOWOMENYOUGEGONGNENGXUYAOSUOYOUDEYEMIANDOUCHUFA,ZHEIYANGQIBUSHIYEHENMAFANME?BIEDANXIN,ZHEIGESHIHOUJIULUNDAOWOMENDEglobalWENJIANFAHUIZUOYONGLE。ZAIglobal.asaxWENJIANXIAMIANDERegisterGlobalFiltersFANGFAXIAMIANZHUCEZIJIXIEDEFilterJIUKEYILE:

 

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    //把自己的filter定义成全局的filter
    filters.Add(new FilterPublic() { Message = "全局<br/>" });

}

然后运行,看结果:

 

 

Action执行之前全局

Action执行之前Controller
Action执行之前Action
Action正在执行···
Action执行之后Action
Action执行之后Controller
Action执行之后全局

返回Result之前全局

返回Result之前Controller
返回Result之前Action
正在返回Result···
返回Result之后Action
返回Result之后Controller
返回Result之后全局

这样全局的触发也就做好了(filter中的message只是用于标识层级的,正式用的时候可以不用定义。)

 

ZHEIYANGYIKAN,RUGUOXIANGYAOKUAISUDETIANJIAYIGEQUANJUDOUYONGDEFANGFA,ZHIXUYAOXINJIANYIGEFilter,RANHOUZAIGAIYIXIAglobalWENJIANJIUKEYIBANDAOLE,SHIBUSHIHENFANGBIANNI。


以上均为本人对于AOP的一些浅浅的研究,如有错误的地方还请读者指正。

Tag标签:      
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规