在 Spring AOP 的服务中注入相同服务实例的最佳方法是什么 [英] Whats the best way to inject same instance of service in service for Spring AOP
问题描述
我有一个 ServiceImpl,其中使用 Spring 的 @Service 构造型进行注释,其中有两个方法,每个方法都使用 Spring 拦截的自定义注释进行注释.
I'va a ServiceImpl with is annotated with @Service stereotype of Spring and have two methods in it each one is annotated with custom annotations which are intercepted by Spring.
@Service
public class ServiceImpl implements Service{
@CustomAnnotation
public void method1(){
...
}
@AnotherCustomAnnotation
public void method2(){
this.method1();
...
}
}
}
现在 Spring 使用基于代理的 AOP 方法,因此当我使用 this.method1()
拦截器 @CustomAnnotation 将无法拦截这个调用,我们曾经在另一个 FactoryClass 中注入这个服务这样我们就能够获得代理实例,例如 -
Now Spring uses proxy based AOP approach and hence as I'm using this.method1()
interceptor for @CustomAnnotation will not able to intercept this call, We used to inject this service in another FactoryClass and in that way we were able to get the proxy instance like -
@AnotherCustomAnnotation
public void method2(){
someFactory.getService().method1();
...
}
我现在用的是Spring 3.0.x,哪个是获取代理实例的最佳方式?
I'm now using Spring 3.0.x, which is the best way to get the proxy instance?
推荐答案
另一种选择是使用 AspectJ 和 @Configurable.春天似乎正在走向这些天(有利于).
The other alternative is to use AspectJ and @Configurable. Spring seems to be going towards these days (favoring).
如果您使用 Spring 3,我会研究它,因为它比基于代理的 aop 更快(性能)和更灵活.
I would look into it if you are using Spring 3 as it is faster (performance) and more flexible than proxy based aop.
这篇关于在 Spring AOP 的服务中注入相同服务实例的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!