Results 1 to 4 of 4

Thread: Can't Get Aspect to Fire using AspectJ Annotations

  1. #1
    Join Date
    Apr 2011
    Location
    Flower Mound, TX
    Posts
    2

    Question Can't Get Aspect to Fire using AspectJ Annotations

    I'm new to Spring and AOP and I have worked through examples and tutorials but I can't get my own to work. I would appreciate any advice. Everything compiles and runs but it only executes the code in the method and does not execute the code in the aspect. Here is my code and config:

    Trans.java:
    Code:
    package example;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import static java.lang.annotation.ElementType.METHOD;
    
    @Target({METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Trans {
        String name() default "";
        String type() default "";
    }
    TransAspect.java:
    Code:
    package example;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    
    @Aspect
    public class TransAspect {
    
        @Around(value = "@annotation(trans)", argNames = "pjp, trans")
        public Object beginEndTransaction(ProceedingJoinPoint pjp, Trans trans) throws Throwable {
            System.out.println("*** BEGIN TRANS ***");
            if (trans.name() != null) System.out.println("*** name: " + trans.name() + " ***");
            if (trans.type() != null) System.out.println("*** type: " + trans.type() + " ***");
            try {
                return pjp.proceed();
            } finally {
                System.out.println("*** END TRANS ***");
            }
        }
    }
    SampleApp.java:
    Code:
    package example;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SampleApp {
    
        private static final String SPRING_APP_CTX = "springappctx.xml";
    
        public static void main(String[] args) throws Exception {
            ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_APP_CTX);
            SampleApp app = (SampleApp) ctx.getBean("sampleApp");
            app.doSomething();
        }
    
        @Trans
        private void doSomething() {
            System.out.println("*** DOING SOMETHING ***");
        }
    }
    springappctx.xml:
    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:aop="http://www.springframework.org/schema/aop"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
                                   http://www.springframework.org/schema/beans/spring-beans.xsd
                                   http://www.springframework.org/schema/aop
                                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        <aop:aspectj-autoproxy>
            <aop:include name="transAspect"/>
        </aop:aspectj-autoproxy>
    
        <bean id="transAspect" class="example.TransAspect"/>
       <bean id="sampleApp" class="example.SampleApp"/>
    
    </beans>
    This is the output:
    Code:
    Apr 15, 2011 11:15:06 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1027b4d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1027b4d]; startup date [Fri Apr 15 11:15:06 CDT 2011]; root of context hierarchy
    Apr 15, 2011 11:15:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [springappctx.xml]
    Apr 15, 2011 11:15:07 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1027b4d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@e2dae9
    Apr 15, 2011 11:15:07 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e2dae9: defining beans [org.springframework.aop.config.internalAutoProxyCreator,transAspect,sampleApp]; root of factory hierarchy
    *** DOING SOMETHING ***

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Spring AOP is PROXY BASED. The class that is to be advised is wrapped around a proxy object and calls made from the outside to a method of the class are made on the proxy instead, so that advice code can be applied. Thus, you can clearly see that one of the biggest limitation of Spring AOP is that only external method calls can be advised. In your case, you are not making an external call since the method is inside the same class that calls it, so a proxy-based kind of AOP like Spring AOP cannot work.
    I hope I made myself clear enough; you can refer to the official reference documentation where all this is explained in detail.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well actually it is an external method however the issue lies with the fact that it is a private method. Due to the java language and visibility rules the same class can call private methods on other instances as well.

    You can simply test this by making another class with a main which loads the xml and tries to call that method (it will fail to compile because the method cannot be called).

    So simply make it a public method and it should work.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Apr 2011
    Location
    Flower Mound, TX
    Posts
    2

    Default

    Thanks, Marten. Making the method public got it working.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •