gpt4 book ai didi

java - Spring 和 Hibernate 配置错误无法从 URL 位置导入 bean 定义

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:41 26 4
gpt4 key购买 nike

我正在尝试使用 NetBeans 开发环境在 Windows 7 x66 上设置新的 Spring/Hibernate/Apache 7/MySQL。

所有必需的 Spring 框架和 Hibernate JAR 均已由 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 无法使用以下标签找到我的 hibernate-context.xml :

方法 1#

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

方法#2

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

我的 hibernate-context.xml 实际上存在于“../WEB-INF/hibernate-context.xml”中

所以这似乎是某种 Spring 类路径问题,我在环境变量中设置了类路径,但仍然出现错误。

请告诉我一个干净的解决方案。

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>

最佳答案

您通常会在 Web.xml 中提到 application-context.xml。这是所有 spring 上下文文件的主配置。

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:”,它应该可以正常工作。

关于java - Spring 和 Hibernate 配置错误无法从 URL 位置导入 bean 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com