Results 1 to 8 of 8

Thread: Spring AOP does not take effect.Please help me.

  1. #1
    Join Date
    Mar 2008
    Posts
    12

    Default 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 at 01:34 AM.

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

    Default

    @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...
    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
    Join Date
    Mar 2008
    Posts
    12

    Default

    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.

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

    Default

    Are they declared in the same ApplicationContext? Do you load them in the same instance?
    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
    Join Date
    Mar 2008
    Posts
    9

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

  6. #6
    Join Date
    Mar 2008
    Posts
    12

    Default

    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?

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

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

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

  8. #8
    Join Date
    Mar 2008
    Posts
    12

    Default

    Thank you. Marten Deinum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •