Announcement Announcement Module
Collapse
No announcement yet.
Hibernate 4 Interceptor BeanCurrentlyInCreationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate 4 Interceptor BeanCurrentlyInCreationException

    Hi folks,

    I'm currently using
    Spring 3.1.1
    Hibernate 4.1.2

    When I try to add an entityInterceptor to my Hibernate sessionFactory Bean, I get the following error:

    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:src/test/resources/spring-test-datasource-context.xml]: Cannot resolve reference to bean 'traineeInterceptor' while setting bean property 'entityInterceptor'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': FactoryBean which is currently in creation returned null from getObject
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
    	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    	... 53 more
    Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': FactoryBean which is currently in creation returned null from getObject
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:156)
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:109)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    	... 68 more
    My sessionFactory and Interceptor look like the following:

    Code:
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    
    		<property name="hibernateProperties">
    			<props>
    [...]
                            </props>
    		</property>
    		<property name="entityInterceptor" ref="traineeInterceptor" />
    	</bean>
    Code:
    	<bean id="traineeInterceptor"
    		class="com.core.dao.hibernate.TraineeInterceptor" depends-on="sessionFactory" lazy-init="true" />
    Code:
    import java.io.Serializable;
    import java.sql.Timestamp;
    import java.util.Date;
    
    import org.hibernate.EmptyInterceptor;
    import org.hibernate.type.Type;
    
    
    /**
     * @author v965287
     * 
     */
    public class TraineeInterceptor extends EmptyInterceptor {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 4756368525765636969L;
    
    	@Override
    	public boolean onSave(Object entity, Serializable id, Object[] state,
    			String[] propertyNames, Type[] types) {
    		if (entity instanceof Trainee) {
    			for (int i = 0; i < propertyNames.length; i++) {
    				if ("lastModified".equals(propertyNames[i])) {
    					state[i] = new Timestamp(new Date().getTime());
    				}
    				return true;
    			}
    		}
    		return false;
    	}
    
    }
    Any help would be appreciated!

    Thanks a lot!
    Last edited by feuse8; May 3rd, 2012, 04:24 AM.

  • #2
    Remove the depends on (not sure why it is even there). You have a bean which you want to inject into the session factory but it requires the session factory to be initialized before it can be injected (not going to work that way).

    Comment


    • #3
      Hi Marten,

      thanks for your response. Removing the "depends-on" tag didn't solve the problem. The exception remains the same.

      Comment


      • #4
        Also make sure that you don't have a dependency on the sessionfactory by beans needed by the interceptor. Judging from the stacktrace there is an annotation which is probaly put on a SessionFactory (@Autowired or @Inject like that).

        Comment


        • #5
          Hi,

          I do have a dependency to the SessionFactory in my DAOs (all DAOs are inheriting from a BaseDAO containing the injected SessionFactory). The interceptor doesn't contain a reference to the SessionFactory. Objects used by the Interceptor (in my case the "Trainee" Object) don't contain a reference.

          So I'm not sure if the autowiring of the SessionFactory in my DAOs really causes the problem.

          @edit:
          After removing the dependency, the application is able to start. But removing it isn't the whole solution because I need the SessionFactory getting injected by Spring anyhow. Is there any other possibility to inject the SessionFactory except by an annotation or setting a property in a bean definition??
          Last edited by feuse8; May 3rd, 2012, 03:58 AM.

          Comment


          • #6
            Ok guys I solved the problem.

            In my servlet.xml I had the following entries:

            Code:
               [...]
               <context:component-scan base-package="com.my.package" />
               <import resource="spring-test-datasource-context.xml" />
               [...]
            This caused the DAOs to be created before my SessionFactory. By changing the order of these two entries, I could solve the problem.
            Code:
               [...]   
               <import resource="spring-test-datasource-context.xml" />
               <context:component-scan base-package="com.my.package" />
               [...]
            Thanks for helping me!!

            Comment

            Working...
            X