hibernate和spring xml配置有问题

时间:2015-11-04 06:47:05

标签: java xml spring hibernate spring-mvc

大家好,我一直在玩spring mvc和hibernate。但是现在我在尝试使用XML配置连接hibernate和spring时遇到错误。我在下面收到此错误:

03-Nov-2015 22:22:19.675 WARNING [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in ServletContext resource [/WEB-INF/services.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/services.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error accessing stax stream

我在src/main创建了我的webapp文件夹。在我的webapp文件夹中,我创建了WEB-INF文件夹。 WEB-INF文件夹包含文件:service.xml,dispatcher-servlet.xml。现在对于我的hibernate.cfg.xml,它被放置在src / main文件夹结构中。但是当我在加载调度程序时尝试在service.xml中添加它时,我的hibernate.cfg.xml似乎找不到了。有谁知道为什么?这是我遇到麻烦的地方:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="" />
    </bean>

我似乎无法找到configLocation的值。有没有人遇到过这个问题。

以下是我的web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Lab 2</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml, /WEB-INF/services.xml</param-value>

    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

调度-servlet.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"
       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">


    <context:component-scan base-package="edu.sjsu.cmpe275.lab2.controller"  />


       <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
       <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

      <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/views/" />
              <property name="suffix" value=".jsp" />
       </bean> -->
</beans>

service.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"
       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">


       <bean name="personService" class="edu.sjsu.cmpe275.lab2.service.PersonImpl" autowire="byName"/>

       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
              <property name="driverClassName" value="com.mysql.jdbc.Driver" />
              <property name="url" value="jdbc:mysql://localhost:3306/Lab2" />
              <property name="username" value="root" />
              <property name="password" value="root" />
       </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="" />
    </bean>
    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
        <property name="sessionFactory" ref="sessionFactory" />
            </bean>
      <context:component-scan base-package="edu.sjsu.cmpe275.lab2.service" />
   <!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> -->
</beans>

2 个答案:

答案 0 :(得分:0)

我通过将hibernate.cfg.xml放在WEB-INF的根目录中来修复错误。它位于webapp文件夹中并解决了我的问题。

答案 1 :(得分:0)

您已经定义了&#34; DataSource&#34; bean,我猜你不需要configLocation属性。

您可以参考以下链接获取更多选项http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example