Results 1 to 4 of 4

Thread: AspectJ LTW as maven Junit test works ; but not with spring ?!

  1. #1
    Join Date
    May 2012
    Posts
    3

    Default AspectJ LTW as maven Junit test works ; but not with spring ?!

    Hi,

    I have an @Aspect that runs if I use the -javaagent aspectjweaver.jar option to do the LTW.

    If I try to use the spring-instrumentation.jar the classes are not woven.

    This is my test context.

    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" 
           xmlns:aop="http://www.springframework.org/schema/aop" 
    
           xsi:schemaLocation="
    		http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    ">
    
        <context:load-time-weaver aspectj-weaving="on"/>
    method="aspectOf" /-->
        
    </beans>
    The aspect to access the fields
    Code:
    @Aspect
    public class UserEntityListener {
    
        private final static Logger LOG = LoggerFactory.getLogger(UserEntityListener.class);
     
        public UserEntityListener() {
        }
    
        @Pointcut("set(@com.example.springaspectjtest.auditable.AuditableProperty * *.*)")
        public void setField() {};
    
        @Before("setField()")
        public void setFieldImpl(JoinPoint jp) {
            final Object[] args = jp.getArgs();
            String newValue = args.length == 1 ? String.valueOf(args[0]) : Arrays.toString(args);
            final FieldSignature signature = (FieldSignature)jp.getSignature();
            try {
                final Field field = signature.getField();
                final boolean accessable = field.isAccessible();
                field.setAccessible(true); 
                try {
                    Object oldValue = signature.getField().get(jp.getTarget());
                    LOG.info(String.format("FIELD set : %s\n\t%s\n\tfrom %s\n\t\toldvalue %s\n\t\tnew value %s", jp.getTarget().toString(), jp.getSignature().toString(), jp.getThis().toString(), String.valueOf(oldValue), newValue));
                } finally {
                    field.setAccessible(accessable);
                }
                
            } catch (IllegalAccessException ex) {
                LOG.error("ERROR ", ex);
            }
        }
    
        public void reset() {
            //Dummy NoOp
        }
    
    }
    Is the problem the field access??
    any idea whats happening? I can priovide a smal maven project as zip file, if needed.


    Arne

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    There is no aspect defined so nothing to weave for spring.
    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

  3. #3
    Join Date
    May 2012
    Posts
    3

    Default

    Quote Originally Posted by Marten Deinum View Post
    There is no aspect defined so nothing to weave for spring.
    The aspects are defined in /META-INF/aop.xml - this is working for plain aspectj - ???

    It looks that the the weaver is doing anything, but actually no weaving at all?
    The classes woven with plain aspectj arent showing up at all in the logs.

    Arne
    Last edited by apl; May 11th, 2012 at 12:46 PM.

  4. #4
    Join Date
    May 2012
    Posts
    3

    Default

    The problem lies in the loading of classes for test runs....

    currently the TestClass UserEntityListenerTest gets loaded and with it the aspect(UserEntityListener) ant the class which has a maching Pointcut(User).
    Then the annotations otf the testclass get processed and the context get loaded.
    That too late for the aspect under test.

    If one avoids the declaration of fields of classes that schould be woven in all things run smoothley ( see code below with comments, what happend if ...)

    Code:
    package com.example.springaspectjtest;
    
    
    import com.example.springaspectjtest.aspects.UserEntityListener;
    import org.aspectj.lang.Aspects;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.test.annotation.DirtiesContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    
    /**
     *
     * @author aploese
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS)
    public class UserEntityListenerTest {
        
        private final static Logger LOG = LoggerFactory.getLogger(UserEntityListenerTest.class);
        //Uncommenting this and the User classs gets loaded before >>> No aspects are woven into this class.
        //private User user;
        //Uncommenting this and the UserEntityListener class gets loaded before <<<  This will never become an aspect and thus aspectOf will fail.
        //private UserEntityListener userEntityListener;
        
        @Test
        public void testUserEntityListener() {
            LOG.info("run test testUserEntityListener");
            UserEntityListener uel = Aspects.aspectOf(UserEntityListener.class);
            User d = new User();
            d.setName("Hello");
        }
    
    }
    This makes the use of setUp/tearDown a bit harder, but it schould work.

    Arne
    Last edited by apl; May 15th, 2012 at 01:43 AM.

Posting Permissions

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