Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: aspectj-autoproxy does not work with aspectj compiler

  1. #1

    Question 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
    Attached Files Attached Files
    Last edited by DebugMeNot; May 10th, 2012 at 01:55 PM.

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

    Default

    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).
    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

    Question factory-method="aspectOf" doesn't solve the problem for me

    Quote 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 at 01:20 PM.

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

    Default

    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!!!
    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

  5. #5

    Question

    Quote 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?

  6. #6
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

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

  7. #7

    Question 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

  8. #8
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote 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.

  9. #9

    Unhappy

    Quote 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.

  10. #10
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote 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.

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
  •