Search This Blog

Sunday, February 26, 2017


Spring focuses around providing a way to manage your business objects.

Spring is an application framework in which spring MVC is
one of the modules of spring framework. Spring uses the
concept of IOC/DI where the objects are not hard coded in
java but they are injected using construtor/setter

Spring is an ideal framework for test driven projects

Applications built using Spring are very easy to unit tes

Spring can make the use of EJB an implementation choice, rather than the determinant of application architecture. You can choose to implement business interfaces as POJOs or local EJBs without affecting calling code

Spring provides a consistent framework for data access

this consistency in the Spring approach to JDBC, JMS, JavaMail, JNDI and many other important APIs.

Spring's main aim is to make J2EE easier to use and promote good programming practice

no logging packages in Spring, no connection pools, no distributed transaction coordinator

Spring container manages relationships between objects

Dependency Injection is a form of IoC that removes explicit dependence on container API, two major flavors of Dependency Injection are Setter Injection (injection via JavaBean setters); and Constructor Injection (injection via constructor arguments)

highly configurable MVC web framework

Spring's MVC model is most similar to that of Struts, although it is not derived from Struts

Spring Controller is similar to a Struts Action in that it is a multithreaded service object

Spring provides a very clean division between controllers, JavaBean models, and views

Spring MVC is truly view-agnostic. You don't get pushed to use JSP if you don't want to; you can use Velocity, XLST or other view technologies

custom view mechanism - for example, your own templating language - you can easily implement the Spring View interface to integrate it


1. Open MyEclipse Editor [or] any editor
2. Create a new Project [java project]
3. Add neccessary Jar Files in lib [ spring, hibernate core 3.1, jre 1.5+ ]
4. Create your DB
5. Create spring-hibernate.xml
6. Create xxxFormBean.hbm.xml
7. Write a
8. Write a DAO
9. Write a program to call

Sample DB Create [MYSQL]

create database springdb;
use springdb;

create table employee ( id int(10) unsigned NOT NULL auto_increment primary key, name varchar(20), age int(3), salary numeric(10,2));

insert into `employee`(`id`,`name`,`age`,`salary`) values ( NULL,'vijay','25','10000');
insert into `employee`(`id`,`name`,`age`,`salary`) values ( NULL,'kumar','20','5000');

spring-hibernate.xml  [save inside src folder]

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""

    <bean id="myDataSource"
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
        <property name="username" value="root" />
        <property name="password" value="root" />

    <bean id="mySessionFactory"
        <property name="dataSource" ref="myDataSource" />
        <property name="mappingResources">
        <property name="hibernateProperties">

    <bean id="hibernateTemplate"
        <property name="sessionFactory">
            <ref bean="mySessionFactory" />

    <bean id="employeeDao" class="spring.hibernate.EmployeeDao">
        <property name="hibernateTemplate">
            <ref bean="hibernateTemplate" />



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="spring.hibernate.Employee" table="employee" lazy="false">
    <id name="id" column="id">
        <generator class="increment"/>

    <property name="name">
        <column name="name"/>
    <property name="age">
        <column name="age"/>
    <property name="salary">
        <column name="salary"/>

package spring.hibernate;

public class Employee {

    private int id;
    private String name;
    private int age;
    private double salary;

    public Employee() {

    public int getId(){
        return id;

    public void setId(int id){ = id;

    public String getName(){
        return name;

    public void setName(String name){ = name;

    public int getAge(){
        return age;

    public void setAge(int age){
        this.age = age;

    public double getSalary(){
        return salary;

    public void setSalary(double salary){
        this.salary = salary;

    public String toString(){
        return "Id = " + id + ", Name = " + name + ", Age = "
            + age + ", Salary = " + salary;

package spring.hibernate;

import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import spring.hibernate.Employee;

public class EmployeeDao

    private HibernateTemplate    hibernateTemplate;

    public void setHibernateTemplate ( HibernateTemplate hibernateTemplate )
        this.hibernateTemplate = hibernateTemplate;

    public HibernateTemplate getHibernateTemplate ()
        return hibernateTemplate;

    public Employee getEmployee ( final int id )
        HibernateCallback callback = new HibernateCallback () {
            public Object doInHibernate ( Session session ) throws HibernateException, SQLException
                return session.load ( Employee.class, id );
        return (Employee) hibernateTemplate.execute ( callback );

    public void saveOrUpdate ( final Employee employee )
        HibernateCallback callback = new HibernateCallback () {
            public Object doInHibernate ( Session session ) throws HibernateException, SQLException
                session.saveOrUpdate ( employee );
                return null;
        hibernateTemplate.execute ( callback );

package spring.hibernate;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class SpringTest

    public static void main ( String [] args )

        Resource resource = new FileSystemResource ( "bin/spring-hibernate.xml" );
        BeanFactory factory = new XmlBeanFactory ( resource );

        Employee employee = new Employee ();
        employee.setId ( 123 );
        employee.setName ( "ABC" );
        employee.setAge ( 20 );
        employee.setSalary ( 15000.00d );

        EmployeeDao employeeDao = (EmployeeDao) factory.getBean ( "employeeDao" );
        //employeeDao.saveOrUpdate ( employee );

        Employee empResult = employeeDao.getEmployee ( 1 );
        System.out.println ( empResult );

M2_REPO classpath Issues In Eclipse IDE

1. Add M2_REPO Manually

Define and add M2_REPO classpath variable manually into Eclipse IDE. Follow below steps :
  1. Eclipse IDE, menu bar
  2. Select Window > Preferences
  3. Select Java > Build Path > Classpath Variables
  4. Click on the new button > defined a new M2_REPO variable and point it to your local Maven repository
  5. Done.


1. Delete your workspace
2. Import your projects with "Existing Maven Project"

NOTE: this will create M2_repo in your classpath. no worries. 

Hit Counter

View My Stats