0
votes

Hello guys i have been playing around with spring mvc and hibernate. But now i am getting an error when trying to connect both hibernate and spring with XML configuration. I get this error below:

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

I created my webapp folder in src/main. in my webapp folder i created WEB-INF folder. And WEB-INF folder contained files: service.xml, dispatcher-servlet.xml. Now for my hibernate.cfg.xml, it was placed in src/main folder structure. But when i try to add it in the service.xml upon loading the dispatcher, my hibernate.cfg.xml seems not to be found. Does anyone know why? This is where i am having trouble:

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

I can't seem to find the value of configLocation. Has anyone encountered this issue.

Below is my web.xml configuration:

<?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>
2
Why do you create service.xml? I think you can access it using dispatcher-servlet.xmlBurak Keceli
how would i access it though dispatcher-servel.xml. I created service.xml because its where i am implementing my business logic. I was learning to break it up as i didn't want it all in one xml configuration fileEI-01
I actually fixed my error. I placed my hibernate.cfg.xml file in the root directory that is webapp folderEI-01

2 Answers

0
votes

I fixed my error by placing hibernate.cfg.xmlin the root directory of WEB-INF. That is placed in webapp folder and solved my issue.

0
votes

you have already defined "DataSource" bean, I guess you don't need to configLocation property.

you can refer below link for more options http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example