Spring Framework Standalone Collections
- Get link
- X
- Other Apps
Spring Framework allows to inject of collection objects into a bean through constructor dependency injection or setter dependency injection using <list>,<map>,<set>, etc. Given below is an example of the same.
Example Project
pom.xml:
- XML
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < groupId >org.example</ groupId > < artifactId >GFGArticles</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >jar</ packaging > < name >GFGArticles</ name > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >11</ maven.compiler.source > < maven.compiler.target >11</ maven.compiler.target > </ properties > < dependencies > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >5.1.0.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >5.1.0.RELEASE</ version > </ dependency > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >3.8.1</ version > < scope >test</ scope > </ dependency > </ dependencies > </ project > |
Student.java:
- Java
import java.util.*; public class Student { private int roll; private String name; private LinkedHashSet phnNo; private List<String> degree; private Map<String,Integer> courses; public Student( int roll, String name, LinkedHashSet phnNo, List<String> degree, Map<String, Integer> courses) { this .roll = roll; this .name = name; this .phnNo = phnNo; this .degree = degree; this .courses = courses; } @Override public String toString() { return "Student{" + "\n roll=" + roll + "\n name=" + name + "\n phnNo=" + phnNo + "\n degree=" + degree + "\n courses=" + courses + '}' ; } } |
config.xml:
- XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans < bean class = "Student" id = "s" > < constructor-arg value = "10" /> < constructor-arg value = "Priya" /> < constructor-arg > < set > < value >9215111448</ value > < value >8026570991</ value > < value >7015247338</ value > </ set > </ constructor-arg > < constructor-arg > < list > < value >BTech</ value > < value >MTech</ value > </ list > </ constructor-arg > < constructor-arg > < map > < entry key = "Hibernate" value = "1" /> < entry key = "Spring" value = "2" /> < entry key = "MySQL" value = "1" /> </ map > </ constructor-arg > </ bean > </ beans > |
App.java:
- Java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext ctx= new ClassPathXmlApplicationContext( "config.xml" ); Student s=ctx.getBean( "s" ,Student. class ); System.out.println(s); } } |
Output:
But Collection beans injected using the above method can not be reused. In order to create standalone Collection beans, util schema is used. The following code needs to be added to the configuration file:
config.xml:
- XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans < util:set set-class = "java.util.LinkedHashSet" id = "phnNo" > < value >9312552334</ value > < value >8723546787</ value > </ util:set > < util:list id = "degree" > < value >BA</ value > < value >MA</ value > </ util:list > < util:map id = "courses" > < entry key = "Django" value = "2" /> < entry key = "Bootstrap" value = "1" /> </ util:map > < bean class = "Student" id = "s" > < constructor-arg value = "12" type = "int" /> < constructor-arg value = "Nisha" /> < constructor-arg ref = "phnNo" /> < constructor-arg ref = "degree" /> < constructor-arg ref = "courses" /> </ bean > </ beans > |
Output:
- Get link
- X
- Other Apps
Comments
Post a Comment