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

  • AOP dosn't work.


    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:
    	<context:component-scan base-package="" />
    	<!-- The first way using spring aop -->
    	<!-- Using spring aop -->
    		<!-- pointcut declared with annotation @BusinessLogger -->
    		<aop:pointcut id="alterPoint"
    		<!-- After returning advisor -->
    		<aop:advisor advice-ref="businessAdvisor" id="afterBusAdvisor" pointcut-ref="alterPoint"/>
    	<!-- After returning advisor bean -->
    	<bean id="businessAdvisor" class=""></bean>
    	<!-- the second way using aspectJ -->
    	<!-- active aspectj autoproxy -->
    	<aop:aspectj-autoproxy />
    	<!-- advisor using aspectJ -->
    	<bean class="" />
    Business Service:
    	/* Method A
    	 * (non Javadoc)
    	 * @see
            @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);
    		while (it.hasNext()) {
    			Menu menu = (Menu);
    			addChild(menu, root);
    		return root;
    The first processing advice method using spring aop:
    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(),
    		BusinessLogger annoLogger = implMethod
    		String operation = annoLogger != null ? annoLogger.value() : "null";
    		StringBuilder tmp = new StringBuilder();
    The second processing advice method using aspectJ:
    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.