Spring + JPA + Hibernate - IllegalStateException: BeanFactory not initialized or already closed -
i tried @ similar threads, cannot figure out mistake doing , causing following error during deploy:
severe: exception sending context destroyed event listener instance of class org.springframework.web.context.contextloaderlistener java.lang.illegalstateexception: beanfactory not initialized or closed - call 'refresh' before accessing beans via applicationcontext
applicationcontext.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:property-placeholder location="classpath*:spring/*.properties" /> <context:component-scan base-package="com.lh.clte" /> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="driverclassname" value="${database.driverclassname}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.username}" /> <property name="password" value="${database.password}" /> <property name="initialsize" value="3" /> <property name="maxactive" value="10" /> </bean> <tx:annotation-driven mode="proxy" transaction-manager="transactionmanager"/> <bean class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" id="entitymanagerfactory"> <property name="persistenceunitname" value="persistenceunit" /> <property name="datasource" ref="datasource" /> </bean> <bean class="org.springframework.orm.jpa.jpatransactionmanager" id="transactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> </bean> <import resource="classpath:persistence.xml" /> </beans>
database.properties correctly placed , loaded
persistence.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" version="2.0" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="persistenceunit" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.mysql5innodbdialect" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.improvednamingstrategy" /> <property name="hibernate.connection.charset" value="utf-8" /> </properties> </persistence-unit> </persistence>
when drop import line in application context different exception (i.e. no persistence unit given name); persistence.xml located @ \src\main\resources\persistence.xml.
web.xml
<?xml version="1.0" encoding="iso-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>clte rest server</display-name> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring/applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>rest</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
removing, application context, line "" import declaration (both included), deploy can correctly concluded.
do not <import /> persistence.xml since not spring configuration file. rather, move meta-inf/persistence.xml default location.
Comments
Post a Comment