Hi,
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.
I've been trying to figure this out for hours and am at wits end. I'll describe my project:...
[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
- 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:
/WEB-INF/applicationContext.xml follows below:Code:WebApplicationContext beanFactory = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); SettingsDAO dao = (SettingsDAO) beanFactory.getBean("SettingsDAO"); out.print(dao.getProperty("systemLogLevel"));
My web.xml has the following in it:Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=..truncated because forum won't let me post urls...> <!-- JPA Setup --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <tx:annotation-driven/> <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"/> </bean> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- DAOs --> <bean id="SettingsDAO" class="com.business.appname.dao.settings.SettingsDAOImpl"/> <!-- JSF MBeans --> <bean id="MyAppContext" class="com.business.appname.model.MyAppContext" init-method="init" destroy-method="destroy"/> <bean id="MyUserContext" class="com.business.appname.model.MyUserContext" destroy-method="destroy" scope="session"/> </beans>
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.Code:... <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> ...
Thanks,
Ryan


Reply With Quote
