04 spring core3

Upload: prashant-shirsath

Post on 04-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 04 Spring Core3

    1/36

    2008 coreservlets.com

    Core Capabilities Part 3

    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, custom 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

  • 7/31/2019 04 Spring Core3

    2/36

    Topics in This Section

    Referencing other beans

    Plain values

    Properties integration

    Resource integration

    Collection mapping

    Java EE trainin : htt ://courses.coreservlets.com4

    2008 coreservlets.com

    e erenc ng er

    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.

  • 7/31/2019 04 Spring Core3

    3/36

    Bean Collaborator Reference

    CollaboratorPeer bean in a bean interaction

    Configuration e erence ot er eans w t n a common pr ng o

    container

    Define collaborators usin local eneral or inner beanreferences

    Java EE trainin : htt ://courses.coreservlets.com6

    Inner Bean Reference

    Fulfills a single dependency injectioninstance

    Analogous to anonymous classes

    Java EE trainin : htt ://courses.coreservlets.com7

  • 7/31/2019 04 Spring Core3

    4/36

    Inner Bean Process

    Nested for XML constructor or propertysetter DI

    XML element type bean y -

    Nested reference for XML collection element ,

    Do not specify identifiers or scope

    Java EE trainin : htt ://courses.coreservlets.com8

    Inner Bean Example

    -

    -

    Java EE trainin : htt ://courses.coreservlets.com

    9

  • 7/31/2019 04 Spring Core3

    5/36

    General Collaborator Reference

    Reference other beans in the Spring IoCcontainer

    Favor as the default method for specifying

    Java EE trainin : htt ://courses.coreservlets.com10

    General Collaborator Process

    Nested reference for XML constructor orproperty setter DIXML element type ref and attribute bean

    -

    Short-hand reference for XML constructor

    Declare as XML attribute type ref

    Nested reference for XML collection elementFor example, XML list child

    Java EE trainin : htt ://courses.coreservlets.com11

  • 7/31/2019 04 Spring Core3

    6/36

    General Collaborator Example

    " "=

    Java EE trainin : htt ://courses.coreservlets.com

    12

    General Collaborator

    Flexible approach Supports collaborator relationships across multiple contexts

    Compatible with parent/child containers or splitting andmer in contexts

    Practical for large multi-module projects

    Fragile configuration Bean references could identify collaborator definitionssupplied at runtime or errors

    o XML parser validation support

    Configuration error discovery can be delayed further if beansare lazily initialized

    Java EE trainin : htt ://courses.coreservlets.com

    Risks can be mitigated through a basic and automated

    integration test13

  • 7/31/2019 04 Spring Core3

    7/36

    Local Collaborator Reference

    Reference other beans in the Spring IoCcontainer

    Leverage XML parser validation m te to co a orator eans w t n t e same ean

    definitions document

    Java EE trainin : htt ://courses.coreservlets.com14

    Local Collaborator Process

    Nested reference for XML constructor orproperty setter DIXML element type ref and attribute local

    -

    No short-hand equivalent

    For example, XML list child

    Java EE trainin : htt ://courses.coreservlets.com15

  • 7/31/2019 04 Spring Core3

    8/36

    Local Collaborator Example

    < -

    Java EE trainin : htt ://courses.coreservlets.com16

    Local Collaborator

    Reduces flexibility Presents an additional configuration constraint

    Bean reference must identify a bean within the same document

    Mitigates the benefits of using multiple bean definition files Less practical for large multi-module projects

    Improves validation parser ver ca on suppor

    Bean references are verified in advance of constructing andinitializing beans

    Java EE trainin : htt ://courses.coreservlets.com17

  • 7/31/2019 04 Spring Core3

    9/36

    Bean Identifier Reference

    Reference a literal bean identifier value

    Inject into application context support

    beans or examp e, pr ng proxy actory eans nee ng to

    acquire the names of advisor beans

    Java EE trainin : htt ://courses.coreservlets.com18

    Bean Identifier Process

    Nested reference for XML constructor orproperty setter DI XML element type idref and attribute local orbean

    bean collaborator concepts local values must correlate to a bean in the same

    container

    Nested reference for XML collection elementFor example, XML list child

    Java EE trainin : htt ://courses.coreservlets.com19

  • 7/31/2019 04 Spring Core3

    10/36

    Bean Identifier Example

    20

    General Approach Review

    Define and create service interfaces

    Implement services interfaces

    Add the bean definitionsEstablish identifiers using the id attribute

    Aliases can be established using name attribute oralias

    elementDevelop bean names consistently using a convention

    Default to singleton beans verr e ean crea on an cac ng us ng scope a r u e

    Specify bean inter-dependencies using DI mechanisms Property setter, constructor, lookup-method, autowiring

    Java EE trainin : htt ://courses.coreservlets.com

    Reference collaborators by referencing general, inner orlocal beans

    21

  • 7/31/2019 04 Spring Core3

    11/36

    General Approach Review

    Access and use container-managed beansThe access and integration method is context-dependent

    Java EE trainin : htt ://courses.coreservlets.com22

    2008 coreservlets.com

    Plain Value

    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.

  • 7/31/2019 04 Spring Core3

    12/36

  • 7/31/2019 04 Spring Core3

    13/36

  • 7/31/2019 04 Spring Core3

    14/36

    Null Value Declaration Example

    Java EE trainin : htt ://courses.coreservlets.com

    < proper y>

    28

    Value Type Conversion

    Converts values from java.lang.String Uses JavaBean PropertyEditors

    Converts to implied or explicitly defined type

    explicit declarations

    Converts to multiple types Primitive and wrapper types

    Enum types

    java.io.File

    java.io.InputStream

    Java EE trainin : htt ://courses.coreservlets.com

    java.lang.Class

    java.util.Properties29

  • 7/31/2019 04 Spring Core3

    15/36

    Value Type Conversion Example

    public class SimpleValues {

    Red, Yellow, Blue;

    }

    public void setInteger(Integer integer) {

    }

    public void setColor(Color color) {

    }

    public void setFile(File file) {

    }

    pu c vo setNum er Num er e

    }

    public void setProperties(Properties properties) {

    Java EE trainin : htt ://courses.coreservlets.com

    public void setInputStream(InputStream inputStream) {

    }

    30

    Value Type Conversion Example

    < =" "

    100

    name00=value00

    name01=value01

    Java EE trainin : htt ://courses.coreservlets.com

    31

  • 7/31/2019 04 Spring Core3

    16/36

    Custom Type Conversion

    Implement java.beans.PropertyEditorExtend default implementation

    java.beans.PropertyEditorSupport

    setAsText(text:String):void

    implementationSee CustomEditorConfigurer from package

    org.springframework.beans.factory.config

    See PropertyEditorRegistrar from packageorg.springframework.beans

    Java EE trainin : htt ://courses.coreservlets.com32

    General Approach Review

    Define and create service interfaces

    Implement services interfaces

    Add the bean definitionsEstablish identifiers using the id attribute

    Aliases can be established using name attribute oralias

    elementDevelop bean names consistently using a convention

    Default to singleton beans verr e ean crea on an cac ng us ng scope a r u e

    Specify bean inter-dependencies using DI mechanisms Property setter, constructor, lookup-method, autowiring

    Java EE trainin : htt ://courses.coreservlets.com

    Reference collaborators by referencing inner beans orgeneral or local beans

    33

  • 7/31/2019 04 Spring Core3

    17/36

    General Approach Review

    Add the bean definitions continued Inject plain values using thevalue attribute or element

    Access and use container-managed beans e access an ntegrat on met o s context- epen ent

    Java EE trainin : htt ://courses.coreservlets.com34

    2008 coreservlets.com

    Properties Integration

    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.

  • 7/31/2019 04 Spring Core3

    18/36

    Properties Integration

    Injects Configuration properties directly into bean definitions

    Constructor DI Pro ert setter DI

    Collection element

    Replaces Configuration management libraries

    Calls from business logic to configuration management APIs

    Iterative exposure over application settings

    Clarifies

    Java EE trainin : htt ://courses.coreservlets.com

    Distinctions between administerable application settings anddeveloper settings

    36

    Properties Integration

    FeatureConfiguration mappings are supported by property editor

    support

    Limitation

    Each container onl su orts one ro erties declaration

    Java EE trainin : htt ://courses.coreservlets.com37

  • 7/31/2019 04 Spring Core3

    19/36

    Properties Integration Process

    spring-context XML namespace spr ng-con ex namespace n orma on

    property-placeholder XML element Add a property-placeholder declaration

    location attr ute Reference a resource path such as a classpath or filesystem location

    ${name} expression Reference property file properties with the property key nested within the

    expression format ${name}

    Configuration management For projects requiring multiple bean definition files, isolate the property-

    placeholder declaration in its own file

    Merge the configuration file into the container with peer bean definition

    Java EE trainin : htt ://courses.coreservlets.com

    i es

    38

    Properties Integration Examplename.01=value

    =

    name.03=value

    name.04=value

    Java EE trainin : htt ://courses.coreservlets.com39

  • 7/31/2019 04 Spring Core3

    20/36

  • 7/31/2019 04 Spring Core3

    21/36

    General Approach Review

    Add the bean definitions continued Inject plain values using thevalue attribute or element

    Reference values sourced from application configurationfiles usin the ex ression format ${name}

    Integrate properties files usingproperty-

    placeholder elements supplied by

    -

    Access and use container-managed beansThe access and inte ration method is context-de endent

    Java EE trainin : htt ://courses.coreservlets.com42

    2008 coreservlets.com

    Resource Integration

    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.

  • 7/31/2019 04 Spring Core3

    22/36

    Resource Integration

    Abstracts multiple resource typesFilesystem resource

    Classpath resource

    Context-specific resources For example, servlet context

    Replaces URL schemesConventional JVM URL scheme registration system is

    n ex e

    Java EE trainin : htt ://courses.coreservlets.com44

    Resource Integration Process

    Identify the path to the I/O sourceFor example, classpath resource path

    Rely on the default I/O context based on thepp ca on on ex

    For instance, ClassPathXmlApplicationContext

    ma s aths to resources located within the class ath Optionally, prefix the path with a custom

    scheme file:/path

    classpath:/path

    Java EE trainin : htt ://courses.coreservlets.com45

  • 7/31/2019 04 Spring Core3

    23/36

    Resource Integration Example

    < =" "

    value="classpath:/log4j.xml" />

    public class SimpleValues {

    public void setInputStream(InputStream inputStream) {

    }

    }

    Java EE trainin : htt ://courses.coreservlets.com46

    2008 coreservlets.com

    Collections

    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.

  • 7/31/2019 04 Spring Core3

    24/36

    Collections

    Enables list or set data structuredeclarations in bean definitions

    Supports data structure type conversion to

    Provides element type conversion to

    Java EE trainin : htt ://courses.coreservlets.com48

    Collections

    ConfigurationXML element type list or set

    Collection type conversion ava.u . o ec on

    java.util.List

    java.util.SetArray types

    Element type conversionGenerics

    Array type

    Java EE trainin : htt ://courses.coreservlets.com

    bean, ref, idref,value, null, list, set,map,props49

  • 7/31/2019 04 Spring Core3

    25/36

    Collections Process

    Identify the DI targetFor example, property setter or constructor

    Verify the parameter type ust e an array type, ava.ut l.Collect on, orjava.util.List, orjava.util.Set

    Declare the collection elements as XMLchild elements

    Java EE trainin : htt ://courses.coreservlets.com50

    Collection Examplepublic class CollectionValues {

    public void setVarargs(String... values) {

    }

    public void setArray(String[] values) {

    }

    public void setList(List values) {

    }

    pu c vo setSet Set va ues

    }

    Java EE trainin : htt ://courses.coreservlets.com

    }

    }

    51

  • 7/31/2019 04 Spring Core3

    26/36

    List Collection Example

    < =" "

    abc

    abc

    abc

    Java EE trainin : htt ://courses.coreservlets.com

    ...

    52

    Set Collection Example

    < =" "

    abc

    abc

    abc

    Java EE trainin : htt ://courses.coreservlets.com

    ...

    53

  • 7/31/2019 04 Spring Core3

    27/36

  • 7/31/2019 04 Spring Core3

    28/36

    Collection Type Conversion

    100

    /etc/hosts

    java.lang.String

    Java EE trainin : htt ://courses.coreservlets.com

    < s >

    56

    Dictionary Collections

    Enables dictionary data structuredeclarations in bean definitions

    Supports data structure type conversion to

    Provides element type conversion to

    Java EE trainin : htt ://courses.coreservlets.com57

  • 7/31/2019 04 Spring Core3

    29/36

    Map Collections

    Configuration

    Data structure

    XML element typemap

    XML element type entry and key elements

    Collection t e conversion java.util.Map

    Element type conversion

    Generics

    Array type

    Java EE trainin : htt ://courses.coreservlets.com

    c e emen ypes bean, ref, idref,value, null, list, set,map,props58

    Map Collections Process

    Identify the DI targetFor example, property setter or constructor

    Verify the parameter type ust e an array type, ava.ut l.Map

    Declare the XML elementmap

    element type entry

    Java EE trainin : htt ://courses.coreservlets.com59

  • 7/31/2019 04 Spring Core3

    30/36

    Map Collections Example

    < =" "

    01

    02

    < ey> < va ue>< ey>

    03

    Java EE trainin : htt ://courses.coreservlets.com

    60

    Map Collections Example

    Java EE trainin : htt ://courses.coreservlets.com61

  • 7/31/2019 04 Spring Core3

    31/36

    Properties Collections

    Enables simple dictionary data structuredeclarations in bean definitions to be

    Provides simplied interface for configuring

    No type conversion support

    Java EE trainin : htt ://courses.coreservlets.com62

    Properties Collections

    Configuration

    ata structure XML element typeprops

    Elements XML element typeprop and key attribute

    Property value is specified as theprop element content body

    Collection type conversion java.util.Map

    java.util.Properties

    Element t e conversion

    None

    XML child element types

    Java EE trainin : htt ://courses.coreservlets.com

    one

    63

  • 7/31/2019 04 Spring Core3

    32/36

    Properties Collections Process

    Identify the DI targetFor example, property setter or constructor

    Verify the parameter type ust e an array type, ava.ut l.Map orjava.util.Properties

    Declare the XML element ro s

    Declare the map elements using XML childelement typeprops

    Java EE trainin : htt ://courses.coreservlets.com64

    Properties Collections Example

    < =" "

    value

    Java EE trainin : htt ://courses.coreservlets.com65

  • 7/31/2019 04 Spring Core3

    33/36

    General Approach Review

    Define and create service interfaces

    Implement services interfaces

    Add the bean definitionsEstablish identifiers using the id attribute

    Aliases can be established using name attribute oraliaselement

    Develop bean names consistently using a convention

    Default to singleton beans verr e ean crea on an cac ng us ng scope a r u e

    Specify bean inter-dependencies using DI mechanisms Property setter, constructor, lookup-method, autowiring

    Java EE trainin : htt ://courses.coreservlets.com

    Reference collaborators by referencing inner beans orgeneral or local beans

    66

    General Approach Review

    Add the bean definitions continued Inject plain values using thevalue attribute or element

    Reference values sourced from application configurationfiles usin the ex ression format ${name}

    Integrate properties files usingproperty-

    placeholder elements supplied by

    -

    Inject collections by defining data structures directlywithin the bean definitions configuration

    List, set, map, or properties structures

    Access and use container-managed beans

    Java EE trainin : htt ://courses.coreservlets.com

    -

    67

  • 7/31/2019 04 Spring Core3

    34/36

    2008 coreservlets.com

    rap-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.

    Summary

    General collaborator referencesFlexible but fragile

    Process

    XML attribute ref

    Local collaborator references Improved validation but inflexible

    Process re oca

    XML attribute ref

    Java EE trainin : htt ://courses.coreservlets.com69

  • 7/31/2019 04 Spring Core3

    35/36

    Summary Continued

    Plain valuesXML element typevalue or attributevalue

    XML attribute type to override implied conversion

    XML elementproperty-placeholder declaration

    from the s rin -context names ace

    Requires the additional spring-context namespacedeclaration

    Java EE trainin : htt ://courses.coreservlets.com70

    Summary Continued

    Resource integrationFacilitated inline when specifying path information using

    scheme prefixes file, classpath, http

    behavior

    CollectionsXML parent element types list, set,map, andproperty

    Java EE trainin : htt ://courses.coreservlets.com71

  • 7/31/2019 04 Spring Core3

    36/36

    2008 coreservlets.com

    ues ons

    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.