Spring tutorial

Spring tutorial

Here I will describe on Spring tutorial. let me describe the working environment. my another blog shows how to set up your environment. To know more on springframework please visit here.

Spring tutorial – step by step

In this example we are using following tools

Eclipse 3.4: Ganymede;

Plugs in: svn, Maven, spring-ide, tomcat lunher, ant.

Tomcat: 5.5

FireFox: 3

Create a new project in Eclipse;

Spring tutorial - Eclipse create new project

Spring tutorial – Eclipse create new project

Spring tutorial - set project name

Spring tutorial – set project name

create one package name:  click the right button on the src folder and select new -> Package and put  com.rajib.spring.samplespring.model value.

create a class Information under the model folder. In this Spring tutorial have used couple of java classes, here are they

——————Information.java

package com.rajib.spring.samplespring.model;

public class Information {
	private String projectName = "Spring demo1";
	private String version = " 1.1";

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}
}

create an xml file  applicationContext.xml under the src file. by default spring is looking this under web-inf folder but we put his under class path intentionally thats why we will mention the conetx file location at the web.xml. In this spring tutorial, I am trying to show you can create a spring application. We can mention all beans under <beans> tag.

————- applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="information"      
    class="com.rajib.spring.samplespring.model.Information"/>
</beans>

we will show information.project name and information.version at index.jsp page. we used the information class as bean.

Create WebContent folder under the project. create WEB-INF folder under the WebContent folder. Create lib folder under WEB-INF folder. I used lot of jar files as library file. My lib contains jarlist jar files.

Create log4j.properties and web.xml file under WEB-INF folder.

————–log4j.properties

log4j.logger.java.sql=DEBUG, STDOUT
log4j.logger.com.ibatis=DEBUG, STDOUT
log4j.logger.java.sql.Statement=INFO,STDOUT
log4j.rootLogger=ERROR,STDOUT

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.conversionPattern=%-5p - %-26.26c{1} - %mn

————————- web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<description>Spring simple example</description>
	<display-name>simplespring</display-name>
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>simplespring</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.properties</param-value>
	</context-param>
	<context-param>
		<param-name>ADMIN_EMAIL_ADDRESS</param-name>
		<param-value>rajib_info@yahoo.com</param-value>
	</context-param>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<session-config>
		<session-timeout>10</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Create index.jsp file under WebContent folder.

———– index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page
	import="org.springframework.beans.factory.BeanFactory,org.springframework.beans.factory.xml.XmlBeanFactory,org.springframework.core.io.ClassPathResource,com.rajib.spring.samplespring.model.Information"%>
<head>
<title>Simple Spring example</title>
</head>
<body bgcolor="white">
	<h2>(Spring example by rajib_info@yahoo.com)</h2>

	<%
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"applicationContext.xml"));
		Information info = (Information) beanFactory.getBean("information");
	%>

	Hello, this is sample spring project!
	<br /> Project Name is :
	<b> <%=info.getProjectName()%>
	</b> Project Version is :
	<b> <%=info.getVersion()%>
	</b>
</body>

to make the build path to do the following

Spring tutorial - eclipse build path

Spring tutorial – eclipse build path

-click right button on the project and select properties select the Java Build Path from the left side. click – Library tab from right side , click Add jar select all jars of  (SimpleSpring->WebContent->lib all jars). click the Ordre and Ecports tab and click on  Select All button.

How to run this project:

for building this project we need buld.xml

 

—————————- build.xml

