Announcement Announcement Module
Collapse
No announcement yet.
AOP dosn't work. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP dosn't work.

    Hi,all

    I am writing an aop example using spring 2.5 and AspectJ,I declared a pointcut to cover two methods in a busniness class(methodA and methodB, methodB is
    invoked in methodA). After I started my application, the after return advice for methodA run ok and and the same advice of methodB didnt work.

    I used two ways to test(Spring Aop and AspectJ), I noticed that if methodB was invoked in methodA within the same object instance, then I recode the project and let methodB was invoked in other instance. Now I can get the expected result.

    So my question is : anyone can explain why aop dosn't work if methodB is invoked in another method within a same instance? How can I let aop work with methodB?

    My spring app context and java code are shown as following:
    Spring app context:
    Code:
    	<context:component-scan base-package="com.jacky.menu.bus" />
    	
    	<!-- The first way using spring aop -->
    	<!-- Using spring aop -->
    	<aop:config>
    		<!-- pointcut declared with annotation @BusinessLogger -->
    		<aop:pointcut id="alterPoint"
    				expression="@annotation(com.jacky.menu.annotation.BusinessLogger)"/>
    		
    		<!-- After returning advisor -->
    		<aop:advisor advice-ref="businessAdvisor" id="afterBusAdvisor" pointcut-ref="alterPoint"/>
    	</aop:config>
    	
    	<!-- After returning advisor bean -->
    	<bean id="businessAdvisor" class="com.jacky.menu.aop.BusinessAdvisor"></bean>
    	
    	<!-- the second way using aspectJ -->
    	<!-- active aspectj autoproxy -->
    	<aop:aspectj-autoproxy />
    	
    	<!-- advisor using aspectJ -->
    	<bean class="com.jacky.menu.aop.AspectJTest" />
    Business Service:
    Code:
    	/* Method A
    	 * (non Javadoc)
    	 * 
    	 * @see com.jacky.menu.bus.IMenuManager#generateMenuTreeRoot()
    	 */
            @BusinessLogger("Generate Tree Root")
    	public Menu generateMenuTreeRoot(){
    		List<Menu> list = menuDao.getMenuList();
    		return this.generateTreeMenu(list);
    	}
    	
    	/**
             * Method B
    	 * @param menuList
    	 * 		a menu object list
    	 * @return
    	 * 		a root menu object with all the children menus in its children fields.
    	 */
            @BusinessLogger("Generate Tree Menu")
    	public Menu generateTreeMenu(List<Menu> menuList) {
    		Iterator<Menu> it = menuList.iterator();
    		if (!it.hasNext())
    			return null;
    		Menu root = (Menu) it.next();
    		while (it.hasNext()) {
    			Menu menu = (Menu) it.next();
    			addChild(menu, root);
    		}
    		return root;
    	}
    The first processing advice method using spring aop:
    Code:
    public class BusinessAdvisor implements AfterReturningAdvice {
    
    	/* (non-Javadoc)
    	 * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
    	 */
    	public void afterReturning(Object returnValue, Method method,
    			Object[] args, Object target) throws Throwable {
    		Method implMethod = target.getClass().getMethod(method.getName(),
    				method.getParameterTypes());
    		BusinessLogger annoLogger = implMethod
    				.getAnnotation(BusinessLogger.class);
    		String operation = annoLogger != null ? annoLogger.value() : "null";
    		StringBuilder tmp = new StringBuilder();
    		tmp.append(
    				operation).append(":").append(target.getClass().getName())
    				.append(".").append(method.getName());
    		System.out.println(tmp.toString());
    	}
    
    }
    The second processing advice method using aspectJ:
    Code:
    @Aspect
    public class AspectJTest {
    	
    	@AfterReturning("execution(* generateMenuTreeRoot(..)) || execution(* generateTreeMenu(..))")
    	public void afterReturning(){
    		System.out.println("Aop invoked!!!");
    	}
    }

  • #2
    All the answers are in the reference guide and are answered here about 1000 times.

    Spring uses proxy based AOP (chapter 6.6 of the ref guide) which means only calls INTO the object are intercepted as soons as you are in the object and call methods no more intercepting is taking place.

    If you want to you need to use load or compile time weaving.

    Comment

    Working...
    X