spring和hibernate配置错误无法从url位置导入bean定义

ttcibm8c  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(365)

我正尝试在Windows7x66上使用netbeans开发环境设置一个新的spring/hibernate/Apache7/mysql。
所有必需的spring框架和hibernate jar都已经由netbeans预先打包。
我得到以下错误:

  1. SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  2. org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:./WEB-INF/hibernate-context.xml]
  3. Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml];

这表示我的applicationcontext.xml无法使用以下标记找到我的hibernate-context.xml:
方法1#

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

进近#2

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

我的hibernate-context.xml实际上出现在“./web inf/hibernate context.xml”中
所以这似乎是某种spring类路径问题,我在环境变量中设置了类路径,但仍然得到错误。
请给我一个干净的解决办法。
web.xml文件:

  1. <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">
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>/WEB-INF/applicationContext.xml</param-value>
  5. </context-param>
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>
  9. <servlet>
  10. <servlet-name>cmgr</servlet-name>
  11. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>cmgr</servlet-name>
  16. <url-pattern>/</url-pattern>
  17. </servlet-mapping>
  18. <session-config>
  19. <session-timeout>
  20. 30
  21. </session-timeout>
  22. </session-config>

applicationcontext.xml:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation=" http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  18. <!-- Activates various annotations to be detected in bean classes -->
  19. <context:annotation-config />
  20. <!-- 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-->
  21. <context:component-scan base-package="com.cmgr" />
  22. <!-- Configures the annotation-driven Spring MVC Controller programming model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
  23. <mvc:annotation-driven/>
  24. <!-- mapping of static resources-->
  25. <mvc:resources mapping="/resources/**" location="/resources/" />
  26. <import resource="classpath:./WEB-INF/hibernate-context.xml" />
  27. </beans>

hibernate-context.xml:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation=" http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  18. <!--
  19. To change this template, choose Tools | Templates
  20. and open the template in the editor.
  21. -->
  22. <!-- Declare a datasource that has pooling capabilities-->
  23. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  24. destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
  25. p:url="jdbc:mysql://localhost:3306/cmgr"
  26. p:username="root"
  27. p:password="pass"
  28. p:maxActive="0"
  29. p:initialSize="50"/>
  30. <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
  31. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource"/>
  33. <property name="configLocation" value="hibernate.cfg.xml"/>
  34. <property name="packagesToScan" value="com.cmgr.controller" />
  35. </bean> <!-- Enable annotation style of managing transactions -->
  36. <tx:annotation-driven transaction-manager="transactionManager" />
  37. <!-- Declare a transaction manager-->
  38. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
  39. </beans>
slwdgvem

slwdgvem1#

web.xml中通常会提到application-context.xml。这是所有spring上下文文件的主配置。
application-context.xml又包含导入的单独的上下文xml文件;就像你做的那样。
这通常是采取的方法。
如果已经有固定数量的上下文文件,但看不到它们在更改,则可以执行以下操作:

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>
  4. WEB-INF/applicationContext.xml,
  5. classpath:hibernate-context.xml
  6. </param-value>
  7. </context-param>

只要上下文文件在类路径中;您只需使用“classpath:”就可以了。

相关问题