The Complete Spring Tutorial
The Complete Spring Tutorial
The Complete Spring Tutorial
In this tutorial I will show you how you can integrate struts, spring and hibernate in your
web application.
Introduction to the Spring Framework
This lesson is an introduction to the Spring Framework and its architecture.
Spring Tutorial By Farihah Noushene
Email: [email protected]
Part1
Introduction to the Spring Framework
Part 2
Downloading installing and writing small program using springframework
Part 3
Data persistent using springframwork.
Part 4
Spring's RMI service example.
What is Spring?
JDBC Exception Handling: The JDBC abstraction layer of the Spring offers a meaningful
exception hierarchy, which simplifies the error handling strategy
Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services
with Hibernate, JDO and iBATIS.
MVC Framework: Spring comes with MVC web application framework, built on core
Spring functionality. This framework is highly configurable via strategy interfaces, and
accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But
other frameworks can be easily used instead of Spring MVC Framework..
Spring Architecture
To allow users to implement custom aspects, complementing their use of OOP with AOP
Spring ORM
The ORM package is related to the database access. It provides integration layers for
popular object-relational mapping APIs, including JDO, Hibernate and iBatis.
Spring Web
The Spring Web module is part of Spring’s web application development stack, which
includes Spring MVC.
Spring DAO
The DAO (Data Access Object) support in Spring is primarily for standardizing the data
access work using the technologies like JDBC, Hibernate or JDO.
Spring Context
This package builds on the beans package to add support for message sources and for the
Observer design pattern, and the ability for application objects to obtain resources using a
consistent API.
Spring Core
The Core package is the most import component of the Spring Framework.
This component provides the Dependency Injection features. The BeanFactory provides
a factory pattern which separates the dependencies like initialization, creation and access
of the objects from your actual program logic.
PART-I
He had earlier, written a book titled 'J2EE Develoment without using EJB' and had
introduced the concept of Light-weight container. Primarily, his argument is that while
EJB has its merits, it is not always necessary and suitable in all applications.
Just as Hibernate attacks CMP as primitive ORM technology, Spring attacks EJB as
unduly complicated and not susceptible to unit-testing. Instead of EJB, Spring suggests
that we make use of ordinary Java beans, with some slight modifications, to get all the
supposed advantages of EJB environment. Thus, Spring is posed as an alternative to EJB
essentially. However, as a concession to the existing EJB investments, Spring is designed
to operate with EJB if required.
Much of the philosophy and approach of Spring framework, however, predates , the latest
EJB vesrion (ie) EJB-3, about to arrive shortly. It is said that EJB-3 has absorbed a
number of new ideas suggested by Spring and some more, to answer the criticisms. There
is a debate going on in the Java community about Spring and EJB-3. Spring is not a
Persistence technolgy but a framework which allows plug in of other such technologies.
But EJB-3 is primarily focussed on Persistence Technology and has now incorporated
Hibernate, the best ORM todate.Talks are going on to incorpotrate another equally nice
ORM Technology known as JDO, which provides for Object Database also. Moreoveer,
EJB-3 's Attribute-Orientaed Meta tags, help in vastly reducing the size of XML lines.
Some have complained that Spring is still too dependent on XML files. In this tutorial,
any reference to EJB, is only to EJB-2.My aim in presenting this tutorial is not to
advocate the choice of either Spring or EJB-3, a topic still being devbated by experts but
to share my perception with our readers.
In spring, we can make use of plain Java Beans to achieve things that were previously
possible with EJB only. The main aim of Spring is to simplify the J2EE development and
testing.
EJB has been around since 1988 and is an established standard and specification in
Enterprise world. Though there have been numerous Open-source Java technologies
recently, no other technology has claimed to be superior to EJB, in its total features.
Rod Johnson, in his book, critices a number of features in EJB. Significantly, he has also
spoken approvingly of some features of EJB and has recommended its use in some
special circumstances.
EJB is a standard with wide Enterprise-level Industry support.It has been deployed in
thousands of successful applications around the world. The EJB specification is a fixed
target and so tool-developers are able to develop wizards, which can make ejb
development quick and easy. There are many vendors for EJB application server like
IBM (Web-Sphere), BEA (Weblogic), Oracle (JDeveloper) etc.
To quote from another book on Spring ("Spring in Action" - Craig Walls-Manning press)
" EJB is complex ", as the author put it nicely, "not for just being complex'. It is complex
because it attempts to provide solutions for complex problems". EJB is developed mainly
for the remote transaction and distributed objects. But a number of enterprise projects do
not have this level of complexity and still use EJBs and even the simple application
becomes complex. In such cases Spring claims to be an alternative.
Since Spring comes with rich support to enterprise level services, it claims to be an
alternative to EJB It is worthwhile to begin with a comparison of EJB-2 and Spring, in
some main features.
The main advantages of EJB are :
a) Transaction Management
b) Declarative Transaction support
c) Persistence ( CMP & BMP)
d) Declarative Security
e) Distributed Computing (Container managed RPC)
Spring does not attempt to do everything by itself but supports the best of breed
technologies for each of these requirements.
For example, instead of CMP & BMP, it supports several persistence technologies like
JDO, Hibernate and OJB. These ORM toools are far more capable than the
implementation in CMP.To simplify JDBC coding, there are tools like iBatis and Spring
supports iBatis also.
Spring makes use of Acegi, an open-source Security framework and provides declarative
security through spring configuration file or class metadata while in EJB declarative
security is configured through deployment descriptor.
Spring provides proxying for RMI (special remoting technologies like Burlap) JAX-RPC
& web-service while EJB provides container-managed remote method calls.
Spring can offer declarative transaction like EJB. But spring provides declarative
rollback behavior also ,for methods and exceptions
Thus, while EJB is monolithic and attempts to do many things, some tasks fairly well and
some others not so well, Spring uses ordinary Java beans only and through special
techniques provides many of the functionalities of EJB, by integrating with a number of
open-source technologies.
1. Core Container:
The core container provides the fundamental functionality of Spring. It's primary
component is the 'BeanFactory', an implementation of the Factory pattern. The
BeanFactory applies the IOC pattern to separate an application's configuration and
dependency specification from the actual application code.
3. Spring AOP:(Aspect-Oriented)
The Spring AOP module integrates aspect-oriented programming functionality directly
into the Spring framework, through its configuration management feature. As a result we
can easily AOP-enable any object managed by the Spring framework. The Spring AOP
module provides transaction management services for objects in any Spring-based
application. With Spring AOP we can incorporate declarative transaction management
into our applications without relying on EJB components.
The Spring AOP module also introduces metadata programming to Spring. Using this we
can add annotation to the source code that instructs Spring on where and how to apply
aspects.
4. Spring DAO:
The Spring's JDBC and DAO abstraction layer offers a meaningful exception hierarchy
for managing the databaase connection, exception handling and error messages thrown by
different database vendors. The exception hierarchy simplifies error handling and greatly
reduces the amount of code that we need to write, such as opening and closing
connections. This module also provide transaction management services for objects in a
spring application.
5. Spring ORM:
The Spring framework can be integrated to several ORM frameworks to provide Object
Relational tool, including JDO, Hibernate, OJB and iBatis SQL Maps.
--------------------------------------------
I would venture to suggest that Spring will win sure acceptance among j2ee devcelopers ,
very soon because of its ready-made adapters for various hot web-tier and presentation
technologies.!
For example, there is a great varierty of technologies in the web-tier like MVC
PATTERN, STRUTS, JSF, WEB-WORK, JSP, TAPESTRY,FREEMARKER etc.
Developers are now puzzled and confused about the relative merits and demerits of all
these. Once they choose a technology and start implementing and later want to change
over to another technology, it is very difficult. But, as Spring offers modules for aall the
above technologies, it is most often simply changing the configuraion file. With this
approach, it is even possible for a development team to try and test a given task in all the
above forms and see the effect and performance before deciding the choice. Spring offers
its own version of MVC architecture. It also offers adapters for Struts.
'TilesConfigurer' can used to used to load Tiles configuration file for rendering Tiles
view. 'AbstractExcelView' is used to generate Excel SpreadSheet as views.
'AbstactPdfView' supports the creation of PDF as views. 'buildPdfDocument()' is used to
create PDF decument. Similarly we have 'buildExcelDocument()' to create the excel
document.
For delegation purpose, Spring provides 'DelegatingRequestProcessor' and to use the tiles
'DelegatingTilesRequestProcessor' is used. 'SpringTapestryEngine' is used for integrating
Tapestry to Spring. 'FacesSpringVariableResolver' is used to resolve spring-managed
beans in JSF.
-----------------------------------------------------------------------------
Next we shall see the main concepts of Spring, Inversion of Control (IoC) and Aspect
Oriented Programming. Spring is based on dependency injection type of IoC . We don't
directly connect our components and services together in code but describe which
services are needed by which components in a configuration file. A container is
responsible for hooking it up. This concept is similar to 'Declarative Management'. IOC is
a broad concept. the two main types are
1. Dependency Lookup:
The container provides callbacks to components and a lookup context. The managed
objects are responsible for their other lookups. This is the EJB Approach. The Inversion
of Control is limited to the Container involved callback methods that the code can use to
obtain resources. Here we need to use JNDI to look up other EJBs and resources.
Because of this reason EJB is not branded as 'IOC framework'.There are some problems
in this implementation. The class needs a application server environment as it is
dependent on JNDI and it is hard to test as we need to provide a dummy JNDI contest for
testing purpose.
2. Dependency Injection:
In this application objects is not responsible for looking up resources they depend on.
Instead IoC container configures the object externalizing resource lookup from
application code into the container. That is, dependencies are injected into objects. Thus
lookups are completely removed from application objects and it can be used outside the
container also.
----
In this method, the objects can be populated via Setter Injection (Java-Beans properties)
or Constructor Injection (constructor arguments). Each method has its own advantage and
disadvantage.
Normally in all the java beans, we will use setter and getter method to set and get the
value of property as follows
String name;
name = a;
}
public String getName()
return name;
We will create an instance of the bean 'namebean' (say bean1) and set property as
bean1.setName("tom"); Here in setter injection, we will set the property 'name' by using
the <property> subelement of <bean> tag in spring configuration file as showm below,
<value>tom</value>
</property>
</bean>
The subelement <value> sets the 'name' property by calling the set method as
String name;
public namebean(String a)
{
name = a;
We will set the property 'name' while creatinf an instance of the bean 'namebean' as
namebean bean1 = new namebean("tom");
Here we use the <constructor-arg> element to set the the property by constructor
injection as
<constructor-arg>
</constructor-arg>
</bean>
To set properties that reference other beans <ref>, subelement of <property> is used as
shown below,
<property name="game">
<ref bean="bean2"/>
</property>
</bean>
Aspect-Oriented Programming
---------------------------
----------------------------------------
The container is at the core of Spring Container. In manages the life cycle and
configuration of application objects. We can configure how each of our beans should be
created either to create a single instance of bean or produce a new instance every time
and how they should be associated with each other. Spring should not, however, be
confused with traditionally heavyweight EJB containers, which are often large. The
Spring actually comes with two distinct containers: Bean Factories-defined by
"org.springframework. beans.factory.BeanFactory" are the simplest containers, providing
support for dependency injection. Application contexts - defined by
"org.springframework.context.Application Context" provides application framework
services.
BEAN FACTORY:
Bean factory is an implementation of the factory design pattern and its function is to
create and dispense beans. As the bean factory knows about many objects within an
application, it is able to create association between collaborating objects as they are
instantiated. This removes the burden of configuration from the bean and the client.
This line tells the bean factory to read the bean definition from the XML file. The bean
definition includes the description of beans and their properties. But the bean factory
doesn't instantiate the bean yet. To retrieve a bean from a 'BeanFactory', the getBean()
method is called. When getBean() method is called, factory will instantiate the bean and
begin setting the bean's properties using dependency injection.
APPLICATION CONTEXT:
While Bean Factory is used for simple applications, the Application Context is spring's
more advanced container. Like 'BeanFactory' it can be used to load bean definitions, wire
beans together and dispense beans upon request.
It also provide
1) a means for resolving text messages, including support for internationalization.
2) a generic way to load file resources.
3) events to beans that are registered as listeners.
There are other lightweight containers like HiveMind, Avalon, PicoContainer etc.,
Avalon was one of the first IoC containers. Avalon mainly provides interface-dependent
IoC. so, we much change our code in order to use a different container. This couples your
code to a particular framework which is an undesirable feature.
PicoContainer is a minimal (very small size nearly 50k) lightweight container that
provides IoC in the form of constructor and setter injection. By using PicoContainer we
can only assemble components programmatically through PicoContainer's API. But it
allows only one instance of any particular type to be present in the registry. Also
PicoContainer is only a container. It does not offer various special features as spring like
integration.
HiveMind is relatively new IoC container. Like PicoContainer, it focuses on wiring with
support for both constructor and setter injections. It also allows us to define our
configuration in an XML file. Like PicoContainer, HiveMind is only a container. It does
not offer integration with other technology. Thus Spring makes it possible to configure
and compose complex applications from simpler components. In Spring, application
objects are composed declaratively, typically in an XML file. Spring also provides much
infrastructure functionality like transaction management, persistence framework
integration, etc., leaving the development of application logic to us.
With this inroduction, we shall see a simple example in Spring in next article.
-------------------------------------------
- Rod Johnson
PART-II
The Spring Framework comes in the form of ZIP file with the necessary jars, examples
etc., The Spring framework can be downloaded from http://www.springframework.org.
There will be two zip files one with dependencies and other without. The spring
framework with dependencies is larger and includes all dependent libraries. Download
Spring framework 1.2 without dependency and unzip on the hard disk as spring12
Inside the folder spring12 we can find 7 folders. The name and the contents of all the
folders are given below,
1. dist: It contains various Spring distribution jar files.
2. docs: It contains general documentation and API javadocs.
3. mock: It contains mock JNDI contexts and a set of servlet API mock objects.
4. samples: It contains demo applications and skeletons.
5. src: It contains the Java source files for the framework.
6. test: It contains the Java source files for Spring's test suite.
7. tiger: It contains JDK1.5 examples and test suite.
Inside the "dist" directory, we can find all the jar files necessary for compiling and
executing the program. The jar files and its contents are listed below:
1. spring.jar : It contains the entire Spring framework including everything in the other
JAR files also.
2. spring-core.jar : It contains the core Spring container and its utilities.
3. spring-beans.jar : It contains the bean Spring container and JavaBeans support
utilities.
4. spring-aop.jar : It contains Spring's AOP framework, source-level metadata support,
AOP Alliance interfaces etc.,
5. spring-context.jar : It contains application context, validation framework, JNDI,
templating support and scheduling.
6. spring-dao.jar : It contains DAO support and transaction infrastructure.
7. spring-jdbc.jar : It contains the JDBC support.
8. spring-support.jar : It contains JMX support, JCA support, scheduling support, mail
support and caching support.
9. spring-web.jar : It contains the web application context, multipart resolver, Struts
support, JSF support and web utilities.
10. spring-webmvc.jar : It contains the framework servlets, web MVC framework, web
controllers and web views.
11. spring-remoting.jar :It contains remoting support, EJB support and JMS support.
12. spring-orm.jar : It contains iBATIS SQL Maps support, Apache OJB support,
TopLink support and JDO support.
13. spring-hibernate.jar : It contains Hibernate 2.1 support, Hibernate 3.x support.
14. spring-mock.jar : It contains JNDI mocks, Servlet API mocks and JUnit support.
--------------------------------------------
f:\>md springdemo
f:\>cd springdemo
As the entire Spring Framework is included in spring.jar. We use that to run our
examples.
Copy spring.jar from spring12\dist folder to the working folder. Also copy commons-
logging.jar from tomcat41\server\lib to the working directory.
f:\springdemo>set path=c:\windows\command;g:\jdk1.5\bin
(* Set path for jdk1.5 or jdk1.4.2 only. )
f:\springdemo>set classpath=f:\springdemo;
f:\springdemo\spring.jar;
f:\springdemo\commons-logging.jar
Edit
1. hello.java
2. helloimpl.java
3. hello.xml
4. helloclient.java
//f:\springdemo\hello.java
public interface hello
{
public String sayhello(String a);
}
--------------------------------------------
//f:\springdemo\helloimpl.java
public class helloimpl implements hello
public helloimpl(String a)
greeting=a;
return greeting+s;
greeting=a;
}
--------------------------------------------
//f:\springdemo\hello.xml
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello"
class="helloimpl">
<property name="greeting">
<value>Good Morning!...</value>
</property>
</bean>
</beans>
--------------------------------------------
//f:\springdemo\helloclient.java
import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
try
{
System.out.println("please Wait.");
Resource res = new ClassPathResource("hello.xml");
BeanFactory factory = new XmlBeanFactory(res);
hello bean1 = (hello)factory.getBean("hello");
String s = bean1.sayhello(args[0]);
System.out.println(s);
catch(Exception e1)
{ System.out.println(""+e1); }
-------------------------------------------
To run:
f:\springdemo>javac hello.java
f:\springdemo>javac helloimpl.java
f:\springdemo>javac helloclient.java
f:\springdemo>java helloclient "sam"
------------------------------------
Thus we have run our first Spring program. Here helloimpl implements the hello
interface. Although it is not necessary to hide the implementation behind an interface, it
is recommended as a way to seperate implementation from interface. helloimpl has a
single property greeting. This property can be set in two different ways: by property's
setter method or by the constructor. The XML file hello.xml declares the instance of
helloimpl.java in the spring container and configures its property greeting with the value
'Good Morning!...'
The root of the hello.xml file is the <beans> element, which is the root element of any
Spring configuration file. The <bean> element is used to tell the Spring container about
the class and how it should be configured. Here, the id attribute takes the interface name
and the class attribute specifies the bean’s fully qualified class name.
Within the <bean> element, the <property> element is used to set the property, in this
case the greeting property. By using <property>, we’re telling the Spring container to call
setGreeting() while setting the property. The value of the greeting is defined within the
<value> element. Here we have given 'Good Morning!...' as the value.
The container instantiates the 'helloimpl' based on the XML definition as,
Similarly, greeting property may be set through the single argument constructor of
'helloimpl' as,
<bean id="hello" class="helloimpl">
<constructor-arg>
<value>Good Morning!...</value>
</constructor-arg>
</bean>
Now the container instantiates the 'helloimpl' based on the XML definition as,
In the client program, 'BeanFactory' class is used which is the Spring container. Then the
'getBean()' method is called to get the reference to the 'hello'. With this reference,
sayhello() method is called.
------------------------------------------
We can also use a frame client. The code is very much similar to console client except the
GUI code.
//f:\springdemo\helloframe.java
import java.io.*;
import java.awt.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
TextField text1;
TextArea area1;
Label label1;
Button button1;
app.setSize(700,500);
app.setVisible(true);
helloframe()
setLayout(new FlowLayout());
setBackground(Color.green);
text1=new TextField(25);
area1=new TextArea(10,50);
button1=new Button("Exit");
button1.setBackground(Color.red);
add(label1);
add(text1);
add(area1);
add(button1);
if(e.target==text1)
try
area1.append("Please Wait..\n");
String s = bean1.sayhello(text1.getText());
area1.append(s);
catch(Exception e1)
{area1.append(""+e1);}
}
if(e.target==button1)
System.exit(0);
return true;
Then compile and run the frame client program. We will a textbox, a exit button and a
text area. Type a name (say 'tom') in text area and press enter. 'Good Morning!... tom' will
appear in the text area
--------------------------------------------
Next we shall see how to run this program as a servlet. Consider Tomcat-5 is installed
in g drive.
First copy g:\spring12\spring.jar to g:\tomcat5\common\lib and start tomcat server.
Then set classpath as shown below and edit the servletclient.java.
f:\springdemo>set classpath=f:\springdemo;
f:\springdemo\spring.jar;
f:\springdemo\commons-logging.jar;
g:\tomcat5\common\lib\servlet-api.jar
//f:\springdemo\servletclient.java
import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
throws ServletException,IOException
resp.setContentType("text/html");
String a = req.getParameter("text1");
try
System.out.println("Please wait.");
System.out.println("Resource ok");
System.out.println("BeanFactory ok");
out.println(s);
catch(Exception e1)
{System.out.println(""+e1);}
--------------------------------------------
//f:\springdemo\servletclient.htm
<html>
<body>
<form method=post
action="http://localhost:8080/
servlet/servletclient">
<input type=submit>
</form>
</body>
</html>
--------------------------------------------
Then compile the servlet and copy all the class files ie., hello.class, helloimpl.class,
servletclient.class and the xml file hello.xml to g:\tomcat5\webapps\root\web-inf\classes.
Copy html file servletclient.htm to g:\tomcat5\webapps\root. Add entry to web.xml file.
Restart Tomcat server and open browser and type url as
http://localhost:8080/servletclient.htm. We will get a text box and a button. Type a name
(say 'tom') and click the 'submit' button.
In the next article, we shall see how to contact the database from Spring.
PART-III
Now we will move on to the main process of any enterprise application: Data Persistence.
For this we have to initialize our data access framework, manage resources, handle
various exceptions and if anything goes wrong, we must roll-back so as to save the
existing data.
Spring comes with a family of data access frameworks that integrates well will variety of
data access technologies like JDBC, Java Data Objects and Object Relational Mapping
(ORM) tools like Hibernate, OJB, iBatis etc.,
Many J2EE application servers and even web servers provide a 'dataSource' via Jndi
name. To configure the spring bean with the Jndi name of our 'dataSource' and use its
connection pooling facility 'JndiObjectFactoryBean' is used. When a DataSource is not
present, we need a connection pooling bean that implements 'dataSource'. For this
purpose we use 'dbcp.BasicDataSource' is used. By using this we can have a 'dataSource'
with connection pooling independent of application server.
To perform unit-tests in our data access code, spring comes with a very lightweight
'dataSource' implementation class: 'DriverManagerDataSource'. This class can be easily
configured for unit tests as,
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
----------------------------------------------
Spring comes with its own data access framework. Spring separates the fixed and variant
parts of data access process into two distinct classes : template and callback. Template
manages the fixed part of our framework like data connection, managing resources,
controlling transaction etc., while the Callback defines the things that are specific to our
application like creating statements, binding parameters etc.,
Then start the 'My Sql Console Line Client' from programs and type the password.
We will get message as 'Database changed'. Next create table in test database as follows
We will get the message 'Query OK, 0 rows affected'. Now we have created a table in
mysql database, set the path and classpath as before and edit the program
-----
f:\sprindemo\datacon.java
import javax.sql.*;
----------------------------------------
f:\sprindemo\dataconimpl.java
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.*;
import org.springframework.jdbc.support.*;
import javax.sql.*;
dataSource = ds;
return dataSource;
----------------------------------------
f:\sprindemo\datacon.xml
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>sql</value>
</property>
</bean>
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
</beans>
----------------------------------------
f:\sprindemo\springservlet.java
import java.io.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.*;
import org.springframework.jdbc.support.*;
throws ServletException,IOException
resp.setContentType("text/html");
String a = req.getParameter("text1");
String b = req.getParameter("text2");
String c = req.getParameter("combo1");
String d = req.getParameter("combo2");
try
System.out.println("Wait...");
DataSource ds=bean1.dbcon();
if(d.equals("add"))
out.println("Record Added");
if(d.equals("delete"))
out.println("Record Deleted");
if(d.equals("find"))
{
List list1;
Iterator i=list1.iterator();
while(i.hasNext())
Object ob = i.next();
out.println(ob.toString());
if(d.equals("update"))
if(c.equals("name"))
table1.name='"+a+"' ");
if(c.equals("place"))
table1.place='"+b+"' ");
}
out.println("Record Updated");
catch(Exception e1)
{System.out.println(""+e1);}
----------------------------------------
f:\sprindemo\springservlet.htm
<html>
<body bgcolor="pink">
<form method=post
action="http://localhost:8080/
servlet/springservlet">
Criterion :
<option value="name">Name
<option value="place">Place
</select> <br><br>
<option value="delete">Remove
<option value="find">Find
<option value="update">Update
</select> <br><br>
<input type=submit>
</form>
</body>
</html>
The deployment procedure is same as before compile the all the files datacon.java,
dataconimpl.java and springservlet.java and copy all the class files and the xml file
datacon.xml to g:\tomcat5\webapps\root\web-inf\classes. Copy the html file to g:\
tomcat5\webapps\root. Add entry to web.xml file.
and type url as http://localhost:8080/ servletclient.htm. We will get a two textboxes, two
comboboxes and a 'submit' button. Type name and place in textboxes, select 'add' from
combobox and click 'submit' button. We will get message as 'Record Added'
----------------------------------------
Spring provides integration for many of the ORM frameworks like Hibernate, JDO,
Apache OJB and iBATIS SQL Maps.
The only property that we need to change to integrate Spring with OJB is
'ConnectionFactoryClass' and it is done by using 'LocalDataSourceConnectionFactory'.
In the next article we shall see how to use a RMI service in Spring and how to export any
Spring managed bean as RMI.
PART-IV
2. Hessian
3. Burlap
4. HTTP invoker
5. EJB
6. JAX-RPC
In all the models, services are configured into the application through spring
configuration file as spring managed beans. This is accomplished by using a proxy
factory bean that enable us to wire remote services into the properties of our beans as if
they were local objects.
For wiring a Hessian based service to Spring client, Spring's 'HessianProxyFactory Bean'
is used. To export a Hessian Service 'HessianServiceExporter' is used and similarly for
wiring a Burlap service 'BurlapProxyFactoryBean' is used and 'BurlapServiceExporter' is
used to export a burlap service.
For all the above models spring provides service exporter classes that exports Java Beans
as remote service. Spring does not provide any EJB Service Exporter and it provides four
abstract support classes to make the development of Spring enabled EJB. They are
The client makes calls to the proxy to provide the service and the proxy calls the remote
service on behalf of the client.
--------------------------------------------
Now we shall see how to wire other RMI services into spring application and also how to
export our own service.
RMI was first introduced in JDK 1.1. But developing and accessing RMI services
involves various steps and also have lookups which makes the code hard to test. Spring
simplifies the RMI by providing a 'proxy factory bean' that enables us to wire the RMI
services into spring application as if they were local beans. Spring also provides a remote
exporter that converts our 'spring managed beans' into RMI services.
<bean id="service1"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://${hostname}/service1</value>
</property>
<property name="serviceInterface">
<value>service1</value>
</property>
</bean>
The url of the RMI service is set through the 'serviceUrl' property. The 'serviceInterface'
property specifies the interface that the service implements and only through that the
client invokes methods on the service.
For using the service the implementation code is wired to the RMI using the following
code,
<property name="service1">
<ref bean="service1"/>
</property>
</bean>
-------------------------------------------
First set the path and classpath as before. Next edit the RMI service.
//f:\springdemo\rmserver.java
import java.rmi.*;
}
----------------------------------------------
//f:\springdemo\rmserverimpl.java
import java.rmi.*;
import java.rmi.server.*;
implements rmserver
try
Naming.rebind("rmserver",ob);
System.out.println("ready");
catch(Exception e1)
{System.out.println(""+e1);}
System.out.println("constructor ok");
return "Hai..."+a;
----------------------------------------------
//f:\springdemo\rmserver.xml
"http://www.springframework.org/dtd/
spring-beans.dtd">
<beans>
<bean id="rmserver"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://localhost/rmserver</value>
</property>
<property name="serviceInterface">
<value>rmserver</value>
</property>
</bean>
<property name="rmserver">
<ref bean="rmserver"/>
</property>
</bean>
</beans>
---------------------------------------------
//f:\springdemo\rmspring.java
import java.rmi.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
try
System.out.println("Wait..");
String r=bean1.getresult(args[0]);
System.out.println(r);
catch(Exception e1)
{System.out.println(""+e1);}
---------------------------------------
To run:
f:\springdemo>javac rmserver.java
f:\springdemo>javac rmserverimpl.java
f:\springdemo>javac rmspring.java
f:\springdemo>java rmserverimpl
Open another Window and run the client code by giving the argument
Wait..
......
Hai... sam
-----------------------------------------------------------------
Spring also supports the server side of RMI. Here the service itself is written with spring
and it is exposed as an RMI service. Here the bean is written as a simple JavaBean. Also
we need not generate the stub and skeleton using 'rmic' command and manually add it to
RMI registry. Instead of these traditional procedure 'RmiServiceExporter' is used to
export any Spring managed bean as an RMI service. It wrapps the bean in an adapter
class. The adapter class is then bound to RMI registry and the proxies request the service.
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service1">
<ref bean="service1"/>
</property>
<property name="serviceName">
<value>service1</value>
</property>
<property name="serviceInterface">
<value>service1</value>
</property>
</bean>
The 'serviceName property' indicates the name of service and 'serviceInterface' specifies
the interface implemented by the service. There is no need of 'serviceUrl' here
First set the path and classpath as before. Next edit the service.
//f:\springdemo\rmservice.java
}
------------------------------------------
//f:\springdemo\rmserviceimpl.java
System.out.println("ready");
}
public rmserviceimpl()
System.out.println("constructor ok");
return "Hai"+a;
------------------------------------------
//f:\springdemo\rmservice.xml
"http://www.springframework.org/dtd/ spring-beans.dtd">
<beans>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<value>rmservice</value>
</property>
<property name="serviceName">
<value>service1</value>
</property>
<property name="serviceInterface">
<value>rmservice</value>
</property>
</bean>
</bean>
</beans>
------------------------------------------
//f:\springdemo\rmserviceclient.java
import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
class rmserviceclient
try
System.out.println("Wait..");
System.out.println("factory created");
rmservice bean1 = (rmservice)factory.getBean("rmservice");
String s = bean1.getresult(args[0]);
System.out.println(s);
catch(Exception e1)
{System.out.println(""+e1);}
---------------------------------------
To run:
f:\springdemo>javac rmservice.java
f:\springdemo>javac rmserviceimpl.java
f:\springdemo>javac rmserviceclient.java
f:\springdemo>java rmsserviceclient
Wait..
org.springframework.beans.factory.
xml.XmlBeanDefinitionReader
loadBeanDefinitions
support.AbstractBeanFactory getBean
constructor ok
Hai...sam
Here the service interface doesn't extend the 'java.rmi.Remote' method and
'RemoteException' is not thrown by the methods. There is no binding in the
implementation code. Also we can direcly run the client. No run to run 'rmserverimpl'
first. Also there is no need to run the RMI registry.