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

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

问题描述

我想在构建时对对象做一些AOP,并发现IConstructorInterceptor,这将是完美的我想要的,但它看起来不工作(至少在1.2版本中)。



我也看过IObjectPostProcessor& IInstantiationAwareObjectPostProcessor,但是我找不到任何方式来处理一个对象周围的构造时间...在IInstantiationAwareObjectPostProcessor的PostProcessPropertyValues方法是关闭,但它只通过setter注入属性,而不是构造函数arg值...

具体来说,我想要的是在实例化之后引用一个对象,同时引用被注入到构造函数中的对象。有没有办法做到这一点,或者是我最好的打赌,只是切换到setter注入&使用后处理器或MethodInterceptor?

解决方案

AOP建议调用拦截,而不是依赖注入。使用Spring.Net你可以使用设置器或构造函数来执行任何依赖注入。对于你的帖子和评论,它应该工作。



在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天全站免登陆