Announcement Announcement Module
Collapse
No announcement yet.
Spring AOP Help - Pointcut Expression not matching on all applicable classes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring AOP Help - Pointcut Expression not matching on all applicable classes

    Hi,

    I am using Spring AOP to implement the auditing use case, which is by nature a cross cutting concern. I have the following scenario:

    UI Action Class:
    Code:
    package com.travelagent.city.web.controller;
    
    import com.travelagent.city.dao.CityDao;
    
    @Controller
    @RequestMapping("/index.htm")
    public class UIControllerClass
    {
    
        private CityDao cityDao;
    
        @Autowired
        public void setCityDao(CityDao cityDao) 
        {
            this.cityDao = cityDao;
        }
    
        @RequestMapping(method = RequestMethod.GET)
        public ModelAndView add(@RequestParam("city") String city) 
        {
            cityDao.add(city);
    
            ModelAndView modelAndView = new ModelAndView("city");
    		modelAndView.addObject("city", cityDao.getCities());
    
            return modelAndView;
        }
    }

    Dao Class:
    Code:
    package com.travelagent.city.dao;
    
    public class CityDao 
    {
        public void add(String cityName)
        {
            City city = new City();
            city.setName(cityName);
    
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    
            session..saveOrUpdate(city);
        }
    }

    I have the following advice class:

    Code:
    @Aspect
    public class AuditLoggingAspect
    {
        @Before("execution(* add(..))")
        private void createAuditEvent(Method method, Object[] args, Object target) throws Throwable 
        {
            ...
        }
    }

    When the UIControllerClass.add method gets invoked as a result of the User selecting 'Add' through the UI, the aspect class intercepts the UIControllerClass.add method, but does not intercept CityDao.add method? I should add that I'm not the original creator of the AOP code in our application so I am trying to both get my head around Spring AOP and understand the existing implementation.

    Given I have written a generic pointcut expression, it seems as though there is something that is either restricting the scope to only certain packages (config outside of the aspect class??) or the default Spring AOP config only allows one method to be matched per flow of execution??

    Appreciate advise and help on this matter.

    TIA,

    Mark

  • #2
    Please read the reference guide...

    Spring AOP works only on spring defined beans and beans in the SAME context as the aspect. My guess you have both a ContextLoaderListener and a DispatcherServlet. The first contains the generic stuff (services, daos, infrastructure) and not this specific aspect the second contains the controller and the aspect...

    Comment

    Working...
    X