Announcement Announcement Module
Collapse
No announcement yet.
howto enable cglib Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • howto enable cglib

    Hallo,

    what should be changed in configuration for AspectJ Load-time Weaving to start using cglib proxy ?

    configuration
    http://www.springbyexample.org/examp...ng-config.html
    :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:load-time-weaver />
        
        <bean id="processor" class="org.springbyexample.aspectjLoadTimeWeaving.Processor" />
        
    </beans>
    Code:
    @Aspect
    public class PerformanceAdvice {
    
        @Pointcut("execution(public * org.springbyexample.aspectjLoadTimeWeaving..*.*(..))")
        public void aspectjLoadTimeWeavingExamples() {
        }
    
        @Around("aspectjLoadTimeWeavingExamples()")
        public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringType());
    
            StopWatch sw = new StopWatch(getClass().getSimpleName());
    
            try {
                sw.start(pjp.getSignature().getName());
    
                return pjp.proceed();
            } finally {
                sw.stop();
    
                logger.debug(sw.prettyPrint());
            }
        }
    
    }
    whole project is available at:
    http://www.springbyexample.org/examp...reference.html

    I would like to look at
    aspectj ltw,
    cglib proxy,
    and JDK dynamic proxy,

    but I don't know how to make spring use cglib proxy.

    I tried adding
    Code:
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    and/or
    Code:
    <aop:config proxy-target-class="true">
        <!-- other beans defined here... -->
    </aop:config>
    and als added CGLIB 2 binaries on my classpath
    as written on http://static.springsource.org/sprin...rence/aop.html

    but it didn't work

    thanks for help
    Last edited by Kazimierz Trzonek; May 5th, 2011, 09:17 AM.

  • #2
    You are very confused, so I'll try to explain it as clearly as I can...

    Spring AOP works by using proxies, and as such can be configured to use either JDK interface proxies OR CGLIB class proxies.

    BUT AspectJ is NOT Spring AOP! AspectJ is a different AOP framework, and it has a different way to work. Specifically, it doesn't use proxies to work. It weaves directly the aspects code into the advised classes, either at compile time (ctw, compile time weaving) or when the class is loaded by the class loader (ltw, load time weaving).

    So, you see, you can't configure AspectJ to use class proxies, simply because AspectJ doesn't use proxies at all!

    I hope I was clear enough, in any case I suggest a deeper reading of the Spring reference documentation: it's all explained in due detail.

    Comment


    • #3
      Thanks for Your reply.

      How should I configure PerformanceAdvice to be used as a cglib proxy?

      Comment


      • #4
        I really don't understand why you want to use class proxies so badly. First of all, you've already taken the trouble of configuring full-blown AspectJ with load time weaving, why going back to using Spring AOP?
        Second, ok, you've decided you want to use Spring AOP, but why not using default configuration and programming by interface so that Spring AOP will use JDK interface proxies? There is a reason why by default Spring AOP uses JDK interface proxies, and it has mainly to do with CGLIB's poor performance.
        In any case, if you really want to switch to Spring AOP with classproxies, here's what you need to do:

        1) remove <context:load-time-weaver /> and <bean id="processor" class="org.springbyexample.aspectjLoadTimeWeaving. Processor" />;

        2) remove every special configuration you did to enable AspectJ ltw (for example the java agent configuration that makes load time weaving possible);

        3) add <aop:aspectj-autoproxy proxy-target-class="true"/> to your configuration;

        4) now you're using Spring AOP so both the Aspect itself and all the classes that you want advised MUST be defined in the context as Spring beans (either with xml declaration, or annotations (@Resource, @Component, @Service etc.) + component-scanning of the appropriate packages;

        5) add cglib to your project libraries. If you're not using Maven to manage dependencies, remember to download the cglib-nodep jar, else you'll incur in NoClassDefFoundErrors(the classic jar hell).

        That's all, you are good to go. Remember however that Spring AOP is NOT AspectJ and it has some strong limitations:

        - non-Spring beans aren't advised;
        - only method execution pointcuts are accepted;
        - only external method calls (calls made from another class to the method that is to be advised) are intercepted, internal calls aren't;
        - static method execution obviously can't be advised;
        - same for non-public methods.

        Comment


        • #5
          I also thank you, it was useful.

          Comment

          Working...
          X