Close

Spring Framework - Injecting Arrays Examples

Spring Framework 

Setter based injection of strings array: The arrays is registered as a bean (using @Bean) itself. This examples uses setter-based injection in the target bean.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

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

@Bean
public String[] strArray () {
return new String[]{"two", "three", "four"};
}

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

TestBean bean = context.getBean(TestBean.class);
System.out.println(Arrays.toString(bean.getStringArray()));
}

private static class TestBean {
private String[] stringArray;

@Autowired
public void setStringArray (String[] stringArray) {
this.stringArray = stringArray;
}

public String[] getStringArray () {
return stringArray;
}
}
}
Original Post




Injecting an array to the constructor: This example uses JavaConfig to pass an array of strings to the target bean constructor. It's still a constructor based injection in a sense that all @Bean methods are proxied using CGLIB. Each time TestBean is injected or access by the Spring container, the testBean() method call returns the same singleton instance of the bean and doesn't create a new instance or pass a new array to constructor.

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

import java.util.Arrays;

@Configuration
public class ArrayInjectionExample {

@Bean
public TestBean testBean () {
return new TestBean(new String[]{"one", "two", "three"});
}

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

TestBean bean = context.getBean(TestBean.class);
System.out.println(Arrays.toString(bean.getStringArray()));
}

private static class TestBean {
private final String[] stringArray;

public TestBean (String[] stringArray) {
this.stringArray = stringArray;
}

public String[] getStringArray () {
return stringArray;
}
}
}
Original Post




Using Component scan to inject an array: The array is registered as bean. This is a constructor based injection.

@Configuration

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

@Bean
public String[] strArray () {
return new String[]{"two", "three", "four"};
}

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


@Component
static class TestBean {
private final String[] stringArray;

@Autowired// Spring 4.3 doesn't require this annotation for constructor injection anymore
public TestBean (String[] stringArray) {
this.stringArray = stringArray;
}

public String[] getStringArray () {
return stringArray;
}
}
}
Original Post




Array injection, based on matching type: This example uses component scanning. The injection point is an array of the interfaceRefBeanService. All beans which are implementing this interface will be injected to the array unless we do some selection based on a qualifier.

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

import java.util.Arrays;

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

public class ArrayInjectionScanRefExample {

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

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

@Component
static class TestBean {
private final RefBeanService[] refBeanServices;

@Autowired // this annotation not needed starting spring 4.3
private TestBean (RefBeanService[] refBean) {
this.refBeanServices = refBean;
}

public RefBeanService[] getRefBeanServices () {
return refBeanServices;
}
}

private static interface RefBeanService {
String getStr ();
}

@Component
private static class RefBean1 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 + '\'' +
'}';
}
}

@Component
private 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 + '\'' +
'}';
}
}
}
Original Post

Array injection, based on a qualifier: The injection point is an array of the interfaceRefBeanService. All beans which are implementing this interface will be injected to the array given that the are registered using the same qualifier.

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


public String[] strArray () {
return new String[]{"two", "three", "four"};
}

@Bean(name = "myArray")
public String[] strArray2 () {
return new String[]{"five", "six", "seven"};
}

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

private static class TestBean {
private String[] stringArray;

@Resource(name = "myArray")
public void setStringArray (String[] stringArray) {
this.stringArray = stringArray;
}

public String[] getStringArray () {
return stringArray;
}
}
}
Original Post




Injecting the Bean array by name and using @Qualifier

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

@Bean
public String[] strArray () {
return new String[]{"two", "three", "four"};
}

@Bean(name = "myArray")
public String[] strArray2 () {
return new String[]{"five", "six", "seven"};
}

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

@Component
static class TestBean {
private final String[] stringArray;

public TestBean (@Qualifier("myArray") String[] stringArray) {
this.stringArray = stringArray;
}

public String[] getStringArray () {
return stringArray;
}
}
}
Original Post




See Also