Professional Documents
Culture Documents
com
2008 coreservlets.com
For live Spring & Hibernate training, see courses at http://courses.coreservlets.com/. t htt // l t /
Taught by the experts that brought you this tutorial. Available at public venues, or customized versions venues can be held on-site at your organization.
C Courses d developed and t l d d taught b M t H ll ht by Marty Hall Courses developed and taught by EE Training: http://courses.coreservlets.com/ Customized Java coreservlets.com experts (edited by Marty)
Spring, Hibernate/JPA, EJB3, Ruby/Rails Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, custom mix of topics
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Contact hall@coreservlets.com for details Developed and taught by well-known author and developer. At public venues or onsite at your location.
2008 coreservlets.com
Motivation M ti ti
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Spring Approach p g pp
Products based on simplicity Maintainable M i t i bl systems t Framework-independent software Portable components Testable components Reliable and predictable systems
Pure Java
Founded on POJO-based development
O di Ordinary Java classes that follow no special APIs J l h f ll i l 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
10
11
Replaces generic corporate libraries Mitigates custom integration activities Consistency eases integration because spring platform is easy to use
Modular
Helps only where needed
M d l i allows only relevant components to be Modularity ll l l 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 I t f Interfaces are clear and consistent l d it t
12
Widely Available y
Spring is integrated into numerous frameworks f k Broad adoption possible because the container is portable and lightweight
The container itself is designed as a POJO
13
Spring Jobs p g
From indeed.com
Claims to compile data from most major job sites
14
2008 coreservlets.com
Spring S t S i Setup
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Spring Download p g
http://www.springframework.org/download
Current version: 2.5.5 (6/2008)
Requires JDK 1.4+
16
Empty applicationContext.xml. For new Java source files. Minimum Spring JARs for API and runtime access to the Spring IoC container Optional Apache ANT build configuration Optional Maven 2 build configuration
17
test package
19
test package
20
Spring Documentation p g
Top-level documentation page
http://www.springframework.org/documentation
Wiki
h // http://opensource.atlassian.com/confluence/spring l i / fl / i
Forum
http://forum springframework org http://forum.springframework.org
Books
Spring Recipes. APress 2008 Spring in Action. Manning 2007 Agile Java Development with Spring, Hibernate and Eclipse. Sams 2006
21
2008 coreservlets.com
Configure the Spring IoC container I t ti t the Spring IoC container Instantiate th S i I C t i Acquire the object from the Spring IoC container
The client must only have knowledge of the interface, HelloWorld
23
public class HelloWorldImpl implements HelloWorld { public void execute() { bli id t () System.out.println("Hello World!"); } }
24
25
Hello World!
26
2008 coreservlets.com
Introduction
Plain Old Java Object What is it?
Business logic F Framework i d k independent d
Features
Portable Testable Flexible
28
Agents
BookLibrary and Client
I t Interactions 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
31
32
33
34
35
Book Implementation p
public class Book { private String title; public Book(String title) { this.title = title; } public String getTitle() { return title; } public String toString() { return title; } }
36
BookLibrary Implementation y p
import java.util.List; public interface BookLibrary { public List<Book> search(String title); }
37
BookLibrary Implementation y p
public class JavaBookLibrary implements BookLibrary { private List<Book> books; public JavaBookLibrary() { this.books = Arrays.<Book>asList( new Book("Core Servlets and JavaServer Pages"), new Book("More Servlets and JavaServer Pages")); } public List<Book> search(String title) { List<Book>results = new ArrayList<Book>(); for(Book book : books){ if(book.getTitle().contains(title)){ if(b k tTitl () t i (titl )){ results.add(book); } } return results; } }
38
Client Implementation p
public class BookReader { private BookLibrary bookLibrary; public BookReader() { this.bookLibrary = new JavaBookLibrary(); } public List read() { List<Book> books = bookLibrary.search("Java"); for(Book book : books) { System.out.printf("Reading: %s%n", book); } return books; } }
39
2008 coreservlets.com
Runtime E i R ti Environment t
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Runtime Model
Transition from a class system to an object system An object model provides a unique and specific instantiation of the class specification
Class Model Object Model
41
Runtime Context
Multiple deployment contexts Complex object models should be portable Object models should be configurable to support changes between environments t h b t i t
42
Runtime Example p
public class Main { public static void main(String[] args) { BookReader client = new BookReader(); List<Book> books = client.read(); System.out.printf("Client read: %s books%n", books.size()); } }
Standard output
Reading: Core Servlets and JavaServer Pages Reading: More Servlets and JavaServer Pages Client read: 2 books
43
Model Analysis y
Hard-coded implementation choices
Object model cannot be reconfigured Future implementation types cannot be used without modifying and rebuilding BookReader
44
Model Analysis y
Hard-coded model configuration
Object model is not portable
45
2008 coreservlets.com
Dependency Injection p y j
Delivers object dependencies at runtime Encourages the separation of responsibilities Wh used with the interface pattern When d ith th i t f tt
Isolates implementations from clients Minimizes the impact on clients when implementations evolve
47
48
public List read() { List<Book> books = bookLibrary.search("Java"); for(Book book f (B k b k : b k ){ books){ System.out.printf("Reading: %s%n", book); } return books; } }
49
Interface type
Runtime Example p
public class Main {
Dependency creation moved out of BookReader Dependency injection
public static void main(String[] args) { BookLibrary service = new JavaBookLibrary(); BookReader client = new BookReader(service); List<Book>books = client.read(); System.out.printf("Client read: %s books%n", books.size()); b k i ()) } }
Reading: Core Servlets and JavaServer Pages Reading: More Servlets and JavaServer Pages Client read: 2 books
51
Standard output
Model Analysis y
Dynamic implementation choices
Object model can be reconfigured Future implementation types can be used without modifying and rebuilding BookReader
52
Model Analysis y
Hard-coded model configuration
Object model is not portable
53
2008 coreservlets.com
Introduction
Inversion of Control Separate program control responsibilities
Object instantiation D Dependency i j i d injection
55
56
IoC Framework
Service provider or plugin framework
Interface Providers Registration system Access API
-- Joshua Bloch from Effective Java
Process
Framework uses supplied APIs
Frame ork handles creation Framework Framework handles dependency injection
public ServiceProviderFramework(){ this.bookReader = new BookReader(new JavaBookLibrary()); } public BookReader getBookReaderInstance() { return this.bookReader; } }
58
Access API
BookReader client = framework.getBookReaderInstance(); List books = client.read(); System.out.printf("Client read: %s books%n", books.size()); b k i ()) } }
Reading: Core Servlets and JavaServer Pages Reading: More Servlets and JavaServer Pages Client read: 2 books
59
Standard output
Model Analysis y
Dynamic implementation choices Portable model configuration
60
2008 coreservlets.com
Wrapup
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary y
Develop POJOs
A id f Avoid framework d k dependencies d i Capture business logic Avoid implementation commitments by using inversion of control and dependency injection patterns
Register POJOs
D l POJOs using XML b Declare POJO i bean elements l t Use bean attributes id and class for specifying the name and type,
respectively
Summary ( y (Continued) )
Instantiate a Spring IoC container
U the B F Use h BeanFactory i l implementation i
ClassPathXmlApplicationContext
2008 coreservlets.com
Questions? Q ti ?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.