Spring.NET & Constructor Interceptors

两盒软妹~` 提交于 2019-12-12 16:04:20

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/728533/spring-net-constructor-interceptors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!