How To Inject Set Into Spring Bean With Example?

In This Article We will Learn How To Inject Set to the spring bean.

Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.7

Project Directory Structure

Maven Dependencies

Add The Following Dependencies to the pom.xml

  • spring-core
  • spring-beans
  • spring-context

1.pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.practice</groupId>
<artifactId>InjectListDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>


</dependencies>
</project>

2.Writing Spring Beans

In This Example Im Writting Two Spring Beans Named With AddressBook and Welcome

AddressBook.java


package com.practice;

import java.util.Set;

public class AddressBook {
private Set houseNumbers;

public Set getHouseNumbers() {
return houseNumbers;
}

public void setHouseNumbers(Set houseNumbers) {
this.houseNumbers = houseNumbers;
}

@Override
public String toString() {
return "AddressBook [houseNumbers=" + houseNumbers + "]";
}



}

Welcome.java


package com.practice;

public class Welcome {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public String toString() {
return "Welcome [message=" + message + "]";
}


}

3.Setting Up Spring Cofiguration File

To Inject Set Of Values Into Spring Bean we need to use <set> tag and <value> tag in spring configuration File(applicationContext.xml).

applicationContext.xml



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="addressBook" class="com.practice.AddressBook">
<property name="houseNumbers">
<set>
<value>1111</value>
<value>2222</value>
<value>2333</value>
<value>1111</value>
<ref bean = "welcomeBean"/>
</set>
</property>
</bean>
<bean id="welcomeBean" class="com.practice.Welcome">
<property name="message" value="Welcome to Javalschool"></property>
</bean>
</beans>

4.Now Create Test class ,RunThe Code

Test.java


package cm.practice;

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

import com.practice.AddressBook;


public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AddressBook address = (AddressBook)context.getBean("addressBook");
System.out.println(address.toString());

}

}

output:


AddressBook [houseNumbers=[1111, 2222, 2333, Welcome [message=Welcome to Javalschool]]]

No comments:

Post a Comment