Tech I Enjoy Logo

Custom Search




  




Spring Framework Interview Questions and Answer

Spring Singleton and GOF Singleton Pattern differences
 
Spring Framework and Hibernate example - Part 1

Spring Framework and Hibernate example - Part 2

Spring Framework and Remoting and Session Proxy example
 
Spring Framework and Local and Session Proxy example
 
Spring Framework DOA support Hibernate example

Spring Framework and Hibernate DOA support discussed with an example

Spring Framework with Hibernate current session context

Spring Framework with Tiles 2.1.4 - example

Spring Framework and Web service Module example
 
Spring Web Module example

Spring WS example
 



When it comes to find a solution for integrating various Frameworks
being used in a medium to large web based enterprise applicaton,
I prefer Spring Frameworks, and all its support classes for
working with Hibernate, JDBC, Transaction, Security, Spring MVC
and many more.

Let us start with a very simple example demonstrating usage of
Spring's MVC capability.

This example is for those who want a very simple to start with
code for trying out one of the way to use Spring MVC using
Spring's dependency injection.

We will have a simple URL as
http://localhost:8080/spring-example/test.do  pasted in the URL
box of a browser, and on submitting this URL, we shall see the
actual example controller getting invoked and then there is a
simple JSP view page showing a single liner on screen.

Various parts/files of this example are as follows:

  • SpringFramework 1.2.8
  • web.xml descriptor file with the Spring context loader listener and Spring's Dispatcher Servlet configuration
  • <web-app>
     <listener>
      <listener-class>
       org.springframework.web.context.ContextLoaderListener
      </listener-class>
     </listener>
     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/applicationContext.xml
      </param-value>
      </context-param>
     <servlet>
      <servlet-name>example</servlet-name>
      <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
     </servlet>
     <servlet-mapping>
      <servlet-name>example</servlet-name>
      <url-pattern>*.do</url-pattern>
     </servlet-mapping>
    </web-app>
    
  • When the Dispatcher Servlet name is defined as example, so we have to define the controller and URL specific mapping in a XML file with a name as example-servlet.xml, in the WEB-INF folder.
  •  <beans>
      <bean id="defaultHandlerMapping"
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="urlMap">
        <map>
         <entry key="test.do" value="example-controller"/>
        </map>
       </property>
      </bean>
    
     <bean name="example-controller"
           class="example.controller.ExampleController">
      <property name="exampleServiceImpl"
                ref="exampleService"/>
     </bean>
    </beans>
    
  • applicationContext.xml file has the DAO, Service and DataSource specific configuration for doing dependency injection so as to inject the final loaded service imple to the example controller
  •   <beans>
      <!-- for this example, I have used a simple Drivermanager
           data source, instead a connection pool should be used -->
      <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="test"/>
        <property name="password" value="test"/>
      </bean>
      <bean id="exampleDAO" class="example.dao.ExampleDAO">
        <property name="dataSource" ref="dataSource"/>
      </bean>
      <bean id="exampleService" class="example.service.ExampleServiceImpl">
        <property name="exampleDao" ref="exampleDAO"/>
      </bean>
    </beans>
    
  • ExampleDAO.java
  • package example.dao;
    import javax.sql.DataSource;
    
    public class ExampleDAO {
      private DataSource dataSource;
      public void setDataSource(DataSource ds) {
    	  dataSource = ds;
      }
    }
    
  • ExampleServiceImpl.java
  • package example.service;
    import example.dao.*;
    
    public class ExampleServiceImpl {
      private ExampleDAO exampleDao;
      public void setExampleDao(ExampleDAO exDao) {
    	  exampleDao = exDao;
      }
    }
    
  • ExampleController.java
  • package example.controller;
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.servlet.ModelAndView;
    import javax.servlet.http.*;
    import example.service.*;
    
    public class ExampleController implements Controller
    {
    	private ExampleServiceImpl exampleServiceImpl;
    	public void setExampleServiceImpl(ExampleServiceImpl exServ) {
    		exampleServiceImpl = exServ;
    	}
        public ModelAndView handleRequest(HttpServletRequest request,
    		                              HttpServletResponse response)
    		                              throws Exception {
    		System.out.println("controller called");
    		return new ModelAndView("/test.jsp");
    	}
    }
    
  • test.jsp
  • <%
     out.println("This is a test page");
    %>
    
    This example is working in my local development environment.
    Spring Batch Framework Example :
    Spring Batch Framework Example source code
     and Step by step code walkthrough
    Example using Spring DAO with Hibernate :
    Example using Spring DAO with Hibernate Framework.
    Spring Framework Local Stateless Session bean :
    Spring Framework Local Stateless Session bean with
    example source code explained.
    Spring Framework Web Example :
    Spring Framework Web Example source code Explained.
    Interview Questions with answer on Spring Framework :
    Interview Questions with answer on Spring Framework.
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework Part 1.
    Spring Framework MVC and Tiles :
    Spring Framework MVC and Tiles with Example 
    source code explained.
    Example using Spring DAO with Hibernate :
    Example using Spring DAO with Hibernate Framework Part 2.
    Spring Framework Remote Session Bean :
    Spring Framework Example source code of
    Remote Session Bean.
    Spring Framework Web Example and WSDL :
    Spring Framework Web Example source code
    with WSDL.Step by step code walkthrough
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework Part 2.
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework and use
    Current Session Context.
    Spring Framework Singleton Example :
    Spring Framework Singleton Example source code 
    compared with GOF Singleton.
    Spring Framework Example :
    Spring Framework Example source code Explained.
    Step by step code walkthrough
    Spring Framework Web Example :
    Spring Framework Web Example source code Explained.


    References :
    Tags: spring dao hibernate example
    Tags: spring hibernate dao example
    Tags: spring hibernate example 2
    Tags: spring hibernate example current session context
    Tags: spring hibernate example
    Tags: Spring Interview Questions Answer
    Tags: Spring Local SLBean
    Tags: Spring MVC Tiles2
    Tags: Spring Remote SLBean
    Tags: Spring Singleton GOF Singleton Difference
    Tags: spring web example
    Tags: spring ws example
    Tags: Spring
    Tags: springws example wsdl
    

    
    
    DISCLAIMER :
    The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
    techienjoy.com is not liable for any negative consequences that may result/arise from 
    implementing directly/indirectly any information covered in these pages/articles/tutorials.
    
    All contents of this site is/are written and provided on an "AS IS" basis,
    without WARRANTIES or conditions of any kind, either express or implied, including, without
    limitation, merchantability, or fitness for a particular purpose. You are solely responsible
    for determining the appropriateness of using or refering this and assume any risks associated
    with this.
    
    In spite of all precautions taken to avoid any typo in these pages, there might be some 
    issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
    extends sincerest apologies to all our visitors for the same.
    
    
    

    Android Examples

    © Copyright 2010, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions