Announcement Announcement Module
Collapse
No announcement yet.
aspectj-autoproxy does not work with aspectj compiler Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • aspectj-autoproxy does not work with aspectj compiler

    Hello,

    I use an aspect that was compiled by aspect-j compiler
    however I want to use aspectj-autoproxy in same project
    I noticed is that when aspect is compiled with aspectj compiler the aspectj-autoproxy stops working.

    Here is the code:

    Code:
    /* Foo.java */Divorce Attorney Denver CO
    Thailand holiday packages
    public interface Foo {
    	void foo();home security services
    estojo original rolex
    }
    
    /* FooImpl.java */
    public class FooImpl implements Foo {
    	public void foo() {
    	}
    }
    
    
    /* FooAspect.java */
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    @Aspect
    public class FooAspect {
    	@Around("execution(void *.foo())")
    	public Object throwException(ProceedingJoinPoint pjp) {
    		throw new RuntimeException();
    	}
    }
    
    /** FooTest.java */
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "/application-context.xml")
    public class FooTest {
    	@Autowired
    	Foo foo;
    	@Test
    	public void undeclaredCheckedExceotionIsThrown() {
    		try {
    			foo.foo();
    			Assert.fail();
    		} catch (RuntimeException e) {
    			//that's what we expect to happen
    		}
    	}
    }
    /* application-context.xml */
    <?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" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    	<aop:aspectj-autoproxy />
    
    	<bean id="foo" class="FooImpl" />
    
    	<bean id="fooAspect" class="FooAspect"  />
    	
    </beans>
    Why aspectj-autoproxy doesn't work when aspect was compiled with aspectj and how to fix it?

    Maven project with code attached.

    Thanks In Advancebreast cancer treatment
    chaga mushroom
    Last edited by DebugMeNot; May 10th, 2012, 02:55 PM.

  • #2
    If you use the AspectJ compiler to create/compile an Aspect (and not use compile time weaving to already apply it) you will have to use the aspectOf factory method on your aspect to declare it an aspect. Why not simply use the normal class compiler (it still is a simple class).

    Comment


    • #3
      factory-method=&quot;aspectOf&quot; doesn't solve the problem for me

      Originally posted by Marten Deinum View Post
      If you use the AspectJ compiler to create/compile an Aspect (and not use compile time weaving to already apply it) you will have to use the aspectOf factory method on your aspect to declare it an aspect. Why not simply use the normal class compiler (it still is a simple class).
      I tried to use
      Code:
      <bean id="fooAspect" class="FooAspect" factory-method="aspectOf"  />
      but no luck.
      Springs doesn't apply the aspect.

      BTW, why should I use aspectOf in this case?

      Thanks in Advance.Toronto Homes For Sale
      los angeles reverse mortgage
      Last edited by DebugMeNot; May 1st, 2012, 02:20 PM.

      Comment


      • #4
        Because now it is an aspect not a simple class... Just one question why the need for the AspectJ compiler? Do you want to use compile time weaving? If not again the question why add this extra dependency and complexity to your build!!!

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          Because now it is an aspect not a simple class... Just one question why the need for the AspectJ compiler? Do you want to use compile time weaving? If not again the question why add this extra dependency and complexity to your build!!!
          I'm developing an aspect library.
          I want to make sure that aspect works for AspectJ as well as for Spring AOP.
          I want to do it in one build ( i use maven for that).

          The problem is that when AspectJ compiles an aspect Spring AOP stops using it and I do not understand why.
          Is that so bad that a class was compiled with aspectj compiler?

          Comment


          • #6
            I found your problem rather interesting and investigated it a little. The results are available here.

            Comment


            • #7
              at least the cause was found... now searching for the solution.

              Thank you very much for your research.
              In simple words spring can not work with such aspects.
              I wonder what a person that writes an library of aspects for common usage is suppose to do?
              Let's hope not to create an aspect for each framework. ;-)

              You mentioned some workaround, can you please share your ideas?

              Thanks In Advance

              Comment


              • #8
                Originally posted by DebugMeNot View Post
                Thank you very much for your research.
                In simple words spring can not work with such aspects.
                I wonder what a person that writes an library of aspects for common usage is suppose to do?
                Let's hope not to create an aspect for each framework. ;-)

                You mentioned some workaround, can you please share your ideas?

                Thanks In Advance
                Welcome

                First of all, I don't quite understand what are you trying to achieve
                I.e. I see the approach you use to do something (compile @AspectJ-configured class by aspectj compiler and use that class as an aspect at spring aop) but target aim of that activity is not clear.

                About workarounds - you can use aspectj weaving and don't use spring aop at all for example.

                Comment


                • #9
                  Originally posted by denis.zhdanov View Post
                  First of all, I don't quite understand what are you trying to achieve
                  I'm writing an aspect library that people will be able to use with AspectJ and Spring AOP.
                  I want to create one aspect which will be possible to use with both frameworks.

                  PS It seems I'll have to create an aspect for each of AOP frameworks.

                  Comment


                  • #10
                    Originally posted by DebugMeNot View Post
                    I'm writing an aspect library that people will be able to use with AspectJ and Spring AOP.
                    I want to create one aspect which will be possible to use with both frameworks.

                    PS It seems I'll have to create an aspect for each of AOP frameworks.
                    Indeed. We just found out that it's not possible to reuse aspectj aspect with spring aop.

                    Comment


                    • #11
                      Just thought that you can do the following trick - write all spects in @AspectJ way and distribute your library at the following assemblies:
                      • compiled by aspectj compiler;
                      • compiled by javac compiler;
                      • as a source code collection;

                      So, experienced users just can grab the sources and use them as appropriate, all others can use precompiled binaries.

                      Comment

                      Working...
                      X