the spring framework:the spring framework: core...

36
© 2008 coreservlets.com The Spring Framework: The Spring Framework: 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, see t htt // lt / courses at http://courses.coreservlets.com/. Taught by the experts that brought you this tutorial. Available at public venues or customized versions Available at public venues, or customized versions can be held on-site at your organization. C d l d dt ht b M t H ll 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. Courses developed and taught by Marty Hall 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

Upload: others

Post on 07-Jul-2020

46 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

© 2008 coreservlets.com

The Spring Framework:The Spring Framework: 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, see t htt // l t /courses at http://courses.coreservlets.com/.

Taught by the experts that brought you this tutorial. Available at public venues or customized versionsAvailable at public venues, or customized versions

can be held on-site at your organization.

C d l d d t ht b M t H ll

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.

• Courses developed and taught by Marty Hall– 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

Page 2: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Topics in This Sectionp

• Referencing other beans• Plain values• Properties integration• Resource integration• Collection mapping

Java EE training: http://courses.coreservlets.com4

© 2008 coreservlets.com

R f i OthReferencing Other BeansBeans

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.

Page 3: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Bean Collaborator Reference

• Collaborator– Peer bean in a bean interaction

• ConfigurationR f h b i hi S i I C– Reference other beans within a common Spring IoC container

– Define collaborators using local, general or inner bean g , greferences

Java EE training: http://courses.coreservlets.com6

Inner Bean Reference

• Fulfills a single dependency injection iinstance

• Analogous to anonymous classes

Java EE training: http://courses.coreservlets.com7

Page 4: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Inner Bean Process

• Nested for XML constructor or property DIsetter DI

– XML element type beanXML property or constructor arg child– XML property or constructor-arg child

• Nested reference for XML collection element– For example XML list childFor example, XML list child

• Do not specify identifiers or scope

Java EE training: http://courses.coreservlets.com8

Inner Bean Examplep

<bean id="bookReader" class="coreservlets.BookReader"><property name="bookLibrary">

<bean class="coreservlets.JavaBookLibrary"/></property>

</bean>

<bean id="bookReader" class="coreservlets.BookReader"><constructor-arg>

<bean class="coreservlets.JavaBookLibrary"/></constructor-arg ></constructor-arg >

</bean>

<bean ...><constructor arg><constructor-arg>

<list><bean class="coreservlets.JavaBookLibrary"/><bean class="coreservlets.JavaBookLibrary"/>

i

Java EE training: http://courses.coreservlets.com

<bean class="coreservlets.JavaBookLibrary"/></list>

</constructor-arg ></bean>9

Page 5: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

General Collaborator Reference

• Reference other beans in the Spring IoC icontainer

• Favor as the default method for specifying collaboratorscollaborators

Java EE training: http://courses.coreservlets.com10

General Collaborator Process

• Nested reference for XML constructor or DIproperty setter DI

– XML element type ref and attribute beanXML property or constructor arg child– XML property or constructor-arg child

• Short-hand reference for XML constructor DIDI– Declare as XML attribute type ref

• Nested reference for XML collection element– For example, XML list child

Java EE training: http://courses.coreservlets.com11

Page 6: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

General Collaborator Examplep

<bean id="bookReader" class="coreservlets.BookReader"><constructor-arg>

<ref bean="bookLibrary"/></constructor-arg >

</bean>

<bean id="bookReader" class="coreservlets.BookReader"><constructor-arg ref="bookLibrary"/>

</bean>

<bean ...><constructor-arg>

<list><ref bean "bookLibrary"/><ref bean="bookLibrary"/><ref bean="bookLibrary"/><ref bean="bookLibrary"/>

</list>

Java EE training: http://courses.coreservlets.com

</constructor-arg ></bean>

12

General Collaborator

• Flexible approach – Supports collaborator relationships across multiple contexts– Compatible with parent/child containers or splitting and

merging contextsg g– Practical for large multi-module projects

• Fragile configurationf ld id if ll b d fi i i– Bean references could identify collaborator definitions

