How can I deploy the same service instance in a service for Spring AOP

I'va ServiceImpl is annotated with the @Service Spring stereotype and has two methods: each of them is annotated with custom annotations that are intercepted by Spring.

@Service public class ServiceImpl implements Service{ @CustomAnnotation public void method1(){ ... } @AnotherCustomAnnotation public void method2(){ this.method1(); ... } } } 

Now Spring uses the proxy-based AOP approach, and therefore, since I use this.method1() interceptor for @CustomAnnotation, it will not be able to intercept this call, we used this service in another FactoryClass, and thus we were able to get the proxy instance , eg -

  @AnotherCustomAnnotation public void method2(){ someFactory.getService().method1(); ... } 

Now I am using Spring 3.0.x, what is the best way to get a proxy server instance?

+3
java spring spring-aop aop
Feb 23 '11 at 12:53
source share
3 answers

Another alternative is to use AspectJ and @Configurable. Spring seems to be coming these days (in favor).

I would look at it if you are using Spring 3, since it is faster (performance) and more flexible than proop based aop.

+3
Feb 23 '11 at 13:21
source share

Both methods are inside the same proxy server, while the AOP function simply enriches calls from the outside (see Understanding AOP Proxies ). There are three ways to deal with this limitation:

  • Change your design (what would I recommend)
  • Change proxy type from JDK proxy to proxy target class (subclasses based on CGLib) No, this will not help, see @axtavt comment, this should be a static AspectJ assembly.
  • Use ((Service)AopContext.currentProxy()).method1() (it works, but it's a terrible violation of AOP, see the end Understanding AOP Proxies )
+1
Feb 23 2018-11-23T00:
source share

You can make your ServiceImpl class a BeanFactoryAware interface and look for it thanks to the provided bean factory. But this is no longer an injection of dependencies.

The best solution is to put method1 in another bean service that will be injected into your existing bean service and to which your existing bean service will be transferred.

0
Feb 23 2018-11-23T00:
source share



All Articles