<project name="simplespring" basedir="." default="deploy">
	<!-- Project settings -->
	<property name="project.distname" value="simplespring" />
	<!-- Local system paths -->
	<property file="${basedir}/build.properties" />
	<property name="webroot.dir" value="${basedir}/WebContent" />
	<property name="webinf.dir" value="${webroot.dir}/WEB-INF" />
	<property name="build.dir" value="target" />

	<!-- classpath for WEB-INF/lib -->
	<path id="compile.classpath">
		<pathelement path="${webinf.dir}/lib/acegi-security-1.0.3.jar" />
		<pathelement path="${webinf.dir}/lib/activation.jar" />
		<pathelement path="${webinf.dir}/lib/antlr-2.7.6.jar" />
		<pathelement path="${webinf.dir}/lib/aopalliance.jar" />
		<pathelement path="${webinf.dir}/lib/avalon-framework-4.2.0.jar" />
		<pathelement path="${webinf.dir}/lib/barbecue-1.1.jar" />
		<pathelement path="${webinf.dir}/lib/bsf-2.3.jar" />
		<pathelement path="${webinf.dir}/lib/bsh-2.0b4.jar" />
		<pathelement path="${webinf.dir}/lib/cglib-nodep-2.1_3.jar" />
		<pathelement path="${webinf.dir}/lib/commons-annotations.jar" />
		<pathelement path="${webinf.dir}/lib/commons-beanutils.jar" />
		<pathelement path="${webinf.dir}/lib/commons-codec.jar" />
		<pathelement path="${webinf.dir}/lib/commons-collections.jar" />
		<pathelement path="${webinf.dir}/lib/commons-dbcp.jar" />
		<pathelement path="${webinf.dir}/lib/commons-digester-1.8.jar" />
		<pathelement path="${webinf.dir}/lib/commons-discovery-0.4.jar" />
		<pathelement path="${webinf.dir}/lib/commons-fileupload.jar" />
		<pathelement path="${webinf.dir}/lib/commons-io.jar" />
		<pathelement path="${webinf.dir}/lib/commons-javaflow-20060411.jar" />
		<pathelement path="${webinf.dir}/lib/commons-lang.jar" />
		<pathelement path="${webinf.dir}/lib/commons-logging-1.1.1.jar" />
		<pathelement path="${webinf.dir}/lib/commons-pool.jar" />
		<pathelement path="${webinf.dir}/lib/commons-validator-1.3.1.jar" />
		<pathelement path="${webinf.dir}/lib/dom4j-1.6.1.jar" />
		<pathelement path="${webinf.dir}/lib/dwr.jar" />
		<pathelement path="${webinf.dir}/lib/EasySI-0.9.0.jar" />
		<pathelement path="${webinf.dir}/lib/ehcache-1.2.3.jar" />
		<pathelement path="${webinf.dir}/lib/fop.jar" />
		<pathelement path="${webinf.dir}/lib/ibatis-common-2.jar" />
		<pathelement path="${webinf.dir}/lib/ibatis-sqlmap-2.jar" />
		<pathelement path="${webinf.dir}/lib/iReport.jar" />
		<pathelement path="${webinf.dir}/lib/itext-1.3.1.jar" />
		<pathelement path="${webinf.dir}/lib/jakarta-oro-2.0.8.jar" />
		<pathelement path="${webinf.dir}/lib/jaxen-1.1-beta-7.jar" />
		<pathelement path="${webinf.dir}/lib/jazzy-core.jar" />
		<pathelement path="${webinf.dir}/lib/jsf-facelets.jar" />
		<pathelement path="${webinf.dir}/lib/jsf-impl.jar" />
		<pathelement path="${webinf.dir}/lib/jstl.jar" />
		<pathelement path="${webinf.dir}/lib/jta.jar" />
		<pathelement path="${webinf.dir}/lib/log4j-1.2.14.jar" />
		<pathelement path="${webinf.dir}/lib/mail.jar" />
		<pathelement path="${webinf.dir}/lib/myfaces-api-1.2.5.jar" />
		<pathelement path="${webinf.dir}/lib/myfaces-impl-1.2.5.jar" />
		<pathelement path="${webinf.dir}/lib/myfaces-shared-impl-3.0.5.jar" />
		<pathelement path="${webinf.dir}/lib/ojdbc14.jar" />
		<pathelement path="${webinf.dir}/lib/poi-3.0.1-FINAL-20070705.jar" />
		<pathelement path="${webinf.dir}/lib/quartz-all-1.6.1-RC1.jar" />
		<pathelement path="${webinf.dir}/lib/serializer-2.7.0.jar" />
		<pathelement path="${webinf.dir}/lib/spring-aop.jar" />
		<pathelement path="${webinf.dir}/lib/spring-beans.jar" />
		<pathelement path="${webinf.dir}/lib/spring-core.jar" />
		<pathelement path="${webinf.dir}/lib/spring-context.jar" />
		<pathelement path="${webinf.dir}/lib/spring-dao.jar" />
		<pathelement path="${webinf.dir}/lib/spring-ibatis.jar" />
		<pathelement path="${webinf.dir}/lib/spring-jdbc.jar" />
		<pathelement path="${webinf.dir}/lib/spring-support.jar" />
		<pathelement path="${webinf.dir}/lib/spring-web.jar" />
		<pathelement path="${webinf.dir}/lib/spring-webmvc.jar" />
		<pathelement path="${webinf.dir}/lib/standard.jar" />
		<pathelement path="${webinf.dir}/lib/tomahawk12-1.1.8.jar" />
		<pathelement path="${webinf.dir}/lib/xalan-2.7.0.jar" />
		<pathelement path="${webinf.dir}/lib/xercesImpl-2.7.1.jar" />
		<pathelement path="${webinf.dir}/lib/xml-apis-1.3.02.jar" />
		<pathelement path="${webinf.dir}/lib/xmlgraphics-commons-1.1.jar" />
		<pathelement path="${webinf.dir}/classes" />
		<pathelement path="${classpath.external}" />
		<pathelement path="${classpath}" />
	</path>

	<!-- define your folder for deployment -->
	<property name="deploy.dir" value="deploy" />

	<!-- Check timestamp on files -->
	<target name="prepare">
		<tstamp />
	</target>

	<!-- Copy any resource or configuration files -->
	<target name="resources">
		<copy todir="${webinf.dir}/classes" includeEmptyDirs="no">
			<fileset dir="src">
				<patternset>
					<include name="**/*.conf" />
					<include name="**/*.properties" />
					<include name="**/*.xml" />
				</patternset>
			</fileset>
		</copy>
	</target>

	<!-- Normal build of application -->
	<target name="compile" depends="prepare,resources">
		<javac srcdir="src" destdir="${webinf.dir}/classes" encoding="UTF-8">
			<classpath refid="compile.classpath" />
		</javac>
	</target>

	<!-- Remove classes directory for clean build -->
	<target name="clean" description="Prepare for clean build">
		<delete dir="${webinf.dir}/classes" />
		<mkdir dir="${webinf.dir}/classes" />
	</target>

	<!-- Build entire project -->
	<target name="build" depends="prepare,compile" />
	<target name="rebuild" depends="clean,prepare,compile" />

	<!-- Create binary distribution -->
	<target name="war" depends="build">
		<mkdir dir="${build.dir}" />
		<war basedir="${webroot.dir}" warfile="${build.dir}/${project.distname}.war"
			webxml="${webinf.dir}/web.xml">
			<exclude name="WEB-INF/${build.dir}/**" />
			<exclude name="WEB-INF/src/**" />
			<exclude name="WEB-INF/web.xml" />
		</war>
	</target>

	<target name="deploy" depends="war">
		<delete file="${deploy.dir}/${project.distname}.war" />
		<delete dir="${deploy.dir}/${project.distname}" />
		<copy file="${build.dir}/${project.distname}.war" todir="${deploy.dir}" />
	</target>

