惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
腾讯CDC
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
NISL@THU
NISL@THU
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
T
Threatpost
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
I
Intezer
C
Check Point Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
Security Latest
Security Latest
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
云风的 BLOG
云风的 BLOG
量子位
T
Tor Project blog
博客园 - 叶小钗
The Cloudflare Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
Tailwind CSS Blog
W
WeLiveSecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
罗磊的独立博客
Know Your Adversary
Know Your Adversary
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
Help Net Security
Help Net Security
美团技术团队
P
Privacy International News Feed
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 抽烟的猫

学习开发webpart MS SQL开发经典 听许巍 年终总结里的建议 实验部分类的代码 在蹉跎中一路前行(转自Eric Liu(刘如鸿)) javascript小技巧 面向对象的Jscript---转自经典论坛中桃花岛主的文章 在Javascript中使用面向对象的编程(转载) JavaScript中的类继承(翻译 ShiningRay)转自nirvanastudio Ioc模式(又称DI:Dependency Injection)(转载) JBoss 5迎来中间件彻底的可配置时代(转载) Spring AOP(转载) [心情驿站]喝一碗孟婆汤,走一遍奈何桥(flash版)(转载) AOP与权限控制实现(转自板桥里人) AOP和AspectJ(转自板桥里人) AOP(转自板桥里人) 今天你多态了吗? 如何在WebPart中访问页面上的其他WebPart (转kaneboy)
Castle实践8-AspectSharp (转自叶子)
抽烟的猫 · 2005-12-02 · via 博客园 - 抽烟的猫

Posted on 2005-08-21 00:30 绿叶 阅读(1441) 评论(5)  编辑 收藏 收藏至365Key

        由于我最近参加雅思培训,一天到晚都在上课,所以一直没时间出来写blog。今天星期六我把aop资料整理了,做了例子,现在放上来,希望堆大家有帮助。
        AOP是基于动态代理技术的。在学习AOP之前必须明白几个概念,这几个概念我将由配置AspectSharp(以下简称A#)的配置文件中一一引出,包括:Advice、Pointcut、Advister、Mixin  。
        A#有自己独特的配置语言,当然也支持XML,但是新的配置语言我觉得比XML更加明了直观,而且也是非常容易使用的。 详细的官方文档在这里:http://www.castleproject.org/index.php/AspectSharp_Language_Documentation

1)配置必须按照以下顺序:

[Imports]

[Global Interceptor map]

[Global Mixin map]

Aspects definitions


       

2)[Imports]:引入命名空间,在下面的配置中用到的拦截器、混淆器所需要的。

Import Namespace.Name [in AssemblyName]

3)[Global Interceptor map]:如果你想在程序共享同一个拦截器而不想重复声明可以将Interceptor声明为全局,在同一配置文件中重用,而不用再次打长长的名称,用声明的别名就可以了。

interceptors [ 
  
"key" : InterceptorType ; 
  
"key2" : InterceptorType2 
]

4)[Global Mixin map] : 同样混淆器也可以声明为全局。

mixins [ 
  
"key" : MixinType ; 
  
"key2" : MixinType2 
]

5)Aspects definitions : 具体定义一个“切面(需要拦截的地方)”

aspect Name for Type 
  [include]
  [pointcuts]
end

6)[include] :定义混淆器(mixin)组合的类

aspect MyAspect for Customer
  include DigitalGravity.Mixins.Security in DigitalGravity.XProject
  include System.Collections.ArrayList in System
end

7)[pointcuts] :拦截的具体名称,这里先指定拦截的类型并可以用通配符匹配名称。类型如下:

  • method :拦截方法的名称
  • property :拦截的属性名称
  • propertyread :拦截的读属性的名称
  • propertywrite:拦截的写属性的名称


    8)Advices :指定由哪个拦截器拦截


            AOP-面向方面编程,定义这里我不想介绍太多,如果有兴趣的话,你可以来:http://www.wjshome.com/bbs/board.aspx?boardid=14看看aop的资料。那么AOP主要应用在哪些方面呢?具体来说,有:
    Authentication--权限、Caching--缓存、Context passing--内容传递、Error handling--错误处理、Lazy loading--延迟加载、Debugging--调试、logging tracing profiling and monitoring--记录跟踪 优化 校准、Performance optimization--性能优化、Persistence--持久化、Resource pooling--资源池、Synchronization--同步、Transactions--事务……。
            A#是CastleProject在.Net上实现AOP的一个框架,另外Spring.Net也有AOP的咚咚。 其实在Java里面,aop已经是很久以前的话题了,只是。net方面资料不如java多,用的人也相对少了。我下面将以一个简单的例子(参照官方改写),阐述A#在.net下面实现简单的“日志”和“权限检查”的AOP编程。

    1)配置文件:

    import AopDemo.Interceptors
    import AopDemo.Mixins

    aspect 

    log for [AopDemo]
        pointcut method(
    *)
            advice(LoggerInterceptor)
        
    end
    end

    aspect Security 

    for [AopDemo]
        include SecurityMixin
        pointcut method(
    *)
            advice(SecurityCheckInterceptor)
        
    end
    end

    最上面两个import是引入命名空间,是LoggerInterceptor、SecurityCheckInterceptor和SecurityMixin的。我发现如果在同一个程序集中,不使用import也行,但是如果你的interceptor是单独的dll的话,应该必须import,我没有尝试。把interceptor独立出来的好处就是可以很方便的替换他。
    接着是两个“切面”定义,第一个是定义所有的方法都会被LoggerInterceptor拦截,用于记录日志。上面讲到可以利用通配符来筛选拦截的方法名称,所以“*”就是代表所有的方法,又比如说:pointcut void Create(*)就是拦截返回类型为void,参数不限制的Create方法,void create(int id), void create(string name)这两个方法都会被拦截。第二个pointcut是加入了一个权限混淆器SecurityMixin,他的所有方法调用会被SecurityCheckInterceptor拦截。SecurityMixin的代码如下:

    public class SecurityMixin : ISecurity, IProxyAware
    {
        
    private object proxy;public SecurityMixin()
        {
    }#region ISecurity 成员public bool Access
        {
            
    get
            {
                
    // use proxy to do sth.
                
    // eg: if (proxy is PersonDao)

                
    return true;
            }
        }
    #endregion#region IProxyAware 成员public void SetProxy(object proxy)
        {
            
    this.proxy = proxy;
        }
    #endregion
    }

    他实现了两个接口:ISecurity和IProxyAware。
    ISecurity有一个Access属性,而实现IProxyAware是为了在mixin中使用代理对象。这样定义之后,在SecurityCheckInterceptor拦截的代理对象就是实现了ISecurity接口的对象,可以调用ISecurity接口中定义的方法了,而原来的对象是没有实现这个接口的。

    使用的时候先加载配置初始化AspectEngine:

    AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder(File.OpenText(@"../../aspectsharp.cfg"));
    AspectEngine engine 
    = builder.Build();

    然后通过这样来使用:

    PersonDao dao = engine.WrapClass(typeof(PersonDao)) as PersonDao;
    dao.Create(
    "CCC");