Announcement Announcement Module
Collapse
No announcement yet.
How does the LocalSessionFactoryBean convert to SessionFacto Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How does the LocalSessionFactoryBean convert to SessionFacto

    My appContext has the following:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource"><ref bean="dataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>org/appfuse/model/User.hbm.xml</value>
    </list>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.P ostgreSQLDialect</prop>
    </props>
    </property>
    </bean>

    <bean id="userDAO" class="org.appfuse.persistence.hibernate.UserDAOHi bernate">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>


    If I look at the setSessionFactory in HibernateDAOSupport the type of the argument is net.sf.hibernate.SessionFactory. However, my sessionFactory in the configuration file is of type org.springframework.orm.hibernate.LocalSessionFact oryBean. LocalSessionFactoryBean is not a descendant of SessionFactory. How does spring convert this?

    Dino

  • #2
    First of all org.springframework.orm.hibernate.LocalSessionFact oryBean is a factory, it implements interface FactoryBean.
    When you ask Spring for a bean from your applicationContext (or references this bean in an other), Spring first checks the nature of your bean, if it implements interface FactoryBean, it will be treated as a factory, thus, Spring calls bean method getObject() and returns the object returned by that method.
    If you look at org.springframework.orm.hibernate.LocalSessionFact oryBean source you will find:
    Code:
    public class LocalSessionFactoryBean implements FactoryBean, InitializingBean, DisposableBean &#123;
      ...
      private SessionFactory sessionFactory;
      ...
    
      /**
       * Return the singleton SessionFactory.
       */
      public Object getObject&#40;&#41; &#123;
        return this.sessionFactory;
      &#125;
    &#125;

    Comment

    Working...
    X