</project>

there are some supporting file exist for this build.xml these are –

————————- build.properties

classpath.external=external-lib/servlet-api.jar;external-lib/jsp-api.jar;

so we need to create another folder external-lib and put these 2 jar files.

Spring tutorial - build

Spring tutorial

to build this project use these stepts – open build.xml. click – window->show view -> outline . click the right button on rebuild select – Run As -> 1 Ant Build.

set a context configaration file at {Tomcat folder}/conf/Catalina/localhost. lest assume this file name is – simplespring.xml and its looks –

 

 

————-simplespring.xml

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

<Context docBase="D:/workspace/simplespring/WebContent"
privileged="true" antiResourceLocking="false" antiJARLocking="false">
</Context>

if your eclipse has tomcat lunch plugs in so you can deploy from eclipse. click on as picture

Spring tutorial - run-tomcat

Spring tutorial

 

The output looks —

Spring tutorial - output

Spring tutorial

Download Spring tutorial

Click here to download this project.

Making workspace (Set up Eclipse, Tomcat, Ant, Maven, Spring Ide, SVN)

I used eclipse IDE for creating my working environment.

  • Download eclipse.  if you want old version visit this site http://archive.eclipse.org/eclipse/downloads/. unzipped that zip file into your desired location. i used here eclipse v3.4.1.
  • Download tomcat. i used tomcat 5.27 (Windows Service Installer). during installation set the jre path. I guess you have already installed jdk 1.4 or later. install tomcat plugins: download plugins from http://www.easyeclipse.org/site/plugins/tomcat-launcher.html. here you can get some installment information too.

