Академический Документы
Профессиональный Документы
Культура Документы
com
© 2008 coreservlets.com
© 2008 coreservlets.com
M ti ti
Motivation
Pure Java
• Founded on POJO-based development
– Ordinary
O di Java
J classes
l that
h follow
f ll no special
i l APIs
API
• Non-invasive for pre-existing POJOs
• Rewards framework-independent business
logic
• Encourages new software to be written as
POJOs
POJO
• Results in highly portable, reusable, and
verifiable software
Modular
• Helps only where needed
–MModularity
d l i allows
ll only
l relevant
l components to be
b
introduced into the application
– For instance choose one:
• Spring BeanFactory
• Spring JMX
• Spring JDBC
– Framework can be interfaced in deep or shallow layers.
– Interfaces are consistent at each layer
• Turn-key solution
– Spring components can be integrated quickly, with
minimal effort and predictable results
– Interfaces
I t f are clear
l andd consistent
it t
Spring
p g Jobs
• From indeed.com
– Claims to compile data from most major job sites
S i Setup
Spring S t
Spring
p g Download
• http://www.springframework.org/download
– Current version: 2.5.5 (6/2008)
• Requires JDK 1.4+
– spring-framework-2.5.5-with-dependencies.zip
spring-framework-2 5 5-with-dependencies zip
• Spring Framework binaries and source
• Third-party binaries
• Documentation
D t ti
– API
– HTML reference
– Project samples
– HOW-TO guides
Path
at Description
esc pt o
© 2008 coreservlets.com
S i Hello
Spring H ll World
W ld
public
bli void
id execute()
t () {
System.out.println("Hello World!");
}
<bean id="helloWorld"
class="coreservlets.HelloWorldImpl" />
</beans>
Executing
g Spring
p g Hello World
import org.springframework.beans.factory.*;
import org.springframework.context.support.
org.springframework.context.support.*;
;
HelloWorld helloWorld =
(HelloWorld) beanFactory.getBean("helloWorld");
helloWorld.execute();
}
}
Standard output
Hello World!
26 Java EE training: http://courses.coreservlets.com
© 2008 coreservlets.com
Background:
POJO Development
Introduction
• Plain Old Java Object
• What is it?
– Business logic
– Framework
F k iindependent
d d
• What it’s not
– Limited to the value object pattern
– Framework implementation software
• Features
– Portable
– Testable
– Flexible
28 Java EE training: http://courses.coreservlets.com
POJO Development
p Process
• Describe the system agents and interactions
– POJO behavioral classes, domain model, and
dependencies
• Determine component responsibilities
– Methods
• Identify information items discovered during
program execution
– Method parameters
• Identify information available during
initialization
– Initialization parameters for constructor, setter, or factory
• Agents
– BookLibrary and Client
• Interactions
I t ti
– Client uses BookLibrary
– BookLibrary aggregates Book
• Responsibilities
– BookLibrary must search for books by title
– Clients must supply search parameters; i.e. title values
30 Java EE training: http://courses.coreservlets.com
POJO Development Process
• Develop implementation
Book Implementation
p
import java.util.List;
BookLibrary
y Implementation
p
public class JavaBookLibrary
implements BookLibrary {
public JavaBookLibrary() {
this.books = Arrays.<Book>asList(
new Book("Core Servlets and JavaServer Pages"),
new Book("More Servlets and JavaServer Pages"));
}
public BookReader() {
this.bookLibrary = new JavaBookLibrary();
}
© 2008 coreservlets.com
R ti
Runtime E
Environment
i t
Runtime Context
• Multiple deployment contexts
• Complex object models should be portable
• Object models should be configurable to
supportt changes
h between
b t environments
i t
}
}
Standard output
Reading: Core Servlets and JavaServer Pages
Reading: More Servlets and JavaServer Pages
Client read: 2 books
43 Java EE training: http://courses.coreservlets.com
Model Analysis
y
• Hard-coded implementation choices
– Object model cannot be reconfigured
– Future implementation types cannot be used without
modifying and rebuilding BookReader
© 2008 coreservlets.com
Manual (Non
(Non-Spring)
Spring)
Dependency Injection
Dependency
p y Injection
j Process
• Design depending types to receive
i
implementations
l i
– Allow dependencies to be supplied using property setters
or constructors
– Other dependency injection methods are also available,
such as field injection, but requires third-party or Java
reflection support
• Avoid constructing objects from the client
p
to fulfill dependencies
– For example, do not use the new operator to manage
services
public BookReader() {
this.bookLibrary = new JavaBookLibrary();
}
Creates dependency
public List read() {
List<Book> books = bookLibrary.search("Java");
f (B k book
for(Book b k : books){
b k ){
System.out.printf("Reading: %s%n", book);
}
return books;
}
}
49 Java EE training: http://courses.coreservlets.com
Dependency
p y Injection
j Example
p
Interface type
public List read() { NOT the implementation type
List<Book> books = bookLibrary.search("Java");
f (B k book
for(Book b k : books){
b k ){
System.out.printf("Reading: %s%n", book);
}
return books;
}
}
50 Java EE training: http://courses.coreservlets.com
Runtime Example
p
public class Main {
Dependency creation moved out of BookReader
Dependency
public static void main(String[] args) { injection
List<Book>books = client.read();
Model Analysis
y
• Dynamic implementation choices
– Object model can be reconfigured
– Future implementation types can be used without
modifying and rebuilding BookReader
© 2008 coreservlets.com
Manual (Non
(Non-Spring)
Spring)
Inversion of Control
• Process
– Framework uses supplied APIs
• Frame
Framework
ork handles creation
• Framework handles dependency injection
– Runtime context uses framework
Model Analysis
y
• Dynamic implementation choices
• Portable model configuration
Wrapup
Summary
y
• Develop POJOs
– A
Avoid
id fframework
k ddependencies
d i
– Capture business logic
– Avoid implementation commitments by using inversion of control
and dependency injection patterns
• Create a new XML file, applicationContext.xml,
based on spring-beans.xsd
p g
– Place applicationContext.xml in the classpath
• Register POJOs
– D
Declare
l POJOs
POJO usingi XML beanb elements
l t
– Use bean attributes id and class for specifying the name and type,
respectively
© 2008 coreservlets.com
Q
Questions?
ti ?