Announcement Announcement Module
Collapse
No announcement yet.
Programmatic Creation of Proxy and Advice Ordering Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programmatic Creation of Proxy and Advice Ordering

    I've been experimenting with programmatic creation of proxied objects, with the use of AspectJProxyFactory.

    I've created a simple example where i have the following:

    A simple target class that looks like the following:
    Code:
    public class Target {
    	public String methodReturningString(){return "StringReturn";}
    }

    An interface defining a business method:
    Code:
    public interface AspectDefiningMethod {
    	String anotherMethodReturningString();
    }
    An interceptor of that business method:
    Code:
    @Aspect
    public class AspectJInterceptor{
    	
    	@Around("execution(* anotherMethodReturningString())")
    	public String pointCut(ProceedingJoinPoint joinPoint){
    		return "anotherMethodReturningString";
    	}
    }
    An interface defining additional values on a given target:
    Code:
    public interface AspectDefiningValues {
    
    	void setBooleanValue(Boolean boolean1);
    	
    	Boolean getBooleanValue();
    }
    And an implementation for that, plus an interceptor that should intercept all calls to a proxied object and change the values accordingly:

    Code:
    @Aspect
    public class AspectDefininingBooleanValueImpl implements AspectDefiningValues, Ordered {
    
    	@DeclareParents(value="com.foo..*", defaultImpl=AspectDefininingBooleanValueImpl.class)
    	AspectDefiningValues values;
    	
    	private Boolean booleanValue = Boolean.FALSE;
    	
    	@Override
    	public Boolean getBooleanValue() {
    	
    		return booleanValue;
    	}
    
    	@Override
    	public void setBooleanValue(Boolean boolean1) {
    		this.booleanValue = boolean1;
    
    	}
    	
    	@Around("execution(* anotherMethodReturning*())")
    	public void pointCut(ProceedingJoinPoint joinPoint) throws Throwable{
    		   ((AspectDefiningValues)joinPoint.getThis()).setBooleanValue(true);
    		joinPoint.proceed();
    	}
    
    	@Override
    	public int getOrder() {
    		return Ordered.HIGHEST_PRECEDENCE;
    	}
    
    }
    Then, I create my proxy like that:

    Code:
    public class Test {
    
    	@org.junit.Test
    	public void test(){
    		AspectJProxyFactory factory = new AspectJProxyFactory();
    		factory.setTarget(new Target());
    		factory.setOptimize(true);
    		factory.setProxyTargetClass(true);
    		factory.addInterface(AspectDefiningMethod.class);
    		Object target = factory.getProxy();
    
    		factory = new AspectJProxyFactory();
    		factory.setTarget(target);
    		factory.setOptimize(true);
    		factory.addAspect(AspectJInterceptor.class);
    		factory.addAspect(AspectDefininingBooleanValueImpl.class);
    
    
    		target = factory.getProxy();
    		
    		Assert.assertTrue(target instanceof Target);
    		Assert.assertTrue(target instanceof AspectDefiningMethod);
    		Assert.assertTrue(target instanceof AspectDefiningValues);
    		
    
    		Assert.assertEquals("anotherMethodReturningString", ((AspectDefiningMethod)target).anotherMethodReturningString());
    		Assert.assertTrue(((AspectDefiningValues)target).getBooleanValue());
    		
    	}
    Given the above, the expected behaviour would be that, in all circumstances, AspectDefininingBooleanValueImpl.pointCut() should kick in first, since it has Ordered.HIGHEST_PRECEDENCE. But, as i realized, the order is never respected.

    Indeed, after searching a bit, i found out that the chain creation for a given proxy does not take under consideration the ordering, as I've seen inside DefaultAdvisorChainFactory.getInterceptorsAndDynam icInterceptionAdvice().

    Is that the default expected behavior or am i missing something?

  • #2
    Anyone having any idea on the subject?

    Comment


    • #3
      hi,
      your aspects have to implement
      Code:
      org.springframework.core.Ordered
      if you want to define an order.

      Comment


      • #4
        Originally posted by shadowLaw View Post
        hi,
        your aspects have to implement
        Code:
        org.springframework.core.Ordered
        if you want to define an order.
        But I do.

        If you check AspectDefininingBooleanValueImpl definition you will see:
        Code:
        @Aspect
        public class AspectDefininingBooleanValueImpl implements AspectDefiningValues, Ordered
        

        And, further on in the same snippet
        Code:
        @Override
        	public int getOrder() {
        		return Ordered.HIGHEST_PRECEDENCE;
        	}
        Except if I've misunderstood something in the documentation, this should work. But, as i mentioned in my first post, the call that created the actual chain of execution for interceptors (namely DefaultAdvisorChainFactory.getInterceptorsAndDynam icInterceptionAdvice() ) does not take under consideration the ordering.

        Given that, there is no pointer in the documentation that Ordering should not work with programmatic creation of Proxies, either I have done something wrong or the implementation is behaving in a different than expected way.

        If the second is the case, maybe I should open a JIRA issue.

        Comment

        Working...
        X