Announcement Announcement Module
No announcement yet.
BeanFactory not initialized or already closed Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • BeanFactory not initialized or already closed


    I'm just getting started with Spring (newbie). I am trying to get a reference to a bean factory from within a servlet so that I can load a Spring bean, but am getting the following exception when I execute that line of code.

    [INFO] ContextLoader - -Root WebApplicationContext: initialization completed in 279 ms
    StandardWrapperValve[TestServlet]: PWC1406: Servlet.service() for servlet TestServlet threw exception
    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    I've been trying to figure this out for hours and am at wits end. I'll describe my project:

    - Maven 2
    - Spring 2.5.5
    - Web application deployed on GlassFish V2
    - web.xml has two listeners configured: org.springframework.web.context.ContextLoaderListe ner and org.springframework.web.context.request.RequestCon textListener
    - JSF 1.2 + Facelets
    - JPA (toplink essentials) and JTA
    - Class interface that describes a DAO
    - Implementation class of DAO interface that uses Transactional and PersistenceContext annotations (didn't put "at" sign because forum software thinks it is a URL and won't let me post). Not using Spring templates.
    - A single JPA entity using annotations
    - Servlet that tries tries to get a reference to the BeanFactory so it can ask for an instance of the DAO and use it.

    Here is the bit of code from my servlet:

    WebApplicationContext beanFactory = WebApplicationContextUtils.getWebApplicationContext(getServletContext());  
    SettingsDAO dao = (SettingsDAO) beanFactory.getBean("SettingsDAO");
    /WEB-INF/applicationContext.xml follows below:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=..truncated because forum won't let me post urls...>
        <!-- JPA Setup -->
        <bean class="" />
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="jpaVendorAdapter">
              <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="generateDdl" value="false"/>
                <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
            <property name="loadTimeWeaver">
              <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        <!-- DAOs -->    
        <bean id="SettingsDAO" class=""/>
        <!-- JSF MBeans -->
        <bean id="MyAppContext" class="" 
            init-method="init" destroy-method="destroy"/>
        <bean id="MyUserContext" class=""
            destroy-method="destroy" scope="session"/>
    My web.xml has the following in it:

    I know that the Spring backed JSF managed beans are working and the facelets can use them. I know it is injecting an EntityManager into my DAO because I put code int he setter to check if a non-null value is being passed in.


  • #2
    I replaced this line:

    WebApplicationContext beanFactory = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    with this one:

    WebApplicationContext beanFactory =  (WebApplicationContext) getServletContext().getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
    and that got me past that problem. I don't understand why loading the spring context from the servlet context manually works. Isn't that what WebApplicationContextUtils.getWebApplicationContex t does too?