Now run the tomcat server and check this address – http://localhost:8080. you can change the port number by editing tomcat-home/conf/server.xml file.

  • After install tomcat plugins restart your the eclipse by execute the following commands.
    command prompt > eclipse -clean
    Now you can see tomcat in eclipse. Before using the plugin, you must set Tomcat home : Window > Preferences, select Tomcat and set the Tomcat version and Tomcat home fields.
  • Ant : download ant. unzipped your desired location. update your computer env path.click win-key + pause/break key and click Advance tab and select environment variables. create new system variable –
    ANT_HOME=D:/ Development Tools/Java/apache-ant-1.7.0-bin
    update path variable
    PATH= …..;D:/Development Tools/Java/apache-ant-1.7.0-bin/bin

Ensure these two environment variable are set by typing following on command prompt
echo %ANT_HOME%
echo %PATH%

lets check is it installed. open command prompt -> ant press enter. it shows-Buildfile: build.xml does not exist!.  that means it is works.

lets configure eclipse ant. Click Windose -> Prefreneces -> Ant (Runtime) select Classpath tab. now click Ant Home button and set the ant home location.

  • Download Maven.Unzip the maven download to your computer:  D:/Development Tools/Java/maven-2.0.9. update your computer env path.click win-key + pause/break key and click Advance tab and select environment variables. create new system variable –
    M2_HOME=D:/Development Tools/Java/maven-2.0.9
    update path variable
    PATH= …..;D:/Development Tools/Java/maven-2.0.9/bin  Ensure these two environment variable are set by typing following on command prompt

    echo %JAVA_HOME%
    echo %M2_HOME%
    echo %PATH%

type in command prompt ->mvn –version . if this command shows its version that means it works.

You can install maven plugins. use http://m2eclipse.codehaus.org/update/ this link for install this plugin (read the following paragraph for how to install plugins).

please use this link -http://m2eclipse.sonatype.org/update/  this is more update than http://m2eclipse.codehaus.org/update/

  • Click help menu of eclipse and select software update. select available software tab and click add site button. put http://springide.org/updatesite value into location and press OK. after restart you eclipse you will get spring features. lets check is it installed. Press ctrl+N , type spring into new wizard text field then you can see spring tree.
  • SVN: you can download svn client version.

for plugings -Click help menu of eclipse and select software update. select available software tab and click add site button. put http://subclipse.tigris.org/update_1.0.x    value into location and press OK.

or visit this page – http://subclipse.tigris.org/

after restart you eclipse click window->open perspective->other you will see SVN Repository Exploring.

Note – for unzipped i used 7-Zip software this is free.