supplied at runtime or errors– No XML parser validation supportp pp– Configuration error discovery can be delayed further if beans

are lazily initialized

• Avoidable risks

Java EE training: http://courses.coreservlets.com

• Avoidable risks– Risks can be mitigated through a basic and automated

integration test13

Page 7: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Local Collaborator Reference

• Reference other beans in the Spring IoC icontainer

• Leverage XML parser validationLi i d ll b b i hi h b– Limited to collaborator beans within the same bean definitions document

Java EE training: http://courses.coreservlets.com14

Local Collaborator Process

• Nested reference for XML constructor or DIproperty setter DI

– XML element type ref and attribute localXML property or constructor arg child– XML property or constructor-arg child

• No short-hand equivalent• Nested reference for XML collection element• Nested reference for XML collection element

– For example, XML list child

Java EE training: http://courses.coreservlets.com15

Page 8: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Local Collaborator Examplep

<bean id="bookReader" class="coreservlets.BookReader"><constructor-arg><constructor arg><ref local="bookLibrary"/>

</constructor-arg></bean>

<bean ...><constructor-arg><list><ref local="bookLibrary"/><ref local="bookLibrary"/>

f l l "b kLib "/<ref local="bookLibrary"/></list>

</constructor-arg></bean>

Java EE training: http://courses.coreservlets.com

</bean>

16

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 filesg g p• Less practical for large multi-module projects

• Improves validationXML parser verification support– XML parser verification support

– Bean references are verified in advance of constructing and initializing beans

Java EE training: http://courses.coreservlets.com17

Page 9: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Bean Identifier Reference

• Reference a literal bean identifier value• Inject into application context support

beansF l S i AOP f b di– For example, Spring AOP proxy factory beans needing to acquire the names of advisor beans

• Leverage validation supportLeverage validation support

Java EE training: http://courses.coreservlets.com18

Bean Identifier Process

• Nested reference for XML constructor or DIproperty setter DI

– XML element type idref and attribute local or bean– Local versus bean concept consistent with local versus general– Local versus bean concept consistent with local versus general

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

document and bean to a collaborator in the scope of thedocument and bean to a collaborator in the scope of the container

• Nested reference for XML collection element– For example, XML list child

Java EE training: http://courses.coreservlets.com19

Page 10: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Bean Identifier Examplep

<bean id="bookLibrary-00"class="coreservlets.JavaBookLibrary" />class coreservlets.JavaBookLibrary />

<bean id="bookLibrary-01"class="coreservlets.JavaBookLibrary" />

<bean id="bookLibrary-02"class="coreservlets.JavaBookLibrary" />

<bean id="beanNames" class="coreservlets BeanNames">class= coreservlets.BeanNames >

<constructor-arg index="0"><idref local="bookLibrary-00" />

</constructor-arg><constructor-arg index="1"><list><idref bean="bookLibrary-01" />id f b "b kLib 02" /

Java EE training: http://courses.coreservlets.com

<idref bean="bookLibrary-02" /></list>

</constructor-arg></bean>20

General Approach Reviewpp

• Define and create service interfaces• Implement services interfaces• Add the bean definitions

– Establish identifiers using the id attribute• Aliases can be established using name attribute or alias

element

– Develop bean names consistently using a convention– Default to singleton beans

O erride bean creation and caching sing attrib te• Override bean creation and caching using scope attribute

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

Java EE training: http://courses.coreservlets.com

p y p g• Reference collaborators by referencing general, inner or

local beans21

Page 11: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

General Approach Review ContinuedContinued

• Access and use container-managed beans– The access and integration method is context-dependent

Java EE training: http://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.

Page 12: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Plain Value

• Idea– Insert simple values into beans without declaring separate

beans for these values– This feature actually makes Spring unique from other IoC– This feature actually makes Spring unique from other IoC

containers

• Conversion– Values are automatically converted from String via

property editorsThe conversion type is implied by the DI target– The conversion type is implied by the DI target

– The conversion type can be explicitly defined

Java EE training: http://courses.coreservlets.com24

Value Declaration Process

• Create and inject plain values into multiple contexts– Constructor DI or property setter DI

• Nested XML element type value• Nested XML element type value• Short-hand value attribute• Nested XML element type null

C ll i l– Collection element• Nested XML element type value• Nested XML element type null

Java EE training: http://courses.coreservlets.com25

Page 13: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Value Declaration Examplep

<bean id="values-00" class="coreservlets.Values"><constructor-arg value="values-00" /><constructor arg value values 00 />

</bean>

<bean id="values-01" class="coreservlets.Values"><constructor-arg><value>values-01</value>

</constructor-arg></bean></bean>

Java EE training: http://courses.coreservlets.com26

Value Declaration Example ContinuedContinued<bean id="values-02" class="coreservlets.Values"><constructor-arg><constructor arg><list><value>values-02</value>

</list></constructor-arg>

</bean>

<bean id="values-03" class="coreservlets Values"><bean id= values-03 class= coreservlets.Values ><property name="value"><value>values-03</value>

</property></bean>

Java EE training: http://courses.coreservlets.com27

Page 14: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Null Value Declaration Examplep

<bean id="values-01" class="coreservlets.Values"><constructor-arg><constructor arg><null/>

</constructor-arg></bean><bean id="values-02" class="coreservlets.Values"><constructor-arg><list><null/><null/>

</list></constructor-arg>

</bean><bean id="values-03" class="coreservlets.Values"><property name="value"><null/>/ t

Java EE training: http://courses.coreservlets.com

</property></bean>

28

Value Type Conversionyp

• Converts values from java.lang.String– Uses JavaBean PropertyEditors

• Converts to implied or explicitly defined type– Use type attribute with a fully qualified class name for– Use type attribute with a fully qualified class name for

explicit declarations

• Converts to multiple types– Primitive and wrapper types– Enum types– List and dictionary collectionsList and dictionary collections– java.io.File– java.io.InputStream

• URL format or custom Spring resource schemes

Java EE training: http://courses.coreservlets.com

URL format or custom Spring resource schemes– java.lang.Class– java.util.Properties

29

Page 15: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Value Type Conversion Exampleyp p

public class SimpleValues {public enum Color{public enum Color{Red, Yellow, Blue;

}

public void setInteger(Integer integer) {}public void setColor(Color color) {}public void setFile(File file) {}bli id tN b (N b fil ) {public void setNumber(Number file) {

}public void setProperties(Properties properties) {}

Java EE training: http://courses.coreservlets.com

}public void setInputStream(InputStream inputStream) {}

}30

Value Type Conversion Exampleyp p

<beans><bean class="coreservlets.SimpleValues"><bean class= coreservlets.SimpleValues ><property name="integer" value="100" /><property name="color" value="Red" /><property name="file" value="/etc/hosts" /><property name="inputStream"

value="classpath:/log4j.xml" /><property name="number"><value type="java.lang.Double">100</value>

</property><property name="properties">

l<value>name00=value00name01=value01

</value>

Java EE training: http://courses.coreservlets.com

</value></property>

</bean></beans>31

Page 16: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Custom Type Conversionyp

• Implement java.beans.PropertyEditor– Extend default implementation

java.beans.PropertyEditorSupport

Override method– Override methodsetAsText(text:String):void

• Register new PropertyEditorRegister new PropertyEditorimplementation– See CustomEditorConfigurer from package org.springframework.beans.factory.config

– See PropertyEditorRegistrar from package org.springframework.beans

Java EE training: http://courses.coreservlets.com32

General Approach Reviewpp

• Define and create service interfaces• Implement services interfaces• Add the bean definitions

– Establish identifiers using the id attribute• Aliases can be established using name attribute or alias

element

– Develop bean names consistently using a convention– Default to singleton beans

O erride bean creation and caching sing attrib te• Override bean creation and caching using scope attribute

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

Java EE training: http://courses.coreservlets.com

p y p g• Reference collaborators by referencing inner beans or

general or local beans33

Page 17: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

General Approach Review ContinuedContinued

• Add the bean definitions continued– Inject plain values using the value attribute or element

• Access and use container-managed beansTh d i i h d i d d– The access and integration method is context-dependent

Java EE training: http://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.

Page 18: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Properties Integrationp g

• Injects – Configuration properties directly into bean definitions

• Constructor DI• Property setter DIp y• Collection element

• Replaces– Configuration management libraries – Calls from business logic to configuration management APIs

• Supports• Supports– Iterative exposure over application settings

• Clarifies

Java EE training: http://courses.coreservlets.com

– Distinctions between administerable application settings and developer settings

36

Properties Integrationp g

• Feature– Configuration mappings are supported by property editor

support– Configuration is visible from anywhere in the container– Configuration is visible from anywhere in the container

• Limitation– Each container only supports one properties declarationy pp p p

Java EE training: http://courses.coreservlets.com37

Page 19: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Properties Integration Processp g

• spring-context XML namespaceAdd s i c t t XML namespace information– Add spring-context XML namespace information

• property-placeholder XML element– Add a property-placeholder declaration

ib• location attribute– 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 managementg g– 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

fil

Java EE training: http://courses.coreservlets.com

files

38

Properties Integration Examplep g p

name.01=valuename.02=valuename.02=valuename.03=valuename.04=value

Java EE training: http://courses.coreservlets.com39

Page 20: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Properties Integration Examplep g p

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"p gxmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:property-placeholder location="classpath:/app.properties" />

<bean id="settings" class="coreservlets.Settings"><property name="value" value="${name 01}" /><property name= value value= ${name.01} />

</bean></beans>

Java EE training: http://courses.coreservlets.com40

General Approach Reviewpp

• Define and create service interfaces• Implement services interfaces• Add the bean definitions

– Establish identifiers using the id attribute• Aliases can be established using name attribute or alias

element

– Develop bean names consistently using a convention– Default to singleton beans

O erride bean creation and caching sing attrib te• Override bean creation and caching using scope attribute

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

Java EE training: http://courses.coreservlets.com

p y p g• Reference collaborators by referencing inner beans or

general or local beans41

Page 21: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

General Approach Review ContinuedContinued

• Add the bean definitions continued– Inject plain values using the value attribute or element

• Reference values sourced from application configuration files using the expression format ${name}g p ${ }

• Integrate properties files using property-placeholder elements supplied by spring-contextspring context

• Access and use container-managed beans– The access and integration method is context-dependentg p

Java EE training: http://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.

Page 22: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Resource Integrationg

• Abstracts multiple resource types– Filesystem resource– Classpath resource

HTTP network resource– HTTP network resource– Context-specific resources

• For example, servlet context

• Replaces URL schemes– Conventional JVM URL scheme registration system is

i fl iblinflexible

Java EE training: http://courses.coreservlets.com44

Resource Integration Processg

• Identify the path to the I/O source– For example, classpath resource path

• Rely on the default I/O context based on the A li ti C t t implementationApplicationContext implementation– For instance, ClassPathXmlApplicationContext

maps paths to resources located within the classpathp p p

• Optionally, prefix the path with a custom scheme– file:/path– classpath:/path– http://path

Java EE training: http://courses.coreservlets.com

http://path

45

Page 23: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Resource Integration Exampleg p

<bean class="coreservlets.SimpleValues"><property name="inputStream"<property name= inputStream

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

public class SimpleValues {public void setInputStream(InputStream inputStream) {}

}

Java EE training: http://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.

Page 24: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Collections

• Enables list or set data structure d l i i b d fi i ideclarations in bean definitions

• Supports data structure type conversion to target collection typetarget collection type

• Provides element type conversion to generic and array typegeneric and array type

Java EE training: http://courses.coreservlets.com48

Collections

• Configuration – XML element type list or set

• Collection type conversionj til C ll ti– java.util.Collection

– java.util.List– java.util.Set

– Array types

• Element type conversion– Generics– Array type

XML child element types

Java EE training: http://courses.coreservlets.com

• XML child element types– bean, ref, idref, value, null, list, set, map, props49

Page 25: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Collections Process

• Identify the DI target– For example, property setter or constructor

• Verify the parameter typeM t b t j il ll i– Must be an array type, java.util.Collection, or java.util.List, or java.util.Set

• Declare the XML element list or setDeclare the XML element list or set• Declare the collection elements as XML

child elements

Java EE training: http://courses.coreservlets.com50

Collection Examplep

public class CollectionValues {

public void setVarargs(String... values) {}

public void setArray(String[] values) {}

public void setList(List<String> values) {}

bli id tS t(S t St i l ) {public void setSet(Set<String> values) {}

public void setCollection(Collection<String> values) {

Java EE training: http://courses.coreservlets.com

public void setCollection(Collection<String> values) {}

}51

Page 26: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

List Collection Examplep

<bean class="coreservlets.CollectionValues"><property name="varargsValues"><property name= varargsValues ><list><value>abc</value>

</list></property><property name="arrayValues"><list><value>abc</value>

</list></property>

t "li tV l "<property name="listValues"><list><value>abc</value>

</list>

Java EE training: http://courses.coreservlets.com

</list></property>...

</bean> 52

Set Collection Examplep

<bean class="coreservlets.CollectionValues"><property name="varargsValues"><property name= varargsValues ><set><value>abc</value>

</set></property><property name="arrayValues"><set><value>abc</value>

</set></property>

t "li tV l "<property name="listValues"><set><value>abc</value>

</set>

Java EE training: http://courses.coreservlets.com

</set></property>...

</bean> 53

Page 27: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Collection Type Conversionyp

• Inspects the target type– Collection generic type– Array type

Converts the element type before• Converts the element type before assignment to the data structure– Conversion based on value PropertyEditor supportConversion based on value PropertyEditor support

Java EE training: http://courses.coreservlets.com54

Collection Type Conversion ExampleExample

public class TypedListValues{

public void setVarargsIntegers(Integer... values) {}

public void setArrayFiles(File[] values) {}

public void setListClasses(List<Class> values) {}

}}

Java EE training: http://courses.coreservlets.com55

Page 28: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Collection Type Conversion ExampleExample

<beans><bean class="coreservlets.TypedListValues"><bean class coreservlets.TypedListValues ><property name="varargsIntegers"><list><value>100</value>

</list></property><property name="arrayFiles"><list><list><value>/etc/hosts</value>

</list></property><property name="listClasses"><list><value>java.lang.String</value>/li t

Java EE training: http://courses.coreservlets.com

</list></property>

</bean></beans>56

Dictionary Collectionsy

• Enables dictionary data structure d l i i b d fi i ideclarations in bean definitions

• Supports data structure type conversion to target collection typestarget collection types

• Provides element type conversion to generic and array typesgeneric and array types

Java EE training: http://courses.coreservlets.com57

Page 29: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Map Collectionsp

• Configuration – Data structure

• XML element type map

Elements– Elements• XML element type entry and key elements

• Collection type conversionyp– java.util.Map

• Element type conversion– Generics– Array type

XML hild l t t

Java EE training: http://courses.coreservlets.com

• XML child element types– bean, ref, idref, value, null, list, set, map, props58

Map Collections Processp

• Identify the DI target– For example, property setter or constructor

• Verify the parameter typeM t b t j il– Must be an array type, java.util.Map

• Declare the XML element map• Declare the map elements using XML child• Declare the map elements using XML child

element type entry

Java EE training: http://courses.coreservlets.com59

Page 30: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Map Collections Examplep p

<bean class="coreservlets.MappedValues"><property name="map"><property name= map ><map><entry key="00" value="00" /><entry key="01"><value>01</value>

</entry><entry value-ref="02"><key><value>02</value></key>

</entry><entry>

k l 03 / l /k<key><value>03</value></key><value>03</value>

</entry></map>

Java EE training: http://courses.coreservlets.com

</map></property>

</bean>60

Map Collections Examplep p

<bean name="00" class="java.lang.Integer"factory-method="valueOf">factory method= valueOf >

<constructor-arg value="00" /></bean>

<bean class="coreservlets.MappedValues"><property name="map"><map><entry key-ref="00" value-ref="00" />

</map></property>/b</bean>

Java EE training: http://courses.coreservlets.com61

Page 31: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Properties Collectionsp

• Enables simple dictionary data structure d l i i b d fi i i bdeclarations in bean definitions to be

• Provides simplied interface for configuring property setsproperty sets– No type conversion support

Java EE training: http://courses.coreservlets.com62

Properties Collectionsp

• Configuration D t t t– Data structure

• XML element type props

– Elements• XML element type prop and key attribute • Property value is specified as the prop element content body

• Collection type conversionyp– java.util.Map– java.util.Properties

• Element type conversionyp– None

• XML child element typesN

Java EE training: http://courses.coreservlets.com

– None

63

Page 32: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Properties Collections Processp

• Identify the DI target– For example, property setter or constructor

• Verify the parameter typeM t b t j il– Must be an array type, java.util.Map orjava.util.Properties

• Declare the XML element propsDeclare the XML element props• Declare the map elements using XML child

element type props

Java EE training: http://courses.coreservlets.com64

Properties Collections Examplep p

<bean class="coreservlets.PropertyValue"><property name="properties"><property name= properties ><props><prop key="name">value</prop>

</props></property>

</bean>

Java EE training: http://courses.coreservlets.com65

Page 33: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

General Approach Reviewpp

• Define and create service interfaces• Implement services interfaces• Add the bean definitions

– Establish identifiers using the id attribute• Aliases can be established using name attribute or alias

element

– Develop bean names consistently using a convention– Default to singleton beans

O erride bean creation and caching sing attrib te• Override bean creation and caching using scope attribute

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

Java EE training: http://courses.coreservlets.com

p y p g• Reference collaborators by referencing inner beans or

general or local beans66

General Approach Review ContinuedContinued

• Add the bean definitions continued– Inject plain values using the value attribute or element

• Reference values sourced from application configuration files using the expression format ${name}g p ${ }

• Integrate properties files using property-placeholder elements supplied by spring-contextspring context

– Inject collections by defining data structures directly within the bean definitions configuration

• List, set, map, or properties structures

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

Java EE training: http://courses.coreservlets.com

– The access and integration method is context-dependent

67

Page 34: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

© 2008 coreservlets.com

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

Summaryy

• General collaborator references– Flexible but fragile– Process

• XML element type ref with the bean attribute• XML element type ref with the bean attribute• XML attribute ref

• Local collaborator references– Improved validation but inflexible– Process

XML element type f with the l l attribute• XML element type ref with the local attribute• XML attribute ref

Java EE training: http://courses.coreservlets.com69

Page 35: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

Summary Continuedy

• Plain values– XML element type value or attribute value– XML attribute type to override implied conversion

Properties integration• Properties integration– XML element property-placeholder declaration

from the spring-context namespacep g p• Requires the additional spring-context namespace

declaration

XML attribute location specifying the I/O source– XML attribute location specifying the I/O source

Java EE training: http://courses.coreservlets.com70

Summary Continuedy

• Resource integration– Facilitated inline when specifying path information using

scheme prefixes file, classpath, http– No prefix results in ApplicationContext dependent– No prefix results in ApplicationContext dependent

behavior

• Collections– XML parent element types list, set, map, and property

Java EE training: http://courses.coreservlets.com71

Page 36: The Spring Framework:The Spring Framework: Core ...courses.coreservlets.com/.../04-spring-core3.pdf · Bean Collaborator Reference • Collaborator – Peer bean in a bean interaction

© 2008 coreservlets.com

Q ti ?Questions?

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.