Search This Blog

Sunday, February 26, 2017

Spring

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
injection.

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


Steps:-
++++++++

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 xxxFormBean.java
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="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="myDataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <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>

    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="mappingResources">
            <list>
                <value>./Employee.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.HSQLDialect
            </value>
        </property>
    </bean>

    <bean id="hibernateTemplate"
        class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="mySessionFactory" />
        </property>
    </bean>

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


</beans>


Employee.hbm.xml
~~~~~~~~~~~~~~~~~~~

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

    <property name="name">
        <column name="name"/>
    </property>
    <property name="age">
        <column name="age"/>
    </property>
    <property name="salary">
        <column name="salary"/>
    </property>
</class>
</hibernate-mapping>


Employee.java
~~~~~~~~~~~~~

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){
        this.id = id;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.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;
    }
}


EmployeeDao.java
~~~~~~~~~~~~~~~~~~~

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 );
    }
}


SpringTest.java
~~~~~~~~~~~~~~~~~

package spring.hibernate;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

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 );
    }
}

drvijayy2k2@gmail.com

No comments:

Hit Counter


View My Stats