Close

Spring Framework - Injecting Collections Examples

Spring Framework 

Constructor based Set injection: Even though instance of a set is directly passed to the constructor, it's similar to the constructor based injection (XML configuration). Please see proxied using CGLIB concepts how that works.

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SetInjectionExample {
@Bean
public TestBean testBean () {
return new TestBean(new HashSet(Arrays.asList("one hundred", "two hundred", "three hundred")));
}

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SetInjectionExample.class);
TestBean bean = context.getBean(TestBean.class);
System.out.println(bean.getStringSet());
}

private static class TestBean {
private final Set<String> stringSet;


private TestBean (Set<String> stringSet) {
this.stringSet = stringSet;
}

public Set<String> getStringSet () {
return stringSet;
}
}
}
Original Post




Component scanning and collection injection, based on generic type and qualifier

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Set;

@PropertySource("classpath:app-props.properties")
@Configuration
@ComponentScan(basePackageClasses = SetInjectionScanRefQualifierExample.class,
useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {SetInjectionScanRefQualifierExample.class}))

public class SetInjectionScanRefQualifierExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
SetInjectionScanRefQualifierExample.class);

TestBean bean = context.getBean(TestBean.class);
System.out.println(bean.getRefBeanServices());
}

@Component
public static class TestBean {

private Set<RefBeanService> refBeanServices;

@Autowired
@Qualifier("myRefBean")
public void setRefBeanServices (Set<RefBeanService> refBeanServices) {
this.refBeanServices = refBeanServices;
}

public Set<RefBeanService> getRefBeanServices () {
return refBeanServices;
}
}

public static interface RefBeanService {
String getStr ();
}

@Component
@Qualifier("myRefBean")
public static class RefBean implements RefBeanService {
private String str;

public String getStr () {
return str;
}

@Value("${some-prop1:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{" + "str='" + str + '\'' + '}';
}
}

@Component
public static class RefBean2 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop2:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{str='" + str + "'}";
}
}

@Component
@Qualifier("myRefBean")
public static class RefBean3 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop3:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{ str='" + str + "'}";
}
}
}
Original Post




Component scanning and constructor based injection: The Set of strings is registered as bean itself.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Configuration
@ComponentScan(basePackageClasses = SetInjectionScanExample.class, useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = SetInjectionScanExample.class)})
public class SetInjectionScanExample {

@Bean
public Set<String> strSet () {
return new HashSet<>(Arrays.asList("two hundred", "three hundred", "four hundred"));
}

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SetInjectionScanExample.class);
TestBean bean = context.getBean(TestBean.class);
System.out.println(bean.getStrSet());
}


@Component
static class TestBean {
private final Set<String> strSet;

//@Autowired Spring 4.3 and later doesn't required the annotation
TestBean (Set<String> strSet) {
this.strSet = strSet;
}

public Set<String> getStrSet () {
return strSet;
}
}
}
Original Post




Injecting collection, based on matching types and qualifier: This is a setter-based injection and the injection point TestBean#setRefBeanServices (Set<RefBeanService> refBeanServices) will be injected with all implementations of RefBeanService having the same qualifier.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import java.util.Set;

@PropertySource("classpath:app-props.properties")
@Configuration

public class SetInjectionRefQualifierExample {

@Bean
public TestBean testBean () {
return new TestBean();
}

@Bean
@Qualifier("myRefBean")
public RefBeanService refBean1 () {
return new RefBean();
}

@Bean
public RefBeanService refBean2 () {
return new RefBean2();
}

@Bean
@Qualifier("myRefBean")
public RefBeanService refBean3 () {
return new RefBean3();
}

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
SetInjectionRefQualifierExample.class);

TestBean bean = context.getBean(TestBean.class);
System.out.println(bean.getRefBeanServices());
}

public static class TestBean {


private Set<RefBeanService> refBeanServices;

@Autowired
@Qualifier("myRefBean")
public void setRefBeanServices (Set<RefBeanService> refBeanServices) {
this.refBeanServices = refBeanServices;
}

public Set<RefBeanService> getRefBeanServices () {
return refBeanServices;
}
}

public static interface RefBeanService {
String getStr ();
}

public static class RefBean implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop1:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{" +
"str='" + str + '\'' +
'}';
}
}

public static class RefBean2 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop2:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{" +
"str='" + str + '\'' +
'}';
}
}

public static class RefBean3 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop3:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{" +
"str='" + str + '\'' +
'}';
}
}
}
Original Post




See Also