Spring Tutorials

Spring Tutorial

Introduction of Spring Framework

Advantages and Disadvantages of Spring Framework

Features of Spring Framework

Basic concept of Spring Framework

Architecture of Spring Framework

Modules of Spring Framework

Goal of Spring Framework

Create Application of Spring without any IDE

Create Application of Spring in Eclipse IDE

Create Application of Spring in MYEclipse IDE

Important JAR of Spring Framework

IOC container in Spring Framework

Bean and Applicationcontext container in Spring Framework

Example of Spring BeanFactory Container in Spring Framework

Example of Spring ApplicationContext Container in Spring Framework

Bean in Spring Framework

Lifecycle of Bean in Spring Framework

Scope of Bean in Spring Framework

Autowiring in Spring Framework

Dependency Injection in Spring Framework

Constructor-based Dependency Injection Example in Spring Framework

Setter-based Dependency Injection Example in Spring Framework

Introduction of AOP in Spring Framework

Core concept and Goal of AOP in Spring Framework

AOP Proxies in Spring framework

XML Schema Based AOP in Spring Framework

AOP Xml based Configuration Example in Spring Framework

Example of Declaring AOP Advices in Spring Framework

AOP AspectJ Annotation with Example in Spring Framework

Declaring an aspect and pointcut using Annotation in Spring Framework

Declaring AOP Advices using Annotation Example in Spring Framework

DAO support in Spring Framework

Introduction of Spring JDBC Framework in Spring Framework

Introduction of Spring JdbcTemplate in Spring Framework

Example of Spring JdbcTemplate class in Spring framework

Example of Executing SQL statements in Spring Framework

Example of Executing DDL statements in Spring Framework

SQL Stored Procedure in Spring Framework

Example of NamedParameterJdbcTemplate in Spring Framework

Example of RowMapperJdbcTemplate in Spring Framework

Introduction of ORM Framework in Spring Framework

Integration of Spring with Hibernate in Spring Framework

Integration of Spring with JPA in Spring Framework

Introduction of Spring Expression Language (SpEL) in Spring Framework

Example of Spring Expression Language (SpEL) in Spring framework

Example of Spring EL in XML and Annotation in Spring Framework

Language Reference with SpEL in Spring Framework

Operators in Spring Expression Language(SpEL) in Spring Framework

Variable in Spring Expression Language(SpEL) in Spring Framework

Introduction of Spring Framework Transaction Management in Spring Framework

Spring Framework Transaction Abstraction

Spring Declarative Transaction Management in Spring Framework

Spring Programmatic Transaction Management in Spring Framework

Introduction of Spring OXM (Object XML Mapping) in Spring Framework

Integration of Spring with Jaxb Example in Spring framework

Example of Spring with Xstream in Spring Framework

Example of Spring with Castor in Spring Framework

Remote Method Invocation(RMI) in Spring Framework

Spring and RMI Integration with Example in Spring Framework

Example of Spring and Httpinvoker in Spring Framework

Example of Spring and Hessian in Spring Framework

Integration of Spring with JMS in Spring Framework

Introduction of Webservice in Spring Framework

Spring Web Services in Spring Framework

Web Services with Jax-WS in Spring framework

Exposing and Exporting servlet-based Web Services using JAX-WS in Spring Framework

Accessing Web Services using JAX-WS in Spring Framework

Introduction of JMS in Spring Framework

JMS Messaging Models in Spring Framework

Using Spring JMS in Spring Framework

Sending and Receiving a Message Using JMS API in Spring Framework

Introduction of JMX (Java Management Extension) in Spring Framework

Integrating Beans with JMX in Spring Framework

Creating a MBeanServer in Spring Framework

Introduction Java Mail with Spring in Spring Framework

Example of Java Mail with spring in Spring Framework

Introduction of EJB(Enterprise JavaBeans) in Spring Framework

Introduction of EJB(Enterprise JavaBeans) Integration in Spring Framework

Integration of Spring With Struts 2 Example in Spring Framework

Spring MVC

Spring MVC

adplus-dvertising
Spring Remoting with Httpinvoker Example
Previous Home Next

Spring HTTP invokers use the standard Java serialization mechanism to expose services through HTTP. Spring uses either the standard facilities provided by the JDK or Apache HttpComponents to perform HTTP calls. The main classes of Spring Http invoker are as follows:

org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter: This is a servlet API based Http request handler. It is used to export the remote services. It takes in a service property that is the service to be exported and a ServiceInterface that specifies the interface that the service is tied to.

org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean: This is a proxy factory for creating http invoker proxies. It has a serviceUrl property that must be an http url exposing an http invoker service. This class serializes the objects that are sent to remote services and deserializes the objects back.

Technologies used to run this Application.
  1. Spring 3.0
  2. Eclipse IDE
  3. JDK 1.6

Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

Step1. Now you create Dyanamic Web Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Dyanamic Web Project wizard from the wizard list. Now name your project as SpringRemotingWithHttpinvoker using the wizard window as given below.

Add spring jar files

Step2. Now add spring jar in your project, To do this, right click on your project in Lib folder, name SpringRemotingWithHttpinvoker and copy the jar as given below. then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path window as given below. To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish.

  1. com.springsource.org.apache.commons.fileupload-1.2.0.jar
  2. com.springsource.org.apache.commons.httpclient-3.1.0.jar
  3. com.springsource.org.apache.commons.logging-1.1.1.jar
  4. com.springsource.org.apache.log4j-1.2.15.jar
  5. com.springsource.org.codehaus.jackson.mapper-1.0.0.jar
  6. jmxtools-1.2.1.jar
  7. org.springframework.asm-3.1.0.
  8. org.springframework.beans-3.1.0.
  9. org.springframework.context.support-3.1.0.
  10. org.springframework.context-3.1.0.
  11. org.springframework.core-3.1.0.
  12. spring-expression-3.1.0.RELEASE.jar
  13. org.springframework.oxm-3.0.1.RELEASE-A.jar
  14. org.springframework.web.portlet-3.0.1.RELEASE-A.jar
  15. org.springframework.web.servlet-3.0.1.RELEASE-A.jar
  16. org.springframework.web.struts-3.0.1.RELEASE-A.jar
  17. org.springframework.web-3.0.1.RELEASE-A.jar
  18. com.springsource.com.caucho-3.2.1.jar
  19. com.springsource.net.sf.cglib-2.2.0.jar
  20. com.springsource.org.aopalliance-1.0.0.jar
  21. com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  22. org.springframework.aop-3.0.1.RELEASE-A.jar
  23. org.springframework.aspects-3.0.1.RELEASE-A.jar
  24. org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar
  25. org.springframework.instrument-3.0.1.RELEASE-A.jar
Create a Java interface.

Step3. Now you create simply java file with any name. First we need to create a package with any name like com.r4r. To do this, Right click on src in package explorer section and follow the option : New -> Package. and then Right click on package(com.r4r) - New - interface - Write the interface name e.g. Square - finish. Following is the content of Interface Square.java containing one sq(square) metod. Write the following code:

package com.r4r;
public interface Square 
{
 int sq(int number);

}

Step4. Following is the content of java class SquareImpl.java. This class provides the implementation of Square interface.

package com.r4r;
public class SquareImpl implements Square{
	@Override
	public int sq(int number) {
	return number*number;
	} }

Step5. Following is the content of xml file web.xml. In this xml file we define the DispatcherServlet as the front controller. If any request is followed by .http extension, it will be forwarded to DispatcherServlet.

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

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>httpInvoker</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>httpInvoker</servlet-name>
<url-pattern>*.http</url-pattern>
</servlet-mapping>

</web-app>

Step6. Following is the content of xml file beans.xml. In this xml file, we define bean for HttpInvokerProxyFactoryBean. now we need to define two properties of this class.

  1. serviceUrl
  2. serviceInterface
<?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.xsd">

<bean id="squareBean" 
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" 
 value="http://localhost:8080/SpringRemotingWithHttpinvoker/Square.http">
</property>
<property name="serviceInterface" value="com.r4r.Square"></property>
</bean>
</beans>

Step7. Following is the content of xml file httpInvoker-servlet.xml. It defines bean for SquareImpl and HttpInvokerServiceExporter. this file must be inside the WEB-INF 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.xsd">

<bean id="squareBean" class="com.r4r.SquareImpl"></bean>
<bean name="/Square.http" 
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="squareBean"></property>
<property name="serviceInterface" value="com.r4r.Square"></property>
</bean>
</beans>

Step8. Following is the content of java class Test.java. It is simply getting the instance of Square and call the method.

package com.r4r;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class Test {  
    public static int getSq(int number){  
        ApplicationContext context = 
			new ClassPathXmlApplicationContext("beans.xml");  
        Square square = (Square)context.getBean("squareBean");  
        return square.sq(number);  
    }  
}  

Step9. Following is the content of Jsp file index.jsp.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Square of the Number</title>
</head>
<body>
<form action="success.jsp">  
Enter Number: <input type="text" name="number"/>  
<input type="submit" value="Square" />  
</form> 
</body>
</html>

Step10. Following is the content of Jsp file success.jsp.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show the Square of Number</title>
</head>
<body>
<jsp:include page="index.jsp"></jsp:include>  
<hr/>  
<%@page import="com.r4r.Test"%>  
  <%  
int number=Integer.parseInt(request.getParameter("number"));  
out.print("Square of "+number+" is: "+Test.getSq(number));  
%>  
</body>
</html>

Now run the Application you see the following output:

And this is the final output.

Previous Home Next