Spring.NET &构造函数拦截器 [英] Spring.NET & Constructor Interceptors

查看:36
本文介绍了Spring.NET &构造函数拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在构建时对对象进行一些 AOP,并找到了 IConstructorInterceptor,这对我想要的来说是完美的,但它 似乎不起作用(至少在 1.2 版中).

我还查看了 IObjectPostProcessor &IInstantiationAwareObjectPostProcessor,但我找不到任何方法在构造时对对象进行处理...... IInstantiationAwareObjectPostProcessor 上的 PostProcessPropertyValues 方法很接近,但它只通过 setter 注入的属性而不是构造函数的 arg 值......

具体来说,我想要的是在实例化之后对对象的引用,同时对注入构造函数的对象的引用.有什么办法可以做到这一点,或者我最好的选择只是切换到 setter 注入 &使用后处理器还是 MethodInterceptor?

解决方案

AOP 提出调用拦截,而不是依赖注入.使用 Spring.Net,您可以使用 seters 或构造函数来执行任何依赖注入.对于您的帖子和评论,它应该可以工作.

在 Spring.Net 初始化管道上,第一步分析依赖图,执行任何对象创建并设置所有依赖项.之后,如果您的类实现Spring.Objects.Factory.IInitializingObject",则方法void AfterPropertiesSet();"叫做.执行此操作的其他方法是使用您的对象配置,您可以在对象节点上设置 init-method.此行为基于 IoC/DI 概念.

也许你需要创建一个特定的工厂对象.要执行此操作,您的工厂必须实现 Spring.Objects.Factory.IFactoryObject 接口.这有助于容器为您委派对象创建(此配置上的属性和构造函数供您的工厂使用).

最好的问候

I'm trying to do some AOP over objects at construction time, and found IConstructorInterceptor, which would be perfect for what I want but it doesn't appear to work (in version 1.2 at least).

I've also looked at both the IObjectPostProcessor & the IInstantiationAwareObjectPostProcessor, but I can't find any way to do processing on an object around construction time... The PostProcessPropertyValues method on the IInstantiationAwareObjectPostProcessor is close, but it only passes through setter injected properties not the constructor arg values...

Specifically what I want is a reference to an object just after instantiation and at the same time, references to the objects that were injected into the constructor. Is there any way to do this, or is my best bet to just switch to setter injection & use the post processor or a MethodInterceptor?

解决方案

AOP proposes call interception, not dependency injection. Using Spring.Net you can use seters or constructors to perform any dependency injection. For your post and comments, it supposed to work.

On Spring.Net initialization pipeline, first step analyse dependency graph, perform any object creation and set all dependencies. After that, if your class implement "Spring.Objects.Factory.IInitializingObject", the method "void AfterPropertiesSet();" is called. Other way to perform that is using your object configuration, you may setting init-method on object node. This behavior is based in IoC/DI concepts.

Maybe you needs create a specific factory object. To perform that, your factory must implement Spring.Objects.Factory.IFactoryObject interface. That helps container to delegate for you the object creation (properties and constructors on this configuration are for your factory).

Best Regards

这篇关于Spring.NET &构造函数拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