Home / Spring Framework / Hibernate – Spring Framework Configuration

Hibernate – Spring Framework Configuration

Steps to Configure Spring Framework

Initially, we configure all spring related setting in web.xml because every request will come to web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.abc</url-pattern>
  </servlet-mapping>
  
</web-app>

Name : dispatcher

Server will find the name start with “dispatcher” and ends with “servlet” for

dispacher-servlet.xml contain all sprint related configureation [packages required for application like beans, hibernatetemplate]


<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">

<context:component-scan base-package="com.fareed"></context:component-scan>


<bean id="basicdatasource" class="org.apache.commons.dbcp2.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
   <property name="url" value="jdbc:mysql://localhost:3306/springs" ></property>
   <property name="username" value="root"></property>
   <property name="password" value=""></property>
</bean>

<mvc:annotation-driven></mvc:annotation-driven>


<bean id="sf" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="basicdatasource"></property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

</props>
	

</property>

<property name="packagesToScan" value="com.fareed.pojo">




</property>

</bean>


<bean id="hibernatetempalte" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>


</bean>

<bean id="transactionManager" class=" org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sf"></property>

</bean>



<tx:annotation-driven></tx:annotation-driven>

<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />

</bean>






</beans>		



package com.fareed.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tbl_student")
public class DBConnection {
	
	
	private int id;
	private String name;
	@Id
	@GeneratedValue
	@Column(name="id")
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
	@Column(name="name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	

}

package com.fareed.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.DispatcherServlet;

import com.fareed.pojo.DBConnection;
import com.fareed.service.TestService;

@Controller

public class TestController {
	
	@Autowired
	TestService ts;
	
	@Autowired
	HibernateTemplate ht;
	
	@RequestMapping("/testfareed")
	public void test(HttpServletResponse res) throws IOException
	{
		
		
		//System.out.println("my First TestController");
		
		/*PrintWriter pw =res.getWriter();
		pw.write("Namaste");*/
		
		/*TestService ts = new TestService();*/
		ts.service(res);
	}
	
	
	@RequestMapping("/dbtest")
	public void dbtest() throws IOException
	{
		ht.get(DBConnection.class, 1);
		
	}

}

About Mohammad Fareed

Software Engineer @ Tekzenit.

Check Also

Spring Framework : Framework of Frameworks

hosted on SourceForge since Jan 2003 First Release on Feb 2003 Version 4.3 Spring is …

Leave a Reply

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