Title: The Freedom to Achieve
1The Freedom to Achieve
- Introduction to Spring
- Platinum Solutions
- All Hands Meeting
- 2/18/2007
- Navid Norouzi
2What is Spring?
- An open source J2EE Framework
- Spring is made up of different components
- Spring Core
- Spring Web
- Spring ORM
- Spring DAO
- Spring AOP
- Other
3Spring Components
4Why Use It?
- Enables better use of POJOs (Plain Old Java
Objects) - Promotes Decoupling, Reusability, Testing
- Built to integrate with other Popular Open Source
Projects (e.g. Hibernate)
5Inversion of Control (IoC)?
- Inversion of Control is a programming principle
- Also known as Dependency Injection
- IoC Promotes Decoupling, Reusability, Testing (as
mentioned before)
6Non-IoC Example
interface Cup drink() isReusable()
class PaperCup implements Cup private String
liquid Public PaperCup (liquid) this.liquid
liquid drink() isReusable() return
false
7Non-IoC Example Continued
class ThirstyPerson Private Cup myCup public
ThirstyPerson() myCup new PaperCup(WATER)
- Weve coupled our Thirsty Person to drink only
water if he/she wants to use their Cup
8IoC Version
- We can set our Thirsty Person to drink other
liquids besides water (de-coupling) - First, we need to make a change to our
ThirstyPerson class - We will allow the liquid to be set via the
constructor and a setter method
9IoC Version Continued
class ThirstyPerson Private Cup myCup //
constructor injection public ThirstyPerson(LIQUID)
myCup new PaperCup(LIQUID)
10IoC Version Continued
- Next we wire the liquid we want with the
Thirsty Person in a configuration file outside of
the code - A XML file serves as the configuration file
- Spring will process the configuration file
11Spring Beans Definition XML
- This will tell Spring to instantiate a
ThirstyPerson object with Apple Juice as the
liquid passed to the constructor
12IoC Version Continued
class ThirstyPerson Private Cup myCup public
ThirstyPerson() // empty constructor //
setter injection public setLiquid(LIQUID)
myCup new PaperCup(LIQUID)
13Spring Beans Definition XML
- This will tell Spring to instantiate a
ThirstyPerson object, and then call the setter
method of the liquid property with the value of
Coffee
14IoC Version Continued
- We used an XML file to instantiate our object
and decoupled our Thirsty Person from being
forced to start with water - Note that Thirsty Person does not really have a
LIQUID property, but a CUP property and that
Thirsty Person is coupled with a Paper Cup no
matter what - Lets make our objects a little cleaner
15Further Decoupling
class ThirstyPerson Private Cup myCup // empty
constructor public ThirstyPerson() //
constructor injection public ThirstyPerson(CUP)
myCup CUP // setter injection public
setCup(CUP) myCup CUP
16Further Decoupling Continued
interface Cup drink() setLiquid() getLiquid() isRe
usable() setReusable()
class AnyCup implements Cup private String
liquid private boolean reusable // empty
constructor Public AnyCup () drink() setLiquid(
) getLiquid() isReusable() setReusable()
17Further Decoupling Continued
- Now ThirstyPerson class only deals with CUPs, and
our CUP class is now generic - We need to change our Spring XML file since we
changed our Class - We can now create CUP beans and pass them into
our ThirstyPerson
18Further Decoupling Continued
19Further Decoupling Continued
20Introduction to Spring
- If your properties are REQUIRED to be set, use
Constructor Injection, to force the properties to
get set during instantiation - We have decoupled (ThirstyPerson) and reused
(AnyCup) - Most likely, we should further de-couple our Cups
from their fixed liquid contents - We can now make changes to the choice of drink,
outside of our compiled code
21Spring Promotes Testing
- Now well take a look at a simple JUnit Test Case
- Our test will create an Application from our
configuration file
22Spring Promotes Testing
class CeramicCupTest extends TestCase Private
AnyCup anyCeramicCup setUp() // create the
application from our configuration file // this
will instantiate our beans ApplicationContext
context new ClassPathXmlApplicationContext(Ou
rSpringXMLFile.xml) // ceramicCup is
defined as a bean in our configuration
file anyCeramicCup (AnyCup) context.getBean(ce
ramiCup) testCeramiCup() assertTrue(anyCera
micCup.isReusable)
23Spring Promotes Testing
- Can more easily test your Application using Stubs
and Mocks - Stubs
- Provide canned answers to calls made during a
test replacing a method with code that returns
a specific result - Mock Objects
- Are preprogrammed with expectations so you can
assert if the expectation was met (was a method
called?)
24Introduction to Spring
- In Summary, Spring, due to its Inversion of
Control nature, promotes - Decoupling
- Reusability
- Testing
- Programming to Interfaces
- Centralized Configuration
- Other
25Spring Application Context Lifecycle
- Initialization
- Application resources (beans) are created
- Use
- Used by application services
- Destruction
- Releases resources for garbage collection
26Bean properties
- Beans also have various properties
- Scope
- Session a new instance is created once per
session - Request a new instance is created once per
request - Other
- Other properties, such as Lazy Instantiation
- Will not be created until referenced
- Beans can have inheritance!
- Inner Beans
- An anonymous bean only available to its parent
bean - Other
27Spring AOP?
- AOP
- Stands for Aspect Oriented Programming
- Object Oriented Programming
- Concentrates on Classes
- Aspect Oriented Programming
- Concentrates on Aspects
28Spring AOP Continued
- AOP works with the Compiler
- Modularizes concerns in your Application,
concerns that cross-cut across your application - Logging
- Tracing
- Transactions
- Security
- Caching
- Error Handling
- Custom Business Rules
- Other
- Can provide a lot of flexibility and power, can
also be abused - Example Using AOP, log each time a specific
variable is changed via its setter method
29Other Spring Components
- Spring Data Access
- Spring provides libraries that support and
simplify writing data access code - Supports JDBC, Hibernate, iBatis, JPA, other
- Spring Web
- Spring MVC
- Request driven framework (Similar to Struts)
- Requests are routed through Controllers by the
Dispatcher Servlet - The Controllers determine how to render the
results - Spring Web Flow
- State and Flow management
- Wizards, Step-by-Step navigation
- Adds a scope Page, Request, Flow, Session,
Application
30Other Spring Components Continued
- Spring Security
- Formerly known as ACEGI Security
- Where did the name come from A B C D E F G H I
- Spring Web Services
- Spring Remoting
- In place of direct RMI
- Spring JMS
- Simplified Messaging / Producer and Consumer
cases - Other Components
31Introduction to Spring