0% found this document useful (0 votes)
24 views4 pages

Collection Dependency-Setter and Constructor Injection:: Package Import Import Import Import

The document describes how to inject dependencies into a Student class using constructor and setter injection in Spring. It defines a Student class with private fields for a List, Set, Map and Properties of student marks. It then defines two Student bean configurations in an XML file, one using setter injection and the other using constructor injection to set the field dependencies. The Main class retrieves the Student beans from the context and prints them.

Uploaded by

Prabhakar Prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Collection Dependency-Setter and Constructor Injection:: Package Import Import Import Import

The document describes how to inject dependencies into a Student class using constructor and setter injection in Spring. It defines a Student class with private fields for a List, Set, Map and Properties of student marks. It then defines two Student bean configurations in an XML file, one using setter injection and the other using constructor injection to set the field dependencies. The Main class retrieves the Student beans from the context and prints them.

Uploaded by

Prabhakar Prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Collection Dependency-Setter and Constructor Injection:

package com.app;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Student {

private List<String> marksList;


private Set<String> marksSet;
private Map<String,String> marksMap;
private Properties marksProp;

public Student() {
super();
}

public Student(List<String> marksList, Set<String> marksSet,


Map<String, String> marksMap, Properties marksProp) {
super();
this.marksList = marksList;
this.marksSet = marksSet;
this.marksMap = marksMap;
this.marksProp = marksProp;
}

public List<String> getMarksList() {


return marksList;
}

public void setMarksList(List<String> marksList) {


this.marksList = marksList;
}

public Set<String> getMarksSet() {


return marksSet;
}

public void setMarksSet(Set<String> marksSet) {


this.marksSet = marksSet;
}

public Map<String, String> getMarksMap() {


return marksMap;
}

public void setMarksMap(Map<String, String> marksMap) {


this.marksMap = marksMap;
}

public Properties getMarksProp() {


return marksProp;
}
public void setMarksProp(Properties marksProp) {
this.marksProp = marksProp;
}

@Override
public String toString() {
return "Student [marksList=" + marksList + ", marksSet=" + marksSet
+ ", marksMap=" + marksMap + ", marksProp=" +
marksProp + "]";
}

Config.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
">

<!-- By using setters -->


<bean class="com.app.Student" name="stdObj1">
<property name="marksList">
<list>
<value>abcd</value>
<value>abcd</value>
<value>mnop</value>
<value>mnop</value>
</list>
</property>
<property name="marksSet">
<set>
<value>abcd</value>
<value>abcd</value>
<value>mnop</value>
<value>mnop</value>
</set>
</property>
<property name="marksMap">
<map>
<entry>
<key>
<value>101</value>
</key>
<value>val1</value>
</entry>
<entry key="101">
<value>val2</value>
</entry>
<entry value="val3">
<key>
<value>103</value>
</key>
</entry>
<entry value="val3" key="104"/>
</map>
</property>

<property name="marksProp">
<props>
<prop key="101">abcd</prop>
<prop key="103">MNOP</prop>
</props>

</property>
</bean>

<!-- by using constructor -->


<bean class="com.app.Student" name="stdObj2">
<constructor-arg index="0">
<list>
<value>abcdc</value>
<value>abcdc</value>
<value>mnopc</value>
<value>mnopc</value>
</list>
</constructor-arg>

<constructor-arg type="java.util.Set">
<set>
<value>abcdc</value>
<value>abcdc</value>
<value>mnopc</value>
<value>mnopc</value>
</set>
</constructor-arg>

<constructor-arg name="marksMap">
<map>
<entry>
<key>
<value>101</value>
</key>
<value>val1c</value>
</entry>
<entry key="101">
<value>val2c</value>
</entry>
<entry value="val3">
<key>
<value>103c</value>
</key>
</entry>
<entry value="val3c" key="104"/>
</map>
</constructor-arg>

<constructor-arg index="3">
<props>
<prop key="101">abcdc</prop>
<prop key="103">MNOPc</prop>
</props>
</constructor-arg>

</bean>

</beans>

Main.java

package com.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

/**
* @param args
* @author Raghu
*/
public static void main(String[] args) {

ApplicationContext context=new
ClassPathXmlApplicationContext("config.xml");

System.out.println(context.getBean("stdObj2", Student.class));
System.out.println(context.getBean("stdObj1", Student.class));
}

You might also like