什么是为Spring AOP注入相同服务实例的最佳方式 [英] Whats the best way to inject same instance of service in service for Spring AOP

查看:121
本文介绍了什么是为Spring AOP注入相同服务实例的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个使用Spring的@Service构造型注释的ServiceImpl,并且有两个方法,每个方法都使用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屋!

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