我的一个ServiceImpl用Spring的@Service构造型进行了注解,其中有两个方法,每个方法都用自定义注解注解,由Spring截获。
@Service
public class ServiceImpl implements Service{
@CustomAnnotation
public void method1(){
...
}
@AnotherCustomAnnotation
public void method2(){
this.method1();
...
}
}
}
现在Spring使用基于代理的面向方面编程方法,因此当我使用@CustomAnnotation的this.method1()
拦截器时,将无法拦截此调用,我们曾在另一个FactoryClass中注入此服务,通过这种方式,我们能够获得代理实例,如-
@AnotherCustomAnnotation
public void method2(){
someFactory.getService().method1();
...
}
我现在使用的是Spring3.0.x,获取代理实例的最佳方式是什么?
发布于 2011-02-23 21:21:22
另一种选择是使用AspectJ和@Configurable。这几天春天似乎要来了。
如果你使用Spring3,我会研究一下它,因为它比基于代理的aop更快(性能)和更灵活。
发布于 2011-02-23 21:06:05
多亏了提供的bean工厂,您可以让ServiceImpl类实现BeanFactoryAware接口,并查找自身。但这不再是依赖注入了。
最好的解决方案是将method1放在另一个服务bean中,该服务bean将被注入到现有的服务bean中,并且将委托给现有的服务bean。
https://stackoverflow.com/questions/5091187
复制相似问题