01 spring foundations
TRANSCRIPT
-
8/8/2019 01 Spring Foundations
1/31
2008 coreservlets.com
Foundations
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/spring.html
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.
2008 coreservlets.com
For live Spring & Hibernate training, seecourses a p: courses.coreserv e s.com .Taught by the experts that brought you this tutorial.
,can be held on-site at your organization.
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.
ourses eve ope an aug y ar y a Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, c ustom mix of topics
Courses developed and taught by coreservlets.com experts (edited by Marty) Spring, Hibernate/JPA, EJB3, Ruby/Rails
Contact [email protected] for details
-
8/8/2019 01 Spring Foundations
2/31
Topics in This Section
Motivation Spring Hello World POJO development Runtime environment Dependency injection Inversion of control
Java EE trainin : htt ://courses.coreservlets.com5
2008 coreservlets.com
o va on
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.
-
8/8/2019 01 Spring Foundations
3/31
Software Development
Solutions are complex equ remen s are cons an y n ux
Software architecture must be flexible
Java EE trainin : htt ://courses.coreservlets.com7
EJB 2.0 Approach Complex products nma n a na e sys ems Non-portable, framework-committed
Unpredictable systems
Java EE trainin : htt ://courses.coreservlets.com8
-
8/8/2019 01 Spring Foundations
4/31
Spring Approach
Products based on simplicity a n a na e sys ems
Framework-independent software
Testable components
Java EE trainin : htt ://courses.coreservlets.com9
Pure Java Founded on POJO-based development
r nary ava c asses t at o ow no spec a s Non-invasive for pre-existing POJOs -
logic
Encourages new software to be written asPOJOs Results in highly portable, reusable, and
Java EE trainin : htt ://courses.coreservlets.com10
-
8/8/2019 01 Spring Foundations
5/31
More With Less Custom Code
Expand capabilities with less code Extensive and tested service abstractions
Email JMX JDBC etc
Replaces generic corporate libraries
Mitigates custom integration activities
Java EE trainin : htt ://courses.coreservlets.com
Consistency eases integration becausespring platform is easy to use
11
Modular Helps only where needed
o u ar ty a ows on y re evant components to eintroduced 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
- Spring components can be integrated quickly, with
minimal effort and predictable results
Java EE trainin : htt ://courses.coreservlets.com
n er aces are c ear an cons s en
12
-
8/8/2019 01 Spring Foundations
6/31
Widely Available
Spring is integrated into numerousframeworks
Broad adoption possible because the
The container itself is designed as a POJO
Performance overhead is rarely aconsideration
Java EE trainin : htt ://courses.coreservlets.com13
Spring Jobs From indeed.com
Claims to compile data from most major job sites
Java EE trainin : htt ://courses.coreservlets.com14
-
8/8/2019 01 Spring Foundations
7/31
2008 coreservlets.com
pr ng e up
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 http://www.springframework.org/download
Current version: 2.5.5 (6/2008) Requires JDK 1.4+
- - - -. . . Spring Framework binaries and source
Third-party binaries ocumen a on API HTML reference Project samples HOW-TO guides
Java EE trainin : htt ://courses.coreservlets.com16
-
8/8/2019 01 Spring Foundations
8/31
Spring Blank Project
spring-blank.zip Available from
http://courses.coreservlets.com/Course-Materials/spring.html
Path Descri tion
src Empty applicationContext.xml. For new Java source files.
lib Minimum Spring JARs for API and runtime access to the Spring IoCcontainer
build.xml Optional Apache ANT build configuration
pom.xml Optional Maven 2 build configuration
Java EE trainin : htt ://courses.coreservlets.com17
Spring Blank Project and Download spring-blank.zip
http://courses.coreservlets.com/Course-Materials/spring.html
Import archive as an existing project into
From the Eclipse menu bar select File and Import
From the Im ort Select dialo , select ExistinProjects into Workspace and Next
From the Import (Import Projects) dialog, select the
Locate and select spring-blank.zip and select Open Verif the ro ect entr , s rin -blank , to be resent in
Java EE trainin : htt ://courses.coreservlets.com
the project list Select Finish
18
-
8/8/2019 01 Spring Foundations
9/31
Spring Blank Project and
Download and unpack spring-blank.zip Install Apache Ant, version 1.6.5+ Execute various Ant build commands
Command Description
clean Removes the build directory target and all nestedbuild artifacts
compile Compiles production Java source contents under src/main/java and places class binaries intotarget/classes
test Executes the compile command and compiles andexecutes tests found under src/test/java
package Packages production Java source and resource
Java EE trainin : htt ://courses.coreservlets.com19
contents into a jar file. The jar package is placed inthe build directory, target
Spring Blank Project and Download and unpack spring-blank.zip Execute various Maven commands
Command Description
build artifacts
compile Compiles production Java source contents under
target/classes
test Executes the compile command and compiles andexecutes tests found under src/test/java
package Packages production Java source and resourcecontents into a jar file. The jar package is placed inthe build directory, target
Java EE trainin : htt ://courses.coreservlets.com
See pom.xml configuration for additional dependencyoptions
20
-
8/8/2019 01 Spring Foundations
10/31
Spring Documentation
Top-level documentation page http://www.springframework.org/documentation
Wiki ttp: opensource.at ass an.com con uence spr ng
Forum . .
Books . Spring in Action. Manning 2007 Agile Java Development with Spring, Hibernate and
Java EE trainin : htt ://courses.coreservlets.com
Eclipse. Sams 2006
21
2008 coreservlets.com
pr ng e o or
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.
-
8/8/2019 01 Spring Foundations
11/31
Spring Hello World
Code a plain Java class model se e n er ace pa ern y co ng a e o or n er ace an a
HelloWorldImpl implementation
Configure the Spring IoC container ns an a e e pr ng o con a ner Acquire the object from the Spring IoC container
The client must only have knowledge of the interface, HelloWorld
Java EE trainin : htt ://courses.coreservlets.com23
Plain Java Class Model public interface HelloWorld {
public void execute();
}
public class HelloWorldImplimplements HelloWorld {
pu c vo execute() {System.out.println("Hello World!");
}
Java EE trainin : htt ://courses.coreservlets.com
}
24
-
8/8/2019 01 Spring Foundations
12/31
Spring IoC Configuration
Java EE trainin : htt ://courses.coreservlets.com25
Executing Spring Hello Worldimport org.springframework.beans.factory.*;
*
public class Main{ public static void main(String[] args) {
BeanFactory beanFactory =new ClassPathXmlApplicationContext (
"applicationContext.xml");
HelloWorld Interface
HelloWorld helloWorld =( HelloWorld ) beanFactory.getBean(" helloWorld ");
Spring-managed bean name
helloWorld.execute();}
Java EE trainin : htt ://courses.coreservlets.com26
Standard output
Hello World!
-
8/8/2019 01 Spring Foundations
13/31
2008 coreservlets.com
POJO Development
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.
Introduction Plain Old Java Object What is it?
Business logic ramewor n epen ent
What its not Framework implementation software
Portable Testable
Java EE trainin : htt ://courses.coreservlets.com
Flexible
28
-
8/8/2019 01 Spring Foundations
14/31
POJO Development Process
Describe the system agents and interactions POJO behavioral classes, domain model, and
dependencies
Methods
program execution Method parameters
Identify information available during
initialization
Java EE trainin : htt ://courses.coreservlets.com
Initialization parameters for constructor, setter, or factory
29
POJO Development Process
Agents BookLibrary and Client
n erac ons Client uses BookLibrary
Responsibilities
Java EE trainin : htt ://courses.coreservlets.com
Clients must supply search parameters; i.e. title values
30
-
8/8/2019 01 Spring Foundations
15/31
POJO Development Process
Develop implementation
Java EE trainin : htt ://courses.coreservlets.com31
POJO Development Process
Plan for change
Java EE trainin : htt ://courses.coreservlets.com32
-
8/8/2019 01 Spring Foundations
16/31
POJO Development Process
Plan for new and additional dependencies
Java EE trainin : htt ://courses.coreservlets.com33
POJO Development Process
Plan for complex configuration requirements
Java EE trainin : htt ://courses.coreservlets.com34
-
8/8/2019 01 Spring Foundations
17/31
POJO Implementation Example
Java EE trainin : htt ://courses.coreservlets.com35
Book Implementation
private String title;
public Book(String title) {this.title = title;
}
public String getTitle() {return title;
}
public String toString() {
Java EE trainin : htt ://courses.coreservlets.com
}
}36
-
8/8/2019 01 Spring Foundations
18/31
BookLibrary Implementation
public interface BookLibrary {
public List search(String title);
}
Java EE trainin : htt ://courses.coreservlets.com37
BookLibrary Implementation public class JavaBookLibrary
implements BookLibrary {
private List books;
this.books = Arrays.asList(
new Book("Core Servlets and JavaServer Pages"),new Book("More Servlets and JavaServer Pages"));
}
public List search(String title) {Listresults = new ArrayList();for(Book book : books){
( oo .getT t e().conta ns(t t e)){results.add(book);
}}
Java EE trainin : htt ://courses.coreservlets.com
}
}
38
-
8/8/2019 01 Spring Foundations
19/31
Client Implementation
ublic class BookReader
private BookLibrary bookLibrary;
public BookReader() {this.bookLibrary = new JavaBookLibrary();
}
public List read() {List books = bookLibrary.search("Java");for(Book book : books) {
System.out.printf("Reading: %s%n", book);}
return books;
Java EE trainin : htt ://courses.coreservlets.com
}}
39
2008 coreservlets.com
un me nv ronmen
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.
-
8/8/2019 01 Spring Foundations
20/31
Runtime Model
Transition from a class system to an objectsystem
An object model provides a unique and
specification
Java EE trainin : htt ://courses.coreservlets.com41
Runtime Context Multiple deployment contexts Complex object models should be portable Object models should be configurable to
suppor c anges e ween env ronmen s
Java EE trainin : htt ://courses.coreservlets.com42
-
8/8/2019 01 Spring Foundations
21/31
Runtime Example public class Main {
public static void main(String[] args) {
BookReader client = new BookReader();
List books = client.read();
System.out.printf("Client read: %s books%n",books.size());
}}
Standard output
Readin : Core Servlets and JavaServer Pa es
Java EE trainin : htt ://courses.coreservlets.com43
Reading: More Servlets and JavaServer PagesClient read: 2 books
Model Analysis Hard-coded implementation choices
Object model cannot be reconfigured Future implementation types cannot be used without
Java EE trainin : htt ://courses.coreservlets.com44
-
8/8/2019 01 Spring Foundations
22/31
Model Analysis
Hard-coded model configuration Object model is not portable
Java EE trainin : htt ://courses.coreservlets.com45
2008 coreservlets.com
-Dependency Injection
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.
-
8/8/2019 01 Spring Foundations
23/31
Dependency Injection
Delivers object dependencies at runtime Encourages the separation of
responsibilities en use w e n er ace pa ern
Isolates implementations from clients
evolve
Java EE trainin : htt ://courses.coreservlets.com47
Dependency Injection Process Design depending types to receive
implementations Allow dependencies to be supplied using property setters
Other dependency injection methods are also available,such as field injection, but requires third-party or Java
Avoid constructing objects from the clientto fulfill dependencies For example, do not use the new operator to manage
services
Java EE trainin : htt ://courses.coreservlets.com48
-
8/8/2019 01 Spring Foundations
24/31
Dependency Injection Candidate
private BookLibrary bookLibrary;
public BookReader() {this.bookLibrary = new JavaBookLibrary();
}
public List read() {List books = bookLibrary.search("Java");
Creates dependency
or(Boo oo : oo s){System.out.printf("Reading: %s%n", book);
}
Java EE trainin : htt ://courses.coreservlets.com
}
}49
Dependency Injection Example
private BookLibrary bookLibrary;
public BookReader( BookLibrary bookLibrary ) {this.bookLibrary = bookLibrary;
} Dependency injection interface
public List read() {List books = bookLibrary.search("Java");
Interface typeNOT the implementation type
or(Boo oo : oo s){System.out.printf("Reading: %s%n", book);
}
Java EE trainin : htt ://courses.coreservlets.com
}
}50
-
8/8/2019 01 Spring Foundations
25/31
Runtime Example public class Main {
Dependency creation moved out of BookReader
public static void main(String[] args) {
BookLibrary service = new JavaBookLibrary();
Dependencyinjection
BookReader client = new BookReader( service );
Listbooks = client.read();
System.out.printf("Client read: %s books%n",oo s.s ze());
}
}Standard output
Readin : Core Servlets and JavaServer Pa es
Java EE trainin : htt ://courses.coreservlets.com51
Reading: More Servlets and JavaServer PagesClient read: 2 books
Model Analysis Dynamic implementation choices
Object model can be reconfigured Future implementation types can be used without
Java EE trainin : htt ://courses.coreservlets.com52
-
8/8/2019 01 Spring Foundations
26/31
Model Analysis
Hard-coded model configuration Object model is not portable
Java EE trainin : htt ://courses.coreservlets.com53
2008 coreservlets.com
-Inversion of Control
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.
-
8/8/2019 01 Spring Foundations
27/31
Introduction
Inversion of Control Separate program control responsibilities
Object instantiation epen ency n ect on
Dependency injection is a type of IoC
Java EE trainin : htt ://courses.coreservlets.com55
Inversion of Control Example Previously used IoC
Dependency injection example demonstrated inversion of control
out of BookReader and into Main
Java EE trainin : htt ://courses.coreservlets.com56
-
8/8/2019 01 Spring Foundations
28/31
IoC Framework
Service provider or plugin framework Interface
Providers Access API
-- Joshua Bloch from Effective Java
Process Framework uses supplied APIs
ramewor an es crea on Framework handles dependency injection
Runtime context uses framework
Java EE trainin : htt ://courses.coreservlets.com57
IoC Framework Exampleimport coreservlets.BookReader;
public class ServiceProviderFramework {
private BookReader bookReader;
public ServiceProviderFramework(){
Implicit registration
this.bookReader =new BookReader(new JavaBookLibrary());
}Access API
public BookReader getBookReaderInstance() {return this.bookReader;
Java EE trainin : htt ://courses.coreservlets.com
}
58
-
8/8/2019 01 Spring Foundations
29/31
IoC Framework Example public class Main { Framework instantiation
ServiceProviderFramework framework =new ServiceProviderFramework();
Access API
BookReader client = framework.getBookReaderInstance() ;
List books = client.read();
System.out.printf("Client read: %s books%n",oo s.s ze());
}
}Standard output
Readin : Core Servlets and JavaServer Pa es
Java EE trainin : htt ://courses.coreservlets.com59
Reading: More Servlets and JavaServer PagesClient read: 2 books
Model Analysis Dynamic implementation choices Portable model configuration
Java EE trainin : htt ://courses.coreservlets.com60
-
8/8/2019 01 Spring Foundations
30/31
2008 coreservlets.com
rapup
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 Develop POJOs
Avo ramewor epen enc es 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 Place applicationContext.xml in the classpath
Register POJOs ec are s us ng ean e emen s Use bean attributes id and class for specifying the name and type,
respectively
Java EE trainin : htt ://courses.coreservlets.com
-
8/8/2019 01 Spring Foundations
31/31
Summary (Continued)
Instantiate a Spring IoC container Use t e BeanFactory mp ementat on
ClassPathXmlApplicationContext for integration withconfiguration files located in the classpath
org.spr ng ramewor .con ex .suppor .ClassPathXmlApplicationContext
Access the Spring IoC container Retrieve objects from the Spring IoC container using the bean
accessor methods For example, BeanFactory#getBean(...):Object
Specify the object name for the method parameter beanFactory.getBean("bookLibrary");
Java EE trainin : htt ://courses.coreservlets.com
2008 coreservlets.com
ues ons