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:
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.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> 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 © The Scientific Sentence. 2008 All rights reserved. |