Announcement Announcement Module
Collapse
No announcement yet.
Method not getting intercepted Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Method not getting intercepted

    I have a very interesting problem: I'm using a hibernate session factory with a RoutingDataSource (for 2 data sources). I'm using 2 facades in my service layer which also defines my transactions boundaries. I'm combining the service layer and data access layer into one layer. Each facade interacts with a different data source. I thought this would be a perfect application using an Aspect - intercept the calls to the service layer to determine which data source to use for the hibernate session. Below are the relevant classes:

    Code:
    public interface HibernateDao {
    	public <T> T get(Long id, Class<T> clazz);
    	public <T> List<T> find(Class<T> clazz, int start, int limit);
    ...
    }
    
    @Repository
    public abstract class AbstractHibernateDaoImpl extends HibernateDaoSupport implements HibernateDao {
    	
    	@SuppressWarnings("unchecked")
    	@Transactional
    	public <T> T get(Long id, Class<T> clazz) {
    		return (T)getHibernateTemplate().load(clazz, id);
    	}
    
    	@SuppressWarnings("unchecked")
    	@Transactional
    	public <T> List<T> find(Class<T> clazz, int start, int limit) {
    		Criteria criteria = getSession().createCriteria(clazz);
                    ..
    		return criteria.list();
    	}
    }
    
    public interface FooFacade extends HibernateDao {
          public List<Product> findProductsByTitle(String title);
    }
    
    public class FooFacadeImpl extends AbstractHibernateDaoImpl implements FooFacade {
    	@SuppressWarnings("unchecked")
    	@Transactional
            public List<Product> findProductsByTitle(String title) {
          ...
                return some list;
          }
    }
    
    public interface BarFacade extends HibernateDao {
    public List<Bar> findBarByTitle(String title);
    }
    
    public class BarFacadeImpl extends AbstractHibernateDaoImpl implements BarFacade {
    @SuppressWarnings("unchecked")
    @Transactional
    public List<Bar> findBarByTitle(String title) {
    ...
    return some list;
    }
    }
    And here is my intercepting class:

    Code:
    @Aspect
    @Order(100)
    public class DataSourceInterceptor {
    	
    	@Before("execution(* com.abc..FooFacade.*(..))")
    	public void setFooDataSource() {
    		log.info("...method intercepted, setting datasource to foo......");
    		StoreContextHolder.setStore(Store.FOO);
    	}
    	
    	@Before("execution(* com.abc..BarFacade.*(..))")
    	public void setBarDataSource() {
    		log.info("...method intercepted, setting datasource to bar......");
    		StoreContextHolder.setStore(Store.BAR);
    	}
    }
    When I call a method from a controller to one of the business facades (i.e.,FooFacade or BarFacade) the call is only being intercepted by my DataSourceInterceptor if the method is defined on the Facade interface( findProductsByTitle() ). In other words, if the method is implemented by AbstractHibernateDaoImpl it will NOT be intercepted. Code example:
    Code:
    @Controller
    @RequestMapping("/somurl")
    public class SomeController {
        @Autowired
        private FooFacade fooFacade;
        @Autowired
        private BarFacade barFacade;
    
        @RequstMapping
        public String handleRequest(String title) {
          // this method will get intercepted, method is implemented by
          // fooFacadeImpl
          fooFacade.findProductsByTitle(title);
    
          // this method will NOT be intercepted, method is implemented by 
          // AbstractHibernateDaoImpl.  An exception will occur indicating the table or
          // view cannot be found
          barFacade.get(new Long(10), Bar.class);
    
         // this method will be intercepted, method is implemented by facade
         barFacade.findBarByTitle(title);
    
        }
    
    }
    How do I intercept the methods implemented by AbstractHibernateDaoImpl?
    Last edited by [email protected]; Aug 3rd, 2009, 12:46 PM.

  • #2
    try with target(com.abc..FooFacade)

    cheers

    Comment


    • #3
      That did the trick!!

      Much appreciated

      Comment


      • #4
        In http://www.eclipse.org/aspectj/doc/r....html#matching says:
        When matching method-execution join points, if the execution pointcut method signature specifies a declaring type, the pointcut will only match methods declared in that type, or methods that override methods declared in or inherited by that type
        so when matching execution(* com.abc..FooFacade.*(..)) only match methods defined in HibernateDao if FooFacadeImpl overwrite thems.

        if using AspectJ you can match over call(* com.abc..FooFacade.*(..)) but Spring don't support the call primitive, so the match on execution over Interfaces may be confused.

        cheers
        Last edited by chelu; Aug 4th, 2009, 10:43 AM.

        Comment

        Working...
        X