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

  • howto enable cglib


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


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <context:load-time-weaver />
        <bean id="processor" class="org.springbyexample.aspectjLoadTimeWeaving.Processor" />
    public class PerformanceAdvice {
        @Pointcut("execution(public * org.springbyexample.aspectjLoadTimeWeaving..*.*(..))")
        public void aspectjLoadTimeWeavingExamples() {
        public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringType());
            StopWatch sw = new StopWatch(getClass().getSimpleName());
            try {
                return pjp.proceed();
            } finally {
    whole project is available at:

    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
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <aop:config proxy-target-class="true">
        <!-- other beans defined here... -->
    and als added CGLIB 2 binaries on my classpath
    as written on

    but it didn't work

    thanks for help
    Last edited by Kazimierz Trzonek; May 5th, 2011, 08: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.


    • #3
      Thanks for Your reply.

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


      • #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.


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