繁体   English   中英

Hibernate和Spring XML配置出现问题

[英]Trouble with hibernate and spring xml configuration

大家好,我一直在玩Spring MVC和Hibernate。 但是现在当我尝试使用XML配置连接冬眠和弹簧时遇到错误。 我在下面收到此错误:

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>

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

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

您已经定义了“ DataSource” bean,我想您不需要configLocation属性。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM