Results 1 to 4 of 4

Thread: Easiest way to get HibernateInterceptor to be called

  1. #1
    Join Date
    Mar 2005
    Posts
    19

    Default Easiest way to get HibernateInterceptor to be called

    Hello everyone,

    I'm using Spring's HibernateInterceptor for my DAOs, in order to get sessions attached to them. Here's an example of my TargetProxy:

    Code:
    <bean id="MemberDAOTarget" class="org.phatcast.db.dao.MemberDAOImpl"  factory-method="getInstance">
            <property name="sessionFactory">
                <ref bean="mySessionFactory"/>
            </property>
        </bean>
    	<bean id="MemberDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
    	  <property name="proxyInterfaces">
    	    <value>org.phatcast.db.dao.MemberDAO</value>
    	  </property>
    	  <property name="interceptorNames">
    	    <list>
    	      <value>myHibernateInterceptor</value>
    	      <value>MemberDAOTarget</value>
    	    </list>
    	  </property>
    	</bean>
    The problem I have is, when I have controllers that need access to the DAOs, it's awkward to inject the DAOs through every time. I was mistakenly using MemberDAO.getInstance() in the code, until I realized the getInstance() call bypasses the HibernateInterceptor.

    Is there an easy way to get access to these DAOs without constantly creating setDao() methods in every controller, and changing the XML around? Is anyone is directly looking up the beans in the ApplicationContext? Or any other thoughts?

    I would very much appreciate it if anyone had advice as to the best way to use HibernateInterceptor.

    Andrew
    Seattle, WA

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    I don't think there is because the main philosophy (how to spell?) is about dependency injection.

    BeanA should never construct BeanB. BeanA should be provided with an instance of BeanB. If BeanA calls Singleton.getInstance() how will you provide a mock object for testing of BeanA?

    If it really is a problem (which I don't think it is) then create a base class with setDAO(final DAO dao) and extend that. I can't believe I just recommended that! Urgh!

    Anyways, the benefits of keeping construction code out of your classes and just injecting collaborators *far* outweigh the inconvenience.

    BTW; have you looked at autowiring in spring context to avoid having to explitly define setter injection?

  3. #3
    Join Date
    Mar 2005
    Posts
    19

    Default Thanks

    Yeah, that works for me. And we're going to switch to autowiring Thanks.

    Andrew

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Be *very* careful with autowiring by type, use autowire by name instead.

Similar Threads

  1. handleInvalidSubmit called, why?
    By sulam in forum Web
    Replies: 9
    Last Post: Feb 2nd, 2007, 03:11 AM
  2. Replies: 6
    Last Post: Nov 30th, 2005, 04:21 AM
  3. Replies: 1
    Last Post: Oct 3rd, 2005, 04:19 PM
  4. onSubmit not called
    By christophe in forum Web
    Replies: 4
    Last Post: Jul 29th, 2005, 05:10 PM
  5. indirectly called method not intercepted
    By springtester in forum AOP
    Replies: 1
    Last Post: Sep 1st, 2004, 06:01 AM

Posting Permissions

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