Web Technologies
|
|
SPRING
SPRING: Application, Frame and Servlet
|
1. Introduction
Spring is a java framework. It is an open source application development.
Its purpose is to simplify programming with Java; primarily J2EE. Spring has
two main concepts: dependency injection and the aspect oriented programming (AOP).
Dependency injection (or Inversion of control) allows possibility to change just
a part in a given application. Aspect Oriented Programming (AOP) reduces duplication
of the related classes. Spring is a container which contains application objects that
are declared in an XML file.
It groups seven modules:
- AOP: module for enterprise services as a replacement for EJB, such as
declarative transaction management
- ORM: package related to the database access. This part includes Hibernate
- Web: part of web application development stack
- DAO(Data Access Object): It includes JDBC and Hibernate
- Context: to emphasize message sources
- Web MVC: for MVC implementations for the web applications
- Core: main component of the Spring Framework. It is the module for
programming: creating, configuring and managing beans.
Each user invocation is related to a business service which is
registered as a bean in the XML beans descriptor file. When Spring
framework receives this invocation of a service from the application,
it will match the corresponding bean and run the mapped service.
To get Spring, go to:
springframework
And download the file:
spring-framework-2.5.1.zip
Extract the file spring.jar from the DIST folder where
stores all Spring jar files.
Copy the file spring.jar and place it in the folders:
- The working directory, (here: C:\J2EE\Spring\Web\),
- The libraries for Tomcat:
(here: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\lib\)
(Its also placed in: C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\)
where is placed servlet-api.jar.
We need also commons-logging.jar and maybe log4j.jar files:
1. Go to:
tomcat.apache.org
Double-Click on: Download Commons Logging , and
download the Binary 1.1.1.zip; then extract commons-logging-1.1.1.jar
2. Go to:
logging.apache.org
Choose log4j-1.2.15.jar, download and extract it.
Place these two files in the tomcat librairies:
(here: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\lib\)
(it is also placed in:C:\Program Files\Apache Software Foundation\Tomcat 6.0\)
Keep then the two files spring.jar and commons-logging-1.1.1.jar in the
working director and seth slasspath to the working directory, like:
set CLASSPATH=C:\J2EE\Spring\Web;
C:\J2EE\Spring\Web\spring.jar;
C:\J2EE\Spring\Web\commons-logging-1.1.1.jar;
C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar
This classpath must be set in the prompt to compile files.
2. Example:
2.1.THE STEPS
2.1.1. registration of a service as bean:
The first related class is
javaBean
This class is the supper class; then an interface: javaBean.java
2.1.2. Implementing javaBean:
The following implBean.java class is the subclass of java
javaBean class:
implBean.java
2.1.3. Declaring and passing values into properties in xml file:
Here is the related file javaBean.xml :
javaBean.xml
2.1.4. The client related file:
This file can be writte as :
clientBean.java
2.1.5. Set the classpath and compile the three files:
C:\J2EE\Spring\Web> javac javaBean.java
C:\J2EE\Spring\Web> javac implBean.java
C:\J2EE\Spring\Web> javac clientBean.java
|
|
2.2. APPLICATION
C:\J2EE\Spring\Web> java clientBean will outputs::
2.2.1. Without argument:
C:\J2EE\Spring\Web>java clientBean
22-Feb-2008 9:16:52 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [javaBean.xml]
java.lang.ArrayIndexOutOfBoundsException: 0
2.2.2. With argument: "Shamael"
C:\J2EE\Spring\Web>java clientBean Shamael
22-Feb-2008 9:17:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [javaBean.xml]
All the best of luck Shamael
C:\Spring\Web>
Here the output
2.3.FRAME
2.3.1. Spring with a frame:
The related file is:
beanFrame.java
Compile as:
C:\J2EE\Spring\Web>javac beanFrame.java
2.3.2.Rise the frame by executing:
C:\Spring\Web>java beanFrame
Here is:
the related frame
2.4.SERVLET
2.4.1. Spring application as a sevlet:
The class related to the client is:
servletClient.java
Once compiled:
2.4.2.
Add in the web.xml file in the directory:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\web.xml
<servlet>
<servlet-name>servletClient</servlet-name>
<servlet-class>servletClient</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet >
And
<servlet-mapping>
<servlet-name>servletClient</servlet-name>
<url-pattern>/servlets/servlet/servletClient</url-pattern>
</servlet-mapping>
2.4.3.
Add in the classes directory:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\classes\
all the classes and xml files; That is:
javaBean.class
implBean.class
clientBean.class
servletClient.class
javaBean.xml
Make sure that we have
all the classes
2.4.4.
Place the file:
servletClient.html
in the servlets directory:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\servlets\
2.4.5. Start Tomcat and Browse at:
http://localhost:8055/examples/servlets/servletClient.html
(the default port 8080 is already occpied, we can change it the conf
file of Tomcat)
We can see:
Spring Servlet Query
and
Sring Servlet Response
|
|
|
|