What is Spring?
• Spring is a framework with many modules
– To simplify many different Java EE tasks

• Core module: dependency injection
– L you define an XML file that specifies which beans Lets d fi fil h ifi hi h b are created and what their properties are
• Simplifies OOP by p p y promoting loose coupling between g p g classes. Also called “Inversion of Control” (IoC) • Small example shown in this lecture; details in next lecture

• Second most important module: AOP
– “Aspect Oriented Programming” refers to the ability to add side effects to a class’ method calls without modifying the class’ source code – Lets you separate business logic from system services
7

Downloading
• http://www.springsource.org/download
– Choose latest released version
• 2.5.6 as of 12/2008

– Choose “Community Download” y
• This is free and open source • There is also Enterprise version that comes with paid support

– Choose version with dependencies
• Includes required and optional JAR files, documentation, and samples
– You only have to use the JAR files you want, so there is no harm in downloading the full version

• spring-framework-2.5.6-with-dependencies.zip as of 12/08

– Alternative: download spring-blank.zip
• Eclipse project with two required JAR files, blank applicationContext.xml, and Eclipse Spring IDE nature • From coreservlets.com; see link on title page of this tutorial.

12

Installation
• Unzip into directory of your choice
– I will refer to this later as spring-install ill f t thi l t i i t ll

Making a Bean Definition File
• Idea
– XML file defines objects (beans) and gives them names. Java code will load file and refer to objects by name. – File is most commonly loaded relative to the class path, path so is typically placed in src folder – File can have any name
• Common choices are beans.xml and applicationContext.xml

• Spring IDE is not required
– This file can be created and edited manually
• Grab one from the samples that come with Spring
– Make su e it does not refer to fancy features you a e not us g yet a e sure t ot e e a cy eatu es are ot using – Delete everything between <beans …> and </beans>

Goal: Loose Coupling
• Geometric analysis
– You have a program that computes areas for various collections of shapes. You want to avoid code that depends on any particular type of shape, since the varieties of shapes keep changing.

• Notes
– No imports of Spring packages
• No ties in any way to Spring

– C d that only uses the area (or inherited methods like Code h l h ( i h i d h d lik toString) should refer only to Shape
• But interfaces cannot be used everywhere: code that uses y more specific info (e.g., the radius of a Circle that implements Shape) will need to use concrete type
25

Concrete Class: Rectangle
public class Rectangle implements Shape { private double length, width; public Rectangle() {}
The driver class will use only getArea and methods inherited from Object, so will refer to Shape, not Rectangle.

The bean definition in the upcoming applicationContext.xml file will not specify any constructor arguments. So, the zero-arg constructor will be used.