1 Dependency Injection Sandro Pedrazzini Approfondimento Dependency Injection.
Spring Framework Dependency Injection Adavnced(Mod)
-
Upload
ankit-jain -
Category
Documents
-
view
242 -
download
0
Transcript of Spring Framework Dependency Injection Adavnced(Mod)
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
1/15
Spring Framework Dependency Injection AdvancedThe Dependency Injection (DI) feature is the core architectural foundation of the Springframework. This lab explores arious adanced dependency injection schemes of Springframework.
Expected duration: 140 minutes (excluding homework)
Resources
• Annoation-driven dependency inection in !pring "#1$"#% by !ark "isher (!ay
#$$%)
• &ustomi'ing annotation coniguration and component detection in !pring
"#1$"#% by !ark "isher (!ay #$$%)
• !pring "#%s comprehensive annoation support tutorial by &uergen 'oeller (&an.
#$$)
• !pring *ependency +nection , ava % tutorial by lef rendsen (!arch #$$)
Lab Exercises
• Exercise 0: +mport sample maven proects o this la.
• Exercise 1: /uild and run diadv&ontextAware sample maven proect ("0
minutes)
• Exercise ": /uild and run diadvprototype sample maven proect ("0
minutes)
• Exercise 2: /uild and run diadv3ropertyEditor sample maven proect ("0
minutes)
Exercise 0: +mport sample maven proects o this la.
*. +reate a new ,&aa -orking Set, called ,spring/di/ad, (or whateer name of your choice).
This is to organi0e the projects under the working set.
http://blog.springsource.com/2007/05/14/annotation-driven-dependency-injection-in-spring-21/http://blog.springsource.com/2007/05/29/customizing-annotation-configuration-and-component-detection-in-spring-21/http://blog.springsource.com/2007/05/29/customizing-annotation-configuration-and-component-detection-in-spring-21/http://blog.springsource.com/2008/01/28/spring-25s-comprehensive-annotation-support/http://blog.springsource.com/2008/03/18/spring-dependency-injection-java-5-including-slides-and-code/http://blog.springsource.com/2007/05/29/customizing-annotation-configuration-and-component-detection-in-spring-21/http://blog.springsource.com/2007/05/29/customizing-annotation-configuration-and-component-detection-in-spring-21/http://blog.springsource.com/2008/01/28/spring-25s-comprehensive-annotation-support/http://blog.springsource.com/2008/03/18/spring-dependency-injection-java-5-including-slides-and-code/http://blog.springsource.com/2007/05/14/annotation-driven-dependency-injection-in-spring-21/
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
2/15
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
3/15
#. Import maen projects of this lab.
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
4/15
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
5/15
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
6/15
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
7/15
Exercise 1: /uild and run diadv&ontextAware sample maven proect
1earning points2
• pplication+ontextware interface
o Interface to be implemented by any object that wishes to be notified of the
pplication+ontext that it runs in.
o Implementing this interface makes sense for example when an object
re3uires access to a set of collaborating beans. 4ote that configuration iabean references is preferable to implementing this interface just for bean
lookup purposes.
o This interface can also be implemented if an object needs access to fileresources5 i.e. wants to call get6esource5 wants to publish an applicationeent5 or re3uires access to the !essageSource. 'oweer5 it is preferable
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
8/15
to implement the more specific 6esource1oaderware5pplication7ent8ublisherware or !essageSourceware interface in sucha specific scenario.
o 4ote that file resource dependencies can also be exposed as bean
properties of type 6esource5 populated ia Strings with automatic type
conersion by the bean factory. This remoes the need for implementingany callback interface just for the purpose of accessing a specific fileresource.
Tasks to be performed
*. /uild and rundiadv&ontextAware sample maven proect
#. !tudy application
(!" #ui$d and run %di&adv&'ontextAware% samp$e maven project
*. 6un the application.
#. 9bsere the ressult.
Displaying pplication+ontext object...
org.springframework.context.support.+lass8ath:mlpplication+ontext;
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
9/15
public class !ain implements Application&ontextAware H
priate pplication+ontext ctx?
$$ A .ean which implements Application&ontextAware interace and is $$ deployed into the context will .e called .ack on creation o the
$$ .ean8 using the interaces setApplication&ontext() method8 and $$ provided with a reerence to the context8 which may .e stored $$ or later interaction with the context# pu.lic void setApplication&ontext(Application&ontext application&ontext) throws /eansException 9 ctx 5 application&ontext ;
public static oid main(String=> args) H pplication+ontext ctx B new +lass8ath:mlpplication+ontext( aware#xml)
FF +ontextwareDemo bean implements pplication+ontextware interface !ain demo B (!ain) ctx.getGean(,contextware,)? demo.displaypp+ontext()?
public oid displaypp+ontext() H
System.out.println(,Displaying pplication+ontext object...Jn,)? System.out.println(ctx)?
. Study the &aadoc of the pplication+ontext.
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
10/15
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
11/15
Exercise ": /uild and run diadvprototype sample maven proect
1earning points2
• -hen a bean is a singleton5 only one shared instance of the bean will be
managed5 and all re3uests for beans with an id or ids matching that beandefinition will result in that one specific bean instance being returned by the Springcontainer. Singleton is the default haiour in Spring. The nonsingleton5 prototypescope of bean deployment results in the creation of a new bean instance eerytime a re3uest for that specific bean is made.
Tasks to be performed
*. /uild and rundiadvprototype sample maven proect
#. !tudy application
()!" #ui$d and run %di&adv&prototype% samp$e maven project
4ote that the bean* and bean# are different instances of 1oggingGean.
Identity 73ualA2 falseddress of bean*1oggingGean;*
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
12/15
priate static final 1og log B 1og"actory.get1og(1oggingGean.class)?
priate String bean4ame B null?
public oid setGean4ame(String bean4ame) H this.bean4ame B bean4ame?
public oid some9peration() H if(log.isInfo7nabled()) H log.info(,Gean =, L bean4ame L ,> some9peration(),)?
. !ain.jaa
package com.softwarecampus.examples?
import org.springframework.beans.factory.Gean"actory?import org.springframework.beans.factory.xml.:mlGean"actory?import org.springframework.core.io.+lass8ath6esource?
public class !ain H
public static oid main(String=> args) H Gean"actory factory B new :mlGean"actory(new +lass8ath6esource( ,logging.xml,))?
1oggingGean bean* B (1oggingGean)factory.getGean(,loggingGean,)? 1oggingGean bean# B (1oggingGean)factory.getGean(,loggingGean,)?
System.out.println(,Identity 73ualA2 , L (bean* BB bean#))?
System.out.println(,ddress of bean*, L bean*)? System.out.println(,ddress of bean#, L bean#)?
Exercise 2: /uild and run diadv3ropertyEditor sample maven proect
1earning points
• 8roperty7ditor is a class that conerts a propertyMs alue to and from its natie
type representation into a String
• Spring comes with builtin 8roperty7ditor implementations that are preregistered
with the Gean"actory +lass7ditor5 "ile7ditor5 1ocale7ditor5 8roperties7ditor5C617ditor
Tasks to be performed
*. /uild and rundiadv3ropertyEditor sample maven proect
#. !tudy application
(*!" #ui$d and run %di&adv&+ropertyEditor% samp$e maven project
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
13/15
Setting class2 jaa.lang.String
Setting file2 test.txtSetting locale2 engb
Setting C612 http2FFwww.springframework.org
1oaded # properties1oaded < Stringsdding ** bytes
(*!" Study %di&adv&+ropertyEditor% samp$e maven project
*. builtin.xml
@Axml ersionB,*.$, encodingB,CT",AE@beans xmlnsB,http2FFwww.springframework.orgFschemaFbeans, xmlns2xsiB,http2FFwww.w.orgF#$$*F:!1Schemainstance, xsi2schema1ocationB, http2FFwww.springframework.orgFschemaFbeans
http2FFwww.springframework.orgFschemaFbeansFspringbeans.$.xsd,E
.ean id5.uilt+n!ampleclass5com#sotwarecampus#examples#6ain3ropertyEditor/ean7 property name5class7 value7ava#lang#!tring$value7 $property7 property name5ile7 value7c:$test#txt$value7 $property7
property name5locale7 value7 en-ello ?orld$value7 $property7 $.ean7@FbeansE
#. !ain/8roperty7ditorGean.jaa
package com.softwarecampus.examples?
import jaa.io."ile?import jaa.net.C61?
import jaa.util.1ocale?import jaa.util.8roperties?
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
14/15
import org.springframework.beans.factory.Gean"actory?import org.springframework.beans.factory.xml.:mlGean"actory?import org.springframework.core.io.+lass8ath6esource?
public class !ain/8roperty7ditorGean H
private &lass cls private @ile ile private BC url private Cocale locale private 3roperties properties private !tringD strings private .yteD .ytes
public static oid main(String=> args) H Gean"actory factory B new :mlGean"actory(new +lass8ath6esource( ,builtin.xml,))? !ain/8roperty7ditorGean bean B (!ain/8roperty7ditorGean) factory .getGean(,builtInSample,)?
public oid set+lass(+lass cls) H System.out.println(,Setting class2 , L cls.get4ame())?
this.cls B cls?
public oid set"ile("ile file) H
System.out.println(,Setting file2 , L file.get4ame())? this.file B file?
public oid set1ocale(1ocale locale) H
System.out.println(,Setting locale2 , L locale.getDisplay4ame())? this.locale B locale?
public oid set8roperties(8roperties properties) H
System.out.println(,1oaded , L properties.si0e() L , properties,)? this.properties B properties?
public oid setStrings(String=> strings) H System.out.println(,1oaded , L strings.length L , Strings,)?
this.strings B strings?
public oid setCrl(C61 url) H System.out.println(,Setting C612 , L url.to7xternal"orm())? this.url B url?
public oid setGytes(byte=> bytes) H System.out.println(,dding , L bytes.length L , bytes,)?
this.bytes B bytes?
-
8/17/2019 Spring Framework Dependency Injection Adavnced(Mod)
15/15