Announcement Announcement Module
No announcement yet.
Strange issue with AOP and autowired classes Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange issue with AOP and autowired classes

    I am looking at a application where I see the below issues
    - I am trying to add a simple interceptor as mentioned on Spring website

    <!-- this is the aspect -->
        <bean id="profiler" class="x.y.SimpleProfiler">
            <!-- execute before the transactional advice (hence the lower order number) -->
            <property name="order" value="1"/>
        <tx:annotation-driven transaction-manager="txManager" order="200"/>
            <!-- this advice will execute around the transactional advice -->
            <aop:aspect id="profilingAspect" ref="profiler">
                <aop:pointcut id="serviceMethodWithReturnValue"
                              expression="execution(!void x.y..*Service.*(..))"/>
                <aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/>
    In the service, I am trying to add advice(XYZService) to, there is a dependency which has @Autowired annotation
        private ABFactory abFactory;
    The above mentioned ABFactory is a class and not a interface.( This is existing code and I am not sure why the autowiring has been done to class and not interface).

    When i try running test class, get following error
    Caused by: org.springframework.aop.framework.AopConfigExcepti on: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

    I do not want to add CGLIb proxies as it is better to use JDK dynamic proxies.

    If I remove the custom interceptor and leave the "<tx:annotation-driven transaction-manager.." advice it works. Why is the issue with CGLib proxies only happening when I try the <aop:config> for custom advice. Should the behavior not be same for the transaction advice as well?

  • #2
    Is your class ABFactory implementing any interfaces for spring to proxy using Jdk proxies? If not spring will require CGLIB to proxy your class.
    Last edited by amiladomingo; May 14th, 2013, 06:02 AM.


    • #3
      Thanks for the reply. The class ABFactory is not implementing any interfaces. But then when I use only the
      <tx:annotation-driven transaction-manager="txManager" order="200"/>
      And comment out the new <aop:config> for "profilingAspect", then it works fine. How is the ABFactory class being proxied in that case without CGLIB.

      The methods in my service which make calls to ABFActory do not have the @Transactional annotation. ABFactory is a class with a getInstance method.


      • #4
        In Spring, you should always use the interface to create instance variable and do dependency injection (using autowired or xml) and not the implementation class as Spring AOP creates dynamic proxy object while applying any kind of "advice" (including @Transactional, @Secured, etc).

        If you remove "Spring AOP Advice" configuration, it will work fine as no need to create any Spring Proxy or Dynamic Proxy.

        If you do not have "Interface" and wanted to work with directly implementation, then you MUST need to have CGLIB or Spring AOP will not work.