2012-08-24 95 views
1

我試圖在Windows 7 x66上使用NetBeans開發環境設置新的Spring/Hibernate/Apache 7/MySQL。Spring&Hibernate配置錯誤無法從URL位置導入bean定義

所有必需的Spring框架& Hibernate JARS已經被NetBeans預包裝。

,我發現了以下錯誤:

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]; 

這表明使用下面的代碼中加入applicationContext.xml中找不到我的休眠-context.xml中:

方法1#

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

方法#2

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

我休眠-context.xml中處於「../WEB-INF/hibernate-context.xml」

所以這似乎是某種春季類路徑問題的實際存在,我設置類路徑在環境變量中,但仍然出現錯誤。

請告訴我一個乾淨的解決方案。

的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> 

休眠-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> 
+0

我有相同的文件夾結構,並使用你的第一種方法它爲我工作。請發佈您的'web.xml'和整個'hibernate-context.xml'。 – jelies

+0

你可以發佈你的web.xml和你的上下文文件作爲你的文章的一部分,而不是一個圖像? –

回答

1

喲你通常擁有Web.xml中提到的application-context.xml。 這是所有彈簧上下文文件的主配置。

application-context.xml又包含單獨的導入的上下文xml文件;正如你所做的那樣。

這通常是採取的方法。

如果你已經擁有的背景文件鎖定一個固定號碼,你沒有看到他們改變了你可以這樣做:

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

只要上下文文件在你的類路徑;你可以使用「classpath:」,它應該可以正常工作。

相關問題