Spring Tutorial

Only available on StudyMode
  • Topic: Return statement, Java, Source code
  • Pages : 27 (6001 words )
  • Download(s) : 87
  • Published : April 4, 2013
Open Document
Text Preview
Spring Framework Tutorial
Isabelle Muszynski 15 April 2003

Chapter 1

Introduction
This tutorial covers the main packages in the Spring Framework. For full details, we refer you to Rod Johnson’s book, Expert One-on-One J2EE Design and Development, published by Wrox Press in 2002. The book’s ISBN number is 1-86100-784-1. The code for the Spring Framework is contained in package com.interface21. We provide UML diagrams for the subpackages, as well as code samples.

1.1

Setting up for the samples

The samples have been written using the MySQL database (www.mysql.com), Tomcat (jakarta.apache.org/tomcat) and JBoss(www.jboss.org). We refer you to these web sites for setting up the environment correctly. In MySQL, create a database named test, a user test with password test, and grant user test all rights: grant all on test.* to test@localhost identified by ’test’; If you use a different database, read the comments at the top of the samples files, they will direct you to the spots in the code you will have to change for your database.

1.2

Building the tutorials

The tutorials can be built using the provided Ant script (you can get ant at http://jakarta.apache.org/ant). You may need to edit the build.properties file to set paths correctly for your system.

1.3

Running the tutorials

We provide a shell script, runtest.sh, and a batch file, runtest.bat, for running a single tutorial. You may need to edit these files to set the paths correctly.

1

Chapter 2

The JDBC Package
Data access is taken care of in the com.interface21.jdbc package and its subpackages. These packages are non-intrusive, and so you can use them separately from the rest of the framework to implement persistence in your application. There are two main packages, com.interface21.jdbc.core, which implements low-level persistence, and the high-level com.interface21.jdbc.object, which presents an object-oriented view of the low-level package.

2.1

Exception Handling

Clients of the package - i.e. you, as the developer - should not have to concern themselves with SQLExceptions, as these will not be very informative to your users. Also, because SQLExceptions are checked exceptions, they have to be caught. The Spring Framework takes a different approach : checked SQLExceptions are converted to unchecked DataAccessException and its subclasses. A number of pathological conditions are recognized, through the SQLExceptionTranslater interface and its implementing classes, and will result in subclasses of DataAccessException being thrown. For example, a unique key integrity violation will cause a DataIntegrityViolationException to be thrown. It is up to you, as the developer, to catch this exception – if your code can recover from it – or not, thereby letting it percolate upwards.

2

2.2

The com.interface21.jdbc.object Package

The com.interface21.jdbc.object package provides a hig-level, JDO-like environment for persistence. Queries, updates and stored procedures are modeled as reusable, threadsafe objects. The abstract RdbmsOperation class is the root of the hierarchy. It holds a javax.sql.DataSource and a SQL string as instance variables, and allows bind parameters to be declared. Before being used, a RdbmsOperation must be compiled. The exact meaning of the compilation operation varies between subclasses. After compilation, no more parameters can be added, but the operation can be performed repeatedly. Parameters are declared using the declareParameter() method. The abstract SqlOperation class extends RdbmsOperation and is used as a base class for queries and updates. The compilation process checks that the number of parameters matches the number of parameter placeholders in the SQL string. The SqlQuery class is used for queries; it requires the user to implement method newResultReader, which should return a ResultReader for the returned ResultSet. The MappingSqlQuery class is a subclass of SqlQuery which provides a default...
tracking img