Announcement Announcement Module
Collapse
No announcement yet.
Spring AOP does not take effect.Please help me. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring AOP does not take effect.Please help me.

    Dear all,I found a problem when I'm using Spring AOP in my project.
    If Annotation is used to define my business service bean, I can not get the excepted result. The related Java codes and Spring configurations are shown as
    follow:
    1.My Spring Configuration aop configurations
    Code:
    	<context:annotation-config/>
    	<aop:config>
    		<aop:pointcut id="businessPoint" 
    				expression="execution(* com.jacky.menu.bus.impl.*.*(..))" />
    		
    		<aop:advisor advice-ref="businessAdvisor" id="afterBusAdvisor" pointcut-ref="businessPoint"/>
    	</aop:config>
    	
    	<bean id="businessAdvisor" class="com.jacky.menu.aop.BusinessAdvisor"></bean>
    2.My business service class
    Code:
    @Service
    public class MenuManagerImpl implements IMenuManager {
    
    	@Autowired
    	private IMenuDao menuDao;
    
    	/*
    	 * (non Javadoc)
    	 * 
    	 * @see com.jacky.menu.bus.IMenuManager#generateMenu()
    	 */
    	public Menu generateMenu(){
    		List<Menu> list = menuDao.getMenuList();
    		return this.generateTreeMenu(list);
    	}
    }
    3.My after returning advise class
    Code:
    public class BusinessAdvisor implements AfterReturningAdvice {
    
    	@Override
    	public void afterReturning(Object returnValue, Method method,
    			Object[] args, Object target) throws Throwable {
    		System.out.println("AOP business class after business method returning invoked!");
    		System.out.println("Class:" + target.getClass().getName());
    		System.out.println("Mehtod:" + method.getName());
    	}
    
    }
    If I define MenuManagerImpl.class as a spring bean by set a "@Service" Annotation, I can not get the output result of afterReturning method in the system console. But if I declare the MenuManagerImpl in the Spring configuration files like this:"
    Code:
    <bean id="menuManager" class="com.jacky.menu.bus.impl.MenuManagerImpl"></bean>
    ", the excepted result of afterReturning method will be printed in the system console.
    I dont know what is the reason, and how to make it work if I config my business service class with a "@Service" Annotation?
    Last edited by dakulaliu; Mar 10th, 2008, 01:34 AM.

  • #2
    @Service is doing nothing in your context. You only supplied the <context:annotation-config /> which does NOTHING for the @Component (or sub) annotations! You should specify the <context:component-scan /> that scans for components and registers them as beans... In your case there are no beans...

    Comment


    • #3
      Sorry, the "<context:component-scan base-package="com.jacky.menu"/>" has been declared in another application context configuration file, but I still can not get the excepted result.

      Comment


      • #4
        Are they declared in the same ApplicationContext? Do you load them in the same instance?

        Comment


        • #5
          I had a similar problem

          Dakulaliu,

          I had a similar problem, although my setup was different. The solution ended up being that my pointcut referred to my concrete class but my code called the method through my interface. For some reason the pointcut didn't take effect. Have you tried adding the @Service to IMenuManager?

          Comment


          • #6
            Thanks,
            mdeinum,I put the them into the same application context config, then I got the excepted result.

            But I dont know why they must be declared in the same app context? Is there any difference if configed in same app ctx or severally?

            Originally posted by mdeinum View Post
            Are they declared in the same ApplicationContext? Do you load them in the same instance?
            And thanks ShaneS.

            Comment


            • #7
              It can be in a different xml file BUT is has to be loaded in the same ApplicationContext. A Bean(Factory)PostProcessor operates only in the ApplicationContext it is loaded in. So a Bean(Factory)PostProcessor defined in a parent context doesn't processes beans in its child contexts.

              Comment


              • #8
                Thank you. Marten Deinum.

                Comment

                Working...
                X