Announcement Announcement Module
Collapse
No announcement yet.
Get hibernate configuration from LocalSessionFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get hibernate configuration from LocalSessionFactoryBean

    I want to get a hibernate configuration object from a LocalSessionFactoryBean, so that I can plug it into a hibernate console on startup. I defined this in my context XML:

    Code:
    <bean 
    		id="sessionFactory"
    class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		
    		<property name="dataSource">
    			<ref bean="dataSource"/>
    		</property>
    	</bean>
    	<bean
    		id="hibernateConfiguration"
    		factory-bean="&amp;sessionFactory"
    		factory-method="getConfiguration"
    	/>
    And it works fine, giving me a configuration bean as hoped. The problem is, hibernate console is using the configuration.buildSessionFactory() method to get a session factory, and I want to override buildSessionFactory() to return the session factory from the spring container.

    So, I thought lookup method injection was the solution. I tried this:

    Code:
    <bean
    		id="hibernateConfiguration"
    		factory-bean="&amp;sessionFactory"
    		factory-method="getConfiguration">
    		
    		<lookup-method 
    			name="buildSessionFactory" 
    			bean="sessionFactory"/>
    	</bean>
    But when the container is created I get this error message:

    org.springframework.beans.factory.support.BeanDefi nitionValidationException: Cannot combine static factory method with method overrides: the static factory method must create the instance
    This is confusing because I'm not using a static factory method, I'm using an instance factory method...what am I missing?

  • #2
    Method over-rides require that Spring can instantiate the object directly, and choose to use CGLIB to generate a subclass. This is not possible if the instance was returned by a factory method, static or otherwise.

    Comment


    • #3
      I was able to use a ProxyFactoryBean to force the method to perform to my liking.

      But....isn't this approach using CGLIB to create a proxy that extends the hibernate configuration (even though the configuration object isn't instantiated by the bean factory)? Isn't that the same approach that's employed with the method override?

      For anyone interested, this is the configuration I used:

      Code:
      	<bean 
      		id="hibernateConfiguration"
      		class="org.springframework.aop.framework.ProxyFactoryBean">
      		
      		<property name="proxyTargetClass">
      			<value>true</value>
      		</property>
      		
      		<!-- can't use anonymous inline bean!?  Must give an ID -->
      		<property name="target">
      			<bean id="baseHibernateConfiguration"
      				factory-bean="&amp;sessionFactory"
      				factory-method="getConfiguration"
      			/>			
      		</property>
      		
      		<property name="interceptorNames">
      			<list>
      				<idref bean="hibernateConfigurationAdvisor"/>
      			</list>
      		</property>
      	</bean>
      	<bean
      		id="hibernateConfigurationAdvisor"
      		class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
      		
      		<property name="mappedName">
      			<value>buildSessionFactory</value>
      		</property>
      		
      		<property name="advice">
      			<bean class="HibernateConfigurationAdvice"/>
      		</property>
      	</bean>
      and in my "HibernateConfigurationAdvice" simply implementing this invoke method:

      Code:
      	
      public class HibernateConfigurationAdvice 
      		implements org.aopalliance.intercept.MethodInterceptor &#123;	
      	public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable &#123;
      		return _ctx.getBean&#40;"sessionFactory"&#41;;
      	&#125;
      &#125;

      Comment

      Working...
      X