java.lang.IllegalStateException: SessionFactory not initialized yet

Spring JIRA | Adrian Moos | 2 years ago
  1. 0

    When a @Bean method returns an instance of a FactoryBean, Spring proxies the factory bean, redirecting calls to getObject() to applicationContext.getBean(). The relevant code reads: {code} /** * Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory * instead of creating a new instance. These proxies are created only when calling a FactoryBean from * within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean * instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing, * it will not be proxied. This too is aligned with the way XML configuration works. */ private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory, final String beanName) throws InstantiationException, IllegalAccessException { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(fbClass); enhancer.setUseFactory(false); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (method.getName().equals("getObject") && args.length == 0) { return beanFactory.getBean(beanName); } return proxy.invokeSuper(obj, args); // bug here? } }); return enhancer.create(); } {code} In the marked line, obj refers to the proxy object. Therefore, calls to methods other than getObject() are forwarded to the super implementation *on the proxy object*, which has a different state than the FactoryBean it proxies. This breaks the following usecase: {code} @Bean protected DBTool dbTool() { return new DBTool(hibernateSessionFactory()); } @Bean protected AnnotationSessionFactoryBean hibernateSessionFactory() { // hibernate setup goes here } {code} where DBTool has a method: {code} public void createSchema() { annotationSessionFactory.createDatabaseSchema(); } {code} which now throws {code} java.lang.IllegalStateException: SessionFactory not initialized yet at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory(AbstractSessionFactoryBean.java:215) at org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema(LocalSessionFactoryBean.java:989) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.CGLIB$createDatabaseSchema$12(<generated>) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept(ConfigurationClassEnhancer.java:383) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema(<generated>) at ch.bedag.ste.app.cf.hibernate.framework.DBTool.createSchema(DBTool.java:23) {code} because the proxy object is a factory that has not been configured.

    Spring JIRA | 2 years ago | Adrian Moos
    java.lang.IllegalStateException: SessionFactory not initialized yet
  2. 0

    When a @Bean method returns an instance of a FactoryBean, Spring proxies the factory bean, redirecting calls to getObject() to applicationContext.getBean(). The relevant code reads: {code} /** * Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory * instead of creating a new instance. These proxies are created only when calling a FactoryBean from * within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean * instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing, * it will not be proxied. This too is aligned with the way XML configuration works. */ private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory, final String beanName) throws InstantiationException, IllegalAccessException { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(fbClass); enhancer.setUseFactory(false); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (method.getName().equals("getObject") && args.length == 0) { return beanFactory.getBean(beanName); } return proxy.invokeSuper(obj, args); // bug here? } }); return enhancer.create(); } {code} In the marked line, obj refers to the proxy object. Therefore, calls to methods other than getObject() are forwarded to the super implementation *on the proxy object*, which has a different state than the FactoryBean it proxies. This breaks the following usecase: {code} @Bean protected DBTool dbTool() { return new DBTool(hibernateSessionFactory()); } @Bean protected AnnotationSessionFactoryBean hibernateSessionFactory() { // hibernate setup goes here } {code} where DBTool has a method: {code} public void createSchema() { annotationSessionFactory.createDatabaseSchema(); } {code} which now throws {code} java.lang.IllegalStateException: SessionFactory not initialized yet at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory(AbstractSessionFactoryBean.java:215) at org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema(LocalSessionFactoryBean.java:989) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.CGLIB$createDatabaseSchema$12(<generated>) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept(ConfigurationClassEnhancer.java:383) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema(<generated>) at ch.bedag.ste.app.cf.hibernate.framework.DBTool.createSchema(DBTool.java:23) {code} because the proxy object is a factory that has not been configured.

    Spring JIRA | 2 years ago | Adrian Moos
    java.lang.IllegalStateException: SessionFactory not initialized yet
  3. 0

    Terracotta 3.5.1 On JBoss 6

    Terracotta | 5 years ago | urciolo
    java.lang.IllegalStateException: not yet initialized
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    ModelRepository startup issue

    GitHub | 5 months ago | kaikreuzer
    java.lang.IllegalStateException: ScriptServiceUtil not initialized yet!
  6. 0

    When there are circular references between the beans AND there is a post processor involved, you get a 'Singleton not initialized yet' error. Below is the full stack trace. I have simple test case that I could send if that would help. Jun 30, 2008 10:04:07 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@104a917: display name [org.springframework.context.support.ClassPathXmlApplicationContext@104a917]; startup date [Mon Jun 30 10:04:06 EDT 2008]; root of context hierarchy Jun 30, 2008 10:04:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [mybeans.xml] Jun 30, 2008 10:04:09 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@104a917]: org.springframework.beans.factory.support.DefaultListableBeanFactory@10741fa Jun 30, 2008 10:04:09 AMorg.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10741fa: defining beans [MyBeanPostProcessor#0,bean1,bean2]; root of factory hierarchy Jun 30, 2008 10:04:09 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10741fa: defining beans [MyBeanPostProcessor#0,bean1,bean2]; root of factory hierarchy Exception in thread "Main Thread" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean1': Cannot resolve reference to bean 'bean2' while setting bean property 'friendBean'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean2': Cannot resolve reference to bean 'bean1' while setting bean property 'friendBean'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean1': Post-processing of the FactoryBean's object failed; nested exception is java.lang.IllegalStateException: Singleton instance not initialized yet at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1172) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:940) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:437) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:353) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:394) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:736) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:123) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:76) at SpringTest.main(SpringTest.java:9) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean2': Cannot resolve reference to bean 'bean1' while setting bean property 'friendBean'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean1': Post-processing of the FactoryBean's object failed; nested exception is java.lang.IllegalStateException: Singleton instance not initialized yet at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1172) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:940) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:437) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:353) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269) ... 16 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean1': Post-processing of the FactoryBean's object failed; nested exception is java.lang.IllegalStateException: Singleton instance not initialized yet at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:140) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:114) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:90) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1269) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269) ... 27 more Caused by: java.lang.IllegalStateException: Singleton instance not initialized yet at org.springframework.beans.factory.config.AbstractFactoryBean.getSingletonInstance(AbstractFactoryBean.java:173) at org.springframework.beans.factory.config.AbstractFactoryBean.access$000(AbstractFactoryBean.java:56) at org.springframework.beans.factory.config.AbstractFactoryBean$1.invoke(AbstractFactoryBean.java:155) at $Proxy0.toString(Unknown Source) at java.lang.String.valueOf(String.java:2615) at java.lang.StringBuilder.append(StringBuilder.java:116) at MyBeanPostProcessor.postProcessAfterInitialization(MyBeanPostProcessor.java:8) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:331) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1340) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:137) ... 33 more

    Spring JIRA | 8 years ago | Manju James
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean1': Cannot resolve reference to bean 'bean2' while setting bean property 'friendBean'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean2': Cannot resolve reference to bean 'bean1' while setting bean property 'friendBean'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean1': Post-processing of the FactoryBean's object failed; nested exception is java.lang.IllegalStateException: Singleton instance not initialized yet

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalStateException

      SessionFactory not initialized yet

      at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory()
    2. Hibernate
      AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke
      1. org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory(AbstractSessionFactoryBean.java:215)
      2. org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema(LocalSessionFactoryBean.java:989)
      3. org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.CGLIB$createDatabaseSchema$12(<generated>)
      4. org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke(<generated>)
      4 frames
    3. Spring Core
      MethodProxy.invokeSuper
      1. org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      1 frame
    4. Spring Context
      ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept
      1. org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept(ConfigurationClassEnhancer.java:383)
      1 frame
    5. Hibernate
      AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema
      1. org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema(<generated>)
      1 frame
    6. ch.bedag.ste
      DBTool.createSchema
      1. ch.bedag.ste.app.cf.hibernate.framework.DBTool.createSchema(DBTool.java:23)
      1 frame