ERROR de configuración de Spring & Hibernate No se pudieron importar las definiciones de bean desde la ubicación de la URL

I’m trying to setup a new Spring/Hibernate/Apache 7/MySQL on Windows 7 x66, using NetBeans Development environment.

All the required Spring Framework & Hibernate JARS are already prepackaged by NetBeans.

I’m getting the following error:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:./WEB-INF/hibernate-context.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml];

Which indicates that my applicationContext.xml cannot find my hibernate-context.xml using the following tags :

Approach 1#

<import resource="hibernate-context.xml" />

Enfoque # 2

<import resource="classpath:./WEB-INF/hibernate-context.xml" />

My hibernate-context.xml is actually present in “../WEB-INF/hibernate-context.xml”

So this seems to be some kind of Spring classpath issue, I set the classpath in environment variables but still getting error.

Please advise me on a clean solution.

web.xml:

<web-app metadata-complete="true" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>cmgr</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cmgr</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout> 
            30 
        </session-timeout>
    </session-config>

applicationContext.xml:

<beans  xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"       
xmlns:context="http://www.springframework.org/schema/context"       
xsi:schemaLocation="    http://www.springframework.org/schema/beans     
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
http://www.springframework.org/schema/tx     
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.   For example @Controller and @Service. Make sure to set the correct base-package--> 
    <context:component-scan base-package="com.cmgr" />    
    <!-- Configures the annotation-driven Spring MVC Controller programming model.  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
    <mvc:annotation-driven/>
    <!-- mapping of static resources-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <import resource="classpath:./WEB-INF/hibernate-context.xml" />     
</beans>

hibernate-context.xml:

<beans  xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"       
xmlns:context="http://www.springframework.org/schema/context"       
xsi:schemaLocation="    http://www.springframework.org/schema/beans     
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
http://www.springframework.org/schema/tx     
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!-- Declare a datasource that has pooling capabilities-->  
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"    
destroy-method="close"    p:driverClassName="com.mysql.jdbc.Driver"    
p:url="jdbc:mysql://localhost:3306/cmgr"   
p:username="root"    
p:password="pass"    
p:maxActive="0"    
p:initialSize="50"/>
<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->   
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">    
<property name="dataSource" ref="dataSource"/>    
<property name="configLocation" value="hibernate.cfg.xml"/>    
<property name="packagesToScan" value="com.cmgr.controller" />
</bean>  <!-- Enable annotation style of managing transactions --> 
<tx:annotation-driven transaction-manager="transactionManager" /> 
<!-- Declare a transaction manager--> 
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
</beans>

preguntado el 24 de agosto de 12 a las 22:08

Could you post your web.xml and your context files as part of your post instead of an image? -

I have the same folder structure and using your first approach it's working for me. Post your web.xml y todo hibernate-context.xml, por favor. -

1 Respuestas

You usually have the application-context.xml mentioned in the Web.xml. This is the master configuration for all spring context files.

application-context.xml in turn contains separate context xml file which are imported; as you have done.

This is usually the approach taken.

If you already have a fixed number of context files and you do not see them changing you can do this:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/applicationContext.xml,
        classpath:hibernate-context.xml
    </param-value>
  </context-param>

As long as the context files are in your class path; you can just use "classpath:" and it should work fine.

respondido 29 mar '13, 19:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.