Injecting Java Collections In Spring

By | | Updated : 2020-12-27 | Viewed : 64 times

injecting-java-collections-in-spring

In this tutorial, we will learn about injecting java collections in spring bean. So let's start looking into different types of Spring injecting Java collections using annotations.

Java List injection in Spring

We will see here List injection in spring annotations. Please have a look at the given example. Here we are taking one class which is Movies to print the list of movies. But the list is here injected list. So we will see how to inject it.

Movies.java
public class Movies {
    @Autowired
    private List<String> movieList;
}

Here we have a Movies class with movieList. movieList will be created outside of Movies class and will be injected. Please observe the AppConfig class below. @Autowired annotation used for injecting Spring beans.

AppConfig.java
@Configuration
public class AppConfig {

    @Bean
    public Movies getMovies() {
        return new Movies();
    }

    @Bean
    public List<String> movieList() {
        return Arrays.asList("Iron Man", "Spider Man", "Thor");
    }
}

So Movie and movieList classes will be initiated here and will be injected as shown above Movie class. We need to MainClient class to test this injection example. Please find GitHub for the entire code example.

Java Set injection in Spring

Now we will learn to know how to Java Set injection in Spring. We will write the Movies class as above given. A change is that set instead of a list. So the class will be as given below.

Movies.java
public class Movies {

    @Autowired
    private Set<String> moviesSet;

    public Movies() {
    }
}

And now AppConfig class is required for preparing the Set of Movies for injecting into Movies class. So the AppConfig class is

AppConfig.java
@Configuration
public class AppConfig {

    @Bean
    public Movies getMovies() {
        return new Movies();
    }

    @Bean
    public Set<String> moviesSet() {
        Set moviesSet = new HashSet<>();
        //.. add the movies here
        return moviesSet;
    }
}

So Set of Movies and Movies beans will be created. As per the definition of the Movies class, the Set of Movies will be injected when Movies object initiated.

Java Map injection in Spring

So for injection of the heroMovieMap into Movies class, we need to movies class as given below.

Movies.java
public class Movies {

    @Autowired
    private Map<String, String> heroMovieMap;

    public Movies() {
    }

}

heroMovieMap will get injected here. in AppConfig, the instantiation for heroMovieMap and Movies is written. Please look at the below-given code snippet.

AppConfig.java
@Configuration
public class AppConfig {

    @Bean
    public Movies getMovies() {
        return new Movies();
    }

    @Bean
    public Map<String, String> heroMovieMap() {
        Map heroMovieMap = new HashMap();
       //..add here hero and Movie entries here
        return heroMovieMap;
    }
}

Spring Bean injection in Spring

So far we looked at how to inject collection class injection into the Spring bean or other class. Now we will look into the injection of Actor class into the Movie class by using the Hero Spring bean reference.

Please look at the Actor and Movies class as given below.

Actor.java
@Getter
public class Actor {
    private String actorName;
}
Movie.java
@Getter
public class Movie {

    @Autowired
    private List<Actor> actorList;
}
AppConfig.java
@Configuration
public class AppConfig {

    @Bean
    public Actor getActor1() {
        return new Actor("Hero");
    }

    @Bean
    public Actor getActor2() {
        return new Actor("Heroine");
    }

    @Bean
    public Movie getMovie() {
        return new Movie("Movie");
    }
}

Here in AppConfig we configure for two actors and one movie. When we creating the Movie instance using application context then these two actor objects will be set to actorList in Movie class.

Properties injection in Spring

One new @Value annotation is introduced in Spring to get the property value into the class object. We will see the same with an example.

In Movie class, we are trying to set the up movieName from properties. Please look at the class below.

Movie.java
@Getter
public class Movie {

    @Value("${test.movie.name}")
    private String movieName;
}
AppConfig.java
@Configuration
@PropertySource("classpath:test.properties")
public class AppConfig {

    @Bean
    public Movie getASpringBean() {
        return new Movie();
    }
}

Here we used the test.properties in which the movieName is configured.

The code repository for this article is in Github. Please look at Spring-Injecting-Collections-Example-App

Leave A Reply