Simple Login Application with Spring Without database by R4R Team

In Spring We can Integrate with the Hibernate. In the Hibernate Framework we provide all the database configuration with hibernate.cfg.xml file. But Now Here are going to integrate the simple example using Hibernate. In that kind of application we don't need to configure hibernate.cfg.xml file. Because it need at the time of the Use of database. We all know that hibernate is a ORM tool which map object of our java code to the database. But Here we will provide the applicationContex.xml file.

Advantages of Spring Framework with Hibernate: 

We have the some basic advantages with the Hibernate and Spring which is given below-

Spring Framework provides Hibernate Template class so we don't need to follow the step which is create Configuration, BuildSessionFactory, Session, Beginning and committing transaction etc. So that it save the a lot of code.

Now Here Below we have given the simple example of the spring using Hibernate without database. For creating the application we need to follow the some basic step with we can create a simple application. Those step are given below:

Step 1. Take a new project in the netbeans. For this go in the File Menu Wizard -> New Project -> Select Java -> Web -> Put the required Name of the required field -> Click on Next -> Select server on which you want to deploy your application -> click on next -> Now Select to Spring Framework -> Click on Finish.

When you click on the finish then you will find the some pre opened file like as under WEB-IINF folder ApplicationContext.xml and another is dispatcher-servlet.xml these are some pre attatched file which will be added when we take a new project of the spring.

Step 2. Now first of all we have to create three package in the src folder. For doing this we have to go in the file menu wizard -> then click on the new file -> search to java -> and opposite side you will see the at the bottom of the page java package -> click on it and now provide the name on the required filed -> and then finish.

Step 3. Now we need to make a java class in which we will define a simple code of the java class. For creating a java class we need to go in the file menu and click on the new file -> then you will java class option in the table of the new file wizard click on it and put the name on the required field. and then finish.

A.java

package my;
public class A {
    private String msg1;
    private String msg2;
    public String getMsg1() {
        return msg1;
    }
    public void setMsg1(String msg1) {
        this.msg1 = msg1;
    }
    public String getMsg2() {
        return msg2;
    }
    public void setMsg2(String msg2) {
        this.msg2 = msg2;
    }
}

B.java

package my;
public class B {
    private String msg1;
    private String msg2;
    private String msg3;
    public String getMsg1() {
        return msg1;
    }
    public void setMsg1(String msg1) {
        this.msg1 = msg1;
    }
    public String getMsg2() {
        return msg2;
    }
    public void setMsg2(String msg2) {
        this.msg2 = msg2;
    }
    public String getMsg3() {
        return msg3;
    }
    public void setMsg3(String msg3) {
        this.msg3 = msg3;
    }
}

BeanAction.java

package my;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class BeanAction implements BeanPostProcessor
{
  static int ctr=0;  
  @Override
  public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
    System.out.println("After Bean initialize of - "+name); 
    return bean;
  }
  @Override
  public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
      ctr++;
      System.out.println(ctr+". beans initialized , this is Before Bean initialize of - "+name); 
      return bean;
    
  }
 
    
}

Hello.java

package my;
public class Hello {
   private String msg;
   Hello()
   {
     msg = "initiailzed with Constructor calling";       
     System.out.println("Constructor calling");
   }
   public void init()
   {
     System.out.println("Inside init() method");  
   }
   public void destroy()
   {
     System.out.println("Inside destroy method");  
   }
   public String getMsg() {
      System.out.println("getter method");  
       return msg;
   }
   public void setMsg(String msg) {
       this.msg = msg;
       System.out.println("setter method");  
   }
}

MyOuter.java

package my;
public class MyOuter 
{
   private A inner;
   public A getInner() {
      return inner;
   }
   public void setInner(A inner) {
      this.inner = inner;
   }
   public String callInner()
   {
      inner.setMsg1("I m inside inner msg1 method");
      return inner.getMsg1();
   }
}

Step 4. Now we will create a spring config file which will under the spring category in the new file menu wizard. It will be stand on the src folder.

Config.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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
">
<bean id="ravi" class="my.Hello" init-method="init" destroy-method="destroy" />
<bean class="my.BeanAction" />
<bean id="parent" class="my.A" abstract="true">
  <property name="msg1" value="inside Parent class msg1" />
  <property name="msg2" value="inside Parent class msg2" />
</bean>
<bean id="child" class="my.B" parent="parent">
  <property name="msg1" value="child class msg1" />
  <property name="msg3" value="child class msg3" />
</bean>
<bean id="outer" class="my.MyOuter">
  <property name="inner">
    <bean id="inner" class="my.A" />
  </property>
</bean>
</beans>

Step 5. In the end of the project work we need to create some jsp page for the front end application. Which can a user see.

index.jsp

<%@page import="my.B"%>
<%@page import="my.A"%>
<%@page import="my.Hello"%>
<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<body bgcolor="lightyellow">
<pre><h3>
<%
   ApplicationContext ctx = new ClassPathXmlApplicationContext("Config.xml");
   Hello o = (Hello)ctx.getBean("ravi");
   out.println(o.getMsg());
   o.setMsg("setter method initialization");
   out.println(o.getMsg());
   out.println("<hr size=3 color=orange>");
   //A n = (A)ctx.getBean("parent");
  // out.println(n.getMsg1());
  // out.println(n.getMsg2());
  // out.println("<hr size=3 color=orange>");
   B k = (B)ctx.getBean("child");
   out.println(k.getMsg1());
   out.println(k.getMsg2());
   out.println(k.getMsg3());
   my.MyOuter s = (my.MyOuter)ctx.getBean("outer");
   out.println(s.callInner());
%>

Hello.jsp

<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<body bgcolor="black" text="white"><h2><pre>
<%
   ApplicationContext ctx = new ClassPathXmlApplicationContext("HelloXML.xml");
   my.Hello h = (my.Hello)ctx.getBean("ravi");
   out.println(h.getMsg()); 
%>
<a href="Hello.jsp">Next</a>

Step 6. Here we have to do nothing in the application attatched file ApplicationContex.xml file and  dispatcher-servlet.xml.

Step 7. Now Select to project node and right click on it and run the application.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!