Spring 4 MVC Hibernate JPA XML Config Example

Spring 4 MVC Hibernate JPA XML Config Example helps developers who are looking for spring XML based configuration to initiate MVC and root application context. The below tutorial explains to the same example with  java annotations and we will use same code base and will create XML context files.

Spring 4 MVC Hibernate JPA Java Config Tutorial

Spring 4 MVC Hibernate JPA Java Config Tutorial helps developers who are looking for spring java based configuration to initiate MVC and database  application context.

READ MORE

Spring MVC CRUD Example using XML config:

This tutorial is more like Spring MVC CRUD with transactions with below functionalities:

  • Displaying Customer List
  • Adding new customer
  • Editing customer
  • Deleting customer.

Spring 4 MVC CRUD Tutorial

Installations:

  • Eclipse
  • Tomcat 8
  • Spring 4
  • Spring ORM and Transaction Management
  • Hibernate 5
  • Bootstrap
  • Maven 4
  1. Lets create new maven project in eclipse: File -> New -> Project -> Maven -> Maven Project and click on Next
  2. Give group Id as “org.javasavvy.spring-jpa-xml” , artifact as “spring-mvc-jpa-xml” and packaging type as “war
  3. Update pom.xml with below dependencies:
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>org.javasavvy.spring-jpa-xml</groupId>
       <artifactId>spring-mvc-jpa-xml</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>war</packaging>
       <name>spring-mvc-jpa-xml</name>
       <description>spring-mvc-jpa-xml</description>
       <properties>
       <spring.version>4.3.6.RELEASE</spring.version>
       <hibernate.version>5.0.5.Final</hibernate.version>
       <mysql.version>5.1.31</mysql.version>
       <joda-time.version>2.3</joda-time.version>
       <mail.version>1.4.1</mail.version>
       <log4j.version>1.2.17</log4j.version>
       </properties>
       <build>
       <finalName>spring-mvc-jpa-xml</finalName>
       <directory>target</directory>
       <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
       <outputDirectory>target/classes</outputDirectory>
       <pluginManagement>
       <plugins>
       <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-war-plugin</artifactId>
       <version>3.0.0</version>
       <configuration>
       <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
       <resources>
       <resource>
       <directory>${project.basedir}/src/main/resources</directory>
       <filtering>false</filtering>
       </resource>
       </resources>
       <warName>spring-mvc-tutorial</warName>
       <failOnMissingWebXml>false</failOnMissingWebXml>
       </configuration>
       </plugin>
       <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>3.2</version>
       <configuration>
       <source>1.8</source>
       <target>1.8</target>
       </configuration>
       </plugin>
       
       </plugins>
       </pluginManagement>
       </build>
       <dependencies>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-beans</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-tx</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-jdbc</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-orm</artifactId>
       <version>${spring.version}</version>
       </dependency>
       <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
       </dependency>
       <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.1.0</version>
       </dependency>
       <dependency>
       <groupId>javax.servlet.jsp</groupId>
       <artifactId>javax.servlet.jsp-api</artifactId>
       <version>2.3.1</version>
       </dependency>
       <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>${hibernate.version}</version>
       </dependency>
       <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-c3p0</artifactId>
       <version>${hibernate.version}</version>
       </dependency>
       <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-entitymanager</artifactId>
       <version>${hibernate.version}</version>
       </dependency>
       <dependency>
       <groupId>javax.transaction</groupId>
       <artifactId>jta</artifactId>
       <version>1.1</version>
       </dependency>
       <!-- MySQL -->
       <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>${mysql.version}</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-mapper-asl</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-core-asl</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-xc</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-jaxrs</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>${log4j.version}</version>
       </dependency>
       </dependencies>
      </project>
  4.  Execute maven install and create the below project structure. In the Java config example, we created packages for app and mvc config, but here it is not required as we are moving configuration to xml files.Spring XML Project Structure
  5. Create applicationContext-jdbc.xml:
    • <context:annotation-config /> will enable annotations @autowired,@required
    • component scan will scan the given base packages and created bean that are annotated with @Component,@Cervice,@Repository
    •  tx-annotation driven will enable the declaractive transactions and will scan for classes that are annotated with @Transaction
    • Configure JPA Transaction manager and LocalContainerEntityManagerFactoryBean in this xml. for more details check the Java config example.
    • <?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"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
            <context:annotation-config />
            <context:component-scan base-package="org.javasavvy.demo.services" />
            <context:component-scan base-package="org.javasavvy.demo.dao" />
            <tx:annotation-driven transaction-manager="transactionManager" />
      
           <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
              <property name="url" value="jdbc:mysql://localhost:3306/javasavvy" />
               <property name="username" value="root" />
               <property name="password" value="root" />
       </bean>
       <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
       </bean>
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
               <property name="dataSource" ref="dataSource" />
               <property name="persistenceUnitName" value="spring-jpa-unit" />
               <property name="jpaVendorAdapter"> 
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
               </property>
              <property name="jpaProperties">
                 <props>
                     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                     <prop key="hibernate.show_sql">true</prop>
                     <prop key="hibernate.format_sql">false</prop>
                      <prop key="hibernate.hbm2ddl.auto">update</prop>
               </props>
               </property>
       </bean>
         <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
      </beans>
  6. Create mvcConfig.xml file in WEB-INF/config. In this context file,
    • mvc-annotation drivcen element will enable MVC annotation @RequestMapping,@Controller etc
    • component scan package will auto create bean that are annotated with @Controller
    • Define view resolver bean configuraiton also in this file
    • <?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:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
             <context:annotation-config /> 
             <mvc:annotation-driven />
             <context:component-scan base-package="org.javasavvy.demo.ui.controller" />
             <mvc:resources mapping="/resources/**" location="/resources/" />
             <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
             <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
      
            <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                 <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
           </bean>
           <mvc:view-controller path="/home.action" view-name="home"></mvc:view-controller>
           <mvc:view-controller path="/customer/customerForm.action" view-name="addCustomer"></mvc:view-controller>
      </beans>
  7. create web.xml file to define
    • root application context listener configuration
    •  dispatcher servlet config
    •  <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee 
       http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
       <welcome-file-list>
           <welcome-file>index.html</welcome-file>
            <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
       <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
       <context-param>
            <param-name>contextConfigLocation</param-name>
           <param-value>classpath*:applicationContext-jdbc.xml</param-value>
       </context-param>
       <servlet>
            <servlet-name>spring-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/config/mvcConfig.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
       </servlet>
         <servlet-mapping>
              <servlet-name>spring-dispatcher</servlet-name>
              <url-pattern>*.action</url-pattern>
          </servlet-mapping>
       </web-app>
  8. for remaining classes, check the java configuration tutorial or click here to download the complete code:

 

Download Spring MVC JPA XML Example Code.

 

2 Comments

  1. Alejandro Barrero

    Cannot run anything on the server. http://localhost:8080/spring-mvc-tutorial/ doesn’t work

    Reply
  2. Akash

    java.lang.IllegalArgumentException: No persistence unit with name ‘spring-jpa-unit’ found

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *