SlideShare a Scribd company logo
1 of 41
Collections for Bean Properties Java Collections Setting Collection for Bean Properties Defining List,Set,Map,Properties Setting Data Type for Collections Defining Concrete Collection Classes using Factory Bean Defining Concrete Collection Classes using Schema Defining Stand Alone Collections
The Java Collections framework defines a set of interfaces, implementations, and algorithms for different types of collections, such as lists, sets, and maps List, Set, and Map are the core interfaces representing three main types of collections. For each collection type, Java provides several implementations with different functions and characteristics from which you can choose. In Spring, these types of collections can be easily configured with a group of built-in XML tags, such as <list>, <set>, and <map>
 
Defining Collections for Bean Properties ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
First, let’s use a java.util.List collection to contain your suffixes. A  list  is an ordered and indexed collection whose elements can be accessed either by index or with a for-each loop. public class SequenceGenerator { ... private List<Object> suffixes; public void setSuffixes(List<Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... for (Object suffix : suffixes) { buffer.append(&quot;-&quot;); buffer.append(suffix); } return buffer.toString(); } } Using List example Bean
To define a property of java.util.List type in the bean configuration, you specify a <list> tag that contains the elements. The elements allowed inside the <list> tag can be a simple constant value specified by <value>, a bean reference by <ref>, an inner bean definition by <bean>, or a null element by <null>. You can even embed other collections in a collection. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <list> <value>A</value> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.javafasttrack.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> <null /> </list> </property> </bean> Using List example Configuration
public class SequenceGenerator { ... private  Set<Object>  suffixes; public void setSuffixes( Set<Object>  suffixes) { this.suffixes = suffixes; } ... } To define a property of java.util.Set type, use the <set> tag to define the elements in the same way as a list. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <set> <value>A</value> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.javafasttrack.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> <null /> </set> </property> </bean> Using Set example Bean Using Set example Configuration
Using Maps example Bean public class SequenceGenerator { ... private Map<Object, Object> suffixes; public void setSuffixes(Map<Object, Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... for (Map.Entry entry : suffixes.entrySet()) { buffer.append(&quot;-&quot;); buffer.append(entry.getKey()); buffer.append(&quot;@&quot;); buffer.append(entry.getValue()); } return buffer.toString(); } }
<bean id=&quot;sequenceGenerator“  Using Maps example configuration class=&quot;SequenceGenerator&quot;> <property name=&quot;suffixes&quot;> <map> <entry> <key> <value>type</value> </key> <value>A</value> </entry> <entry> <key> <value>url</value> </key> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.apress.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> </entry> </map> </property> </bean>
There are  shortcuts to defining map keys and values  as attributes of the <entry> tag. If they are simple constant values, you can define them by key and value. If they are bean references, you can define them by key-ref and value-ref. <bean id=&quot;sequenceGenerator&quot; class=&quot;com.apress.springrecipes.sequence.SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <map> <entry key=&quot;type&quot; value=&quot;A&quot; /> <entry key=&quot;url&quot;> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.apress.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> </entry> </map> </property> </bean>
Using java.util.Properties A java.util.Properties collection is very similar to a map. It also implements the java.util.Map interface and stores entries in key/value pairs. The only difference is that the keys and values of a Properties collection are always strings....
public class SequenceGenerator { ... private Properties suffixes;  java.util.Properties Bean example public void setSuffixes(Properties suffixes) { this.suffixes = suffixes; } ... } To define a java.util.Properties collection in Spring, use the <props> tag with multiple <prop> tags as children. Each <prop> tag must have a key attribute defined and the corresponding value enclosed. <bean id=&quot;sequenceGenerator“ class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <props> <prop key=&quot;type&quot;>A</prop> <prop key=&quot;url&quot;>http://www.jftcom/</prop> </props></property> </bean>
Merging the Collection of the Parent Bean If you define your beans with inheritance, a child bean’s collection can be merged with that of its parent by setting the merge attribute to true. For a <list> collection, the child elements will be appended after the parent’s to preserve the order. So, the following sequence generator will have four suffixes: A, B, A, and C.
Merging the Collection of the Parent Bean Example <beans ...>  <bean id=&quot;baseSequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <list> <value>A</value> <value>B</value> </list> </property> </bean> <bean id=&quot;sequenceGenerator&quot; parent=&quot;baseSequenceGenerator&quot;> <property name=&quot;suffixes&quot;> <list  merge=&quot;true&quot; > <value>A</value> <value>C</value> </list> </property>
Merging the Collection of the Parent Bean Example (Contd ..) </bean> ... </beans> For a <set> or <map> collection, the child elements will overwrite the parent’s if they have the same value. So, the following sequence generator will have three suffixes: A, B, and C. <beans ...> <bean id=&quot;baseSequenceGenerator&quot; class=&quot;com.apress.springrecipes.sequence.SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <set> <value>A</value> <value>B</value> </set> </property> </bean>
<bean id=&quot;sequenceGenerator&quot; parent=&quot;baseSequenceGenerator&quot;> <property name=&quot;suffixes&quot;> <set merge=&quot;true&quot;> <value>A</value> <value>C</value> </set> </property> </bean> ... </beans>
Specifying the Data Type for Collection Elements ,[object Object],[object Object],[object Object],[object Object],[object Object]
Now suppose you are going to accept a list of integer numbers as the suffixes of your sequence generator. Each number will be formatted into four digits by an instance of java.text. DecimalFormat. public class SequenceGenerator { ... private List<Object> suffixes; public void setSuffixes(List<Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... DecimalFormat formatter = new DecimalFormat(&quot;0000&quot;); for (Object suffix : suffixes) { buffer.append(&quot;-&quot;); buffer.append(formatter.format((Integer) suffix)); } return buffer.toString(); } }
<bean id=&quot;sequenceGenerator&quot; class=&quot;com.apress.springrecipes.sequence.SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <list> <value>5</value>However, when you run this application, you will encounter a ClassCastException, indicating that the suffixes cannot be cast into integers because their type is String. Spring treats every element in a collection as a string by default <value>10</value> <value>20</value> </list> </property> </bean>
<bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <list> <value type=&quot;int&quot;>5</value> <value type=&quot;int&quot;>10</value> <value type=&quot;int&quot;>20</value> </list> </property> </bean> Or you may set the value-type attribute of the collection tag to specify the type for all elements in this collection. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <list value-type=&quot;int&quot;> <value>5</value> <value>10</value> <value>20</value> </list> </property> </bean>
In Java 1.5 or higher, you can define your suffixes list with a type-safe collection that stores integers. ... public class SequenceGenerator { ... private List<Integer> suffixes; public void setSuffixes(List<Integer> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... DecimalFormat formatter = new DecimalFormat(&quot;0000&quot;); for (int suffix : suffixes) { buffer.append(&quot;-&quot;); buffer.append(formatter.format(suffix)); }
return buffer.toString(); } } Once you have defined your collections in a type-safe way, Spring will be able to read the collection’s type information through reflection. In this way, you no longer need to specify the value-type attribute of <list>. <bean id=&quot;sequenceGenerator“ class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <list> <value>5</value> <value>10</value> <value>20</value> </list> </property> </bean>
class CarFactory{ Car static newInstance(){ return new Maruthi(); } } interface Car{ } Maruthi implements Car{ }
Defining Collections Using Factory Beans and the Utility Schema ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
<bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <bean class=&quot;org.springframework.beans.factory.config.SetFactoryBean&quot;> <property name=&quot; targetSetClass &quot;> <value> java.util.TreeSet </value> </property> <property name=&quot;sourceSet&quot;> <set> <value>5</value> <value>10</value> <value>20</value> </set> </property> </bean> </property> </bean>
Or you can use a collection tag in the util schema to define a collection and set its target class (e.g., by the set-class attribute of <util:set>). But you must remember to add the util schema definition to your <beans> root element. <beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:util=&quot;http://www.springframework.org/schema/util&quot; xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd&quot;> <bean id=&quot;sequenceGenerator“ class=“SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <util:set set-class=&quot;java.util.TreeSet&quot;> <value>5</value> <value>10</value> <value>20</value> </util:set> </property> </bean> ... </beans>
Defining Stand-Alone Collections ,[object Object],[object Object],[object Object]
<beans ...> <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <ref local=&quot;suffixes&quot; /> </property> </bean> <bean id=&quot;suffixes&quot; class=&quot;org.springframework.beans.factory.config.SetFactoryBean&quot;> <property name=&quot;sourceSet&quot;> <set> <value>5</value> <value>10</value> <value>20</value> </set> </property> </bean> ... </beans>
Or you can define a stand-alone set by using the <util:set> tag in the util schema. <beans ...> <bean id=&quot;sequenceGenerator“ class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <ref local=&quot;suffixes&quot; /> </property> </bean> <util:set id=&quot;suffixes&quot;> <value>5</value> <value>10</value> <value>20</value> </util:set> ... </beans>
Scanning Components from the Classpath ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
class CarFactory{ Car static newInstance(){ return new Maruthi(); } } interface Car{ } Maruthi implements Car{ }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],For example, you can write the following createProduct() static factory method to create a product from a predefined product ID. According to the product ID, this method will decide which concrete product class to instantiate. If there is no product matching this ID, it will throw an IllegalArgumentException
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

Similar to Sping Slide 6

Similar to Sping Slide 6 (20)

displaytag
displaytagdisplaytag
displaytag
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To Hibernate
 
KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7
 
KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7
 
2. wiring beans
2. wiring beans2. wiring beans
2. wiring beans
 
Sencha Touch Intro
Sencha Touch IntroSencha Touch Intro
Sencha Touch Intro
 
Struts Tags Speakernoted
Struts Tags SpeakernotedStruts Tags Speakernoted
Struts Tags Speakernoted
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Spring 2.0
Spring 2.0Spring 2.0
Spring 2.0
 
Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"
 
Spring 2.0
Spring 2.0Spring 2.0
Spring 2.0
 
JavaEE Spring Seam
JavaEE Spring SeamJavaEE Spring Seam
JavaEE Spring Seam
 
Learning jsp
Learning jspLearning jsp
Learning jsp
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
JSP Custom Tags
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
 
Complex Data Binding
Complex Data BindingComplex Data Binding
Complex Data Binding
 
Os Leonard
Os LeonardOs Leonard
Os Leonard
 
ASP.NET 10 - Data Controls
ASP.NET 10 - Data ControlsASP.NET 10 - Data Controls
ASP.NET 10 - Data Controls
 

More from patinijava

More from patinijava (18)

Web Services Part 2
Web Services Part 2Web Services Part 2
Web Services Part 2
 
Web Services Part 1
Web Services Part 1Web Services Part 1
Web Services Part 1
 
Struts N E W
Struts N E WStruts N E W
Struts N E W
 
Session Management
Session  ManagementSession  Management
Session Management
 
S E R V L E T S
S E R V L E T SS E R V L E T S
S E R V L E T S
 
Struts Java I I Lecture 8
Struts  Java  I I  Lecture 8Struts  Java  I I  Lecture 8
Struts Java I I Lecture 8
 
Servlet Api
Servlet ApiServlet Api
Servlet Api
 
Servlet11
Servlet11Servlet11
Servlet11
 
Entity Manager
Entity ManagerEntity Manager
Entity Manager
 
Ejb6
Ejb6Ejb6
Ejb6
 
Ejb5
Ejb5Ejb5
Ejb5
 
Ejb4
Ejb4Ejb4
Ejb4
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
 
Webbasics
WebbasicsWebbasics
Webbasics
 
Internetbasics
InternetbasicsInternetbasics
Internetbasics
 
Jsp
JspJsp
Jsp
 
Portlet
PortletPortlet
Portlet
 

Recently uploaded

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Recently uploaded (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Sping Slide 6

  • 1. Collections for Bean Properties Java Collections Setting Collection for Bean Properties Defining List,Set,Map,Properties Setting Data Type for Collections Defining Concrete Collection Classes using Factory Bean Defining Concrete Collection Classes using Schema Defining Stand Alone Collections
  • 2. The Java Collections framework defines a set of interfaces, implementations, and algorithms for different types of collections, such as lists, sets, and maps List, Set, and Map are the core interfaces representing three main types of collections. For each collection type, Java provides several implementations with different functions and characteristics from which you can choose. In Spring, these types of collections can be easily configured with a group of built-in XML tags, such as <list>, <set>, and <map>
  • 3.  
  • 4.
  • 5. First, let’s use a java.util.List collection to contain your suffixes. A list is an ordered and indexed collection whose elements can be accessed either by index or with a for-each loop. public class SequenceGenerator { ... private List<Object> suffixes; public void setSuffixes(List<Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... for (Object suffix : suffixes) { buffer.append(&quot;-&quot;); buffer.append(suffix); } return buffer.toString(); } } Using List example Bean
  • 6. To define a property of java.util.List type in the bean configuration, you specify a <list> tag that contains the elements. The elements allowed inside the <list> tag can be a simple constant value specified by <value>, a bean reference by <ref>, an inner bean definition by <bean>, or a null element by <null>. You can even embed other collections in a collection. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <list> <value>A</value> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.javafasttrack.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> <null /> </list> </property> </bean> Using List example Configuration
  • 7. public class SequenceGenerator { ... private Set<Object> suffixes; public void setSuffixes( Set<Object> suffixes) { this.suffixes = suffixes; } ... } To define a property of java.util.Set type, use the <set> tag to define the elements in the same way as a list. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <set> <value>A</value> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.javafasttrack.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> <null /> </set> </property> </bean> Using Set example Bean Using Set example Configuration
  • 8. Using Maps example Bean public class SequenceGenerator { ... private Map<Object, Object> suffixes; public void setSuffixes(Map<Object, Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... for (Map.Entry entry : suffixes.entrySet()) { buffer.append(&quot;-&quot;); buffer.append(entry.getKey()); buffer.append(&quot;@&quot;); buffer.append(entry.getValue()); } return buffer.toString(); } }
  • 9. <bean id=&quot;sequenceGenerator“ Using Maps example configuration class=&quot;SequenceGenerator&quot;> <property name=&quot;suffixes&quot;> <map> <entry> <key> <value>type</value> </key> <value>A</value> </entry> <entry> <key> <value>url</value> </key> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.apress.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> </entry> </map> </property> </bean>
  • 10. There are shortcuts to defining map keys and values as attributes of the <entry> tag. If they are simple constant values, you can define them by key and value. If they are bean references, you can define them by key-ref and value-ref. <bean id=&quot;sequenceGenerator&quot; class=&quot;com.apress.springrecipes.sequence.SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <map> <entry key=&quot;type&quot; value=&quot;A&quot; /> <entry key=&quot;url&quot;> <bean class=&quot;java.net.URL&quot;> <constructor-arg value=&quot;http&quot; /> <constructor-arg value=&quot;www.apress.com&quot; /> <constructor-arg value=&quot;/&quot; /> </bean> </entry> </map> </property> </bean>
  • 11. Using java.util.Properties A java.util.Properties collection is very similar to a map. It also implements the java.util.Map interface and stores entries in key/value pairs. The only difference is that the keys and values of a Properties collection are always strings....
  • 12. public class SequenceGenerator { ... private Properties suffixes; java.util.Properties Bean example public void setSuffixes(Properties suffixes) { this.suffixes = suffixes; } ... } To define a java.util.Properties collection in Spring, use the <props> tag with multiple <prop> tags as children. Each <prop> tag must have a key attribute defined and the corresponding value enclosed. <bean id=&quot;sequenceGenerator“ class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <props> <prop key=&quot;type&quot;>A</prop> <prop key=&quot;url&quot;>http://www.jftcom/</prop> </props></property> </bean>
  • 13. Merging the Collection of the Parent Bean If you define your beans with inheritance, a child bean’s collection can be merged with that of its parent by setting the merge attribute to true. For a <list> collection, the child elements will be appended after the parent’s to preserve the order. So, the following sequence generator will have four suffixes: A, B, A, and C.
  • 14. Merging the Collection of the Parent Bean Example <beans ...> <bean id=&quot;baseSequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <list> <value>A</value> <value>B</value> </list> </property> </bean> <bean id=&quot;sequenceGenerator&quot; parent=&quot;baseSequenceGenerator&quot;> <property name=&quot;suffixes&quot;> <list merge=&quot;true&quot; > <value>A</value> <value>C</value> </list> </property>
  • 15. Merging the Collection of the Parent Bean Example (Contd ..) </bean> ... </beans> For a <set> or <map> collection, the child elements will overwrite the parent’s if they have the same value. So, the following sequence generator will have three suffixes: A, B, and C. <beans ...> <bean id=&quot;baseSequenceGenerator&quot; class=&quot;com.apress.springrecipes.sequence.SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <set> <value>A</value> <value>B</value> </set> </property> </bean>
  • 16. <bean id=&quot;sequenceGenerator&quot; parent=&quot;baseSequenceGenerator&quot;> <property name=&quot;suffixes&quot;> <set merge=&quot;true&quot;> <value>A</value> <value>C</value> </set> </property> </bean> ... </beans>
  • 17.
  • 18. Now suppose you are going to accept a list of integer numbers as the suffixes of your sequence generator. Each number will be formatted into four digits by an instance of java.text. DecimalFormat. public class SequenceGenerator { ... private List<Object> suffixes; public void setSuffixes(List<Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... DecimalFormat formatter = new DecimalFormat(&quot;0000&quot;); for (Object suffix : suffixes) { buffer.append(&quot;-&quot;); buffer.append(formatter.format((Integer) suffix)); } return buffer.toString(); } }
  • 19. <bean id=&quot;sequenceGenerator&quot; class=&quot;com.apress.springrecipes.sequence.SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <list> <value>5</value>However, when you run this application, you will encounter a ClassCastException, indicating that the suffixes cannot be cast into integers because their type is String. Spring treats every element in a collection as a string by default <value>10</value> <value>20</value> </list> </property> </bean>
  • 20. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <list> <value type=&quot;int&quot;>5</value> <value type=&quot;int&quot;>10</value> <value type=&quot;int&quot;>20</value> </list> </property> </bean> Or you may set the value-type attribute of the collection tag to specify the type for all elements in this collection. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <list value-type=&quot;int&quot;> <value>5</value> <value>10</value> <value>20</value> </list> </property> </bean>
  • 21. In Java 1.5 or higher, you can define your suffixes list with a type-safe collection that stores integers. ... public class SequenceGenerator { ... private List<Integer> suffixes; public void setSuffixes(List<Integer> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... DecimalFormat formatter = new DecimalFormat(&quot;0000&quot;); for (int suffix : suffixes) { buffer.append(&quot;-&quot;); buffer.append(formatter.format(suffix)); }
  • 22. return buffer.toString(); } } Once you have defined your collections in a type-safe way, Spring will be able to read the collection’s type information through reflection. In this way, you no longer need to specify the value-type attribute of <list>. <bean id=&quot;sequenceGenerator“ class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <list> <value>5</value> <value>10</value> <value>20</value> </list> </property> </bean>
  • 23. class CarFactory{ Car static newInstance(){ return new Maruthi(); } } interface Car{ } Maruthi implements Car{ }
  • 24.
  • 25. <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> <property name=&quot;prefixGenerator&quot; ref=&quot;datePrefixGenerator&quot; /> <property name=&quot;initial&quot; value=&quot;100000&quot; /> <property name=&quot;suffixes&quot;> <bean class=&quot;org.springframework.beans.factory.config.SetFactoryBean&quot;> <property name=&quot; targetSetClass &quot;> <value> java.util.TreeSet </value> </property> <property name=&quot;sourceSet&quot;> <set> <value>5</value> <value>10</value> <value>20</value> </set> </property> </bean> </property> </bean>
  • 26. Or you can use a collection tag in the util schema to define a collection and set its target class (e.g., by the set-class attribute of <util:set>). But you must remember to add the util schema definition to your <beans> root element. <beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:util=&quot;http://www.springframework.org/schema/util&quot; xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd&quot;> <bean id=&quot;sequenceGenerator“ class=“SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <util:set set-class=&quot;java.util.TreeSet&quot;> <value>5</value> <value>10</value> <value>20</value> </util:set> </property> </bean> ... </beans>
  • 27.
  • 28. <beans ...> <bean id=&quot;sequenceGenerator&quot; class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <ref local=&quot;suffixes&quot; /> </property> </bean> <bean id=&quot;suffixes&quot; class=&quot;org.springframework.beans.factory.config.SetFactoryBean&quot;> <property name=&quot;sourceSet&quot;> <set> <value>5</value> <value>10</value> <value>20</value> </set> </property> </bean> ... </beans>
  • 29. Or you can define a stand-alone set by using the <util:set> tag in the util schema. <beans ...> <bean id=&quot;sequenceGenerator“ class=&quot;SequenceGenerator&quot;> ... <property name=&quot;suffixes&quot;> <ref local=&quot;suffixes&quot; /> </property> </bean> <util:set id=&quot;suffixes&quot;> <value>5</value> <value>10</value> <value>20</value> </util:set> ... </beans>
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38. class CarFactory{ Car static newInstance(){ return new Maruthi(); } } interface Car{ } Maruthi implements Car{ }
  • 39.
  • 40.
  • 41.