diff --git a/spring-core/src/main/java/com/baeldung/collection/CollectionConfig.java b/spring-core/src/main/java/com/baeldung/collection/CollectionConfig.java index 6034ac9e02..8ccf76ebc5 100644 --- a/spring-core/src/main/java/com/baeldung/collection/CollectionConfig.java +++ b/spring-core/src/main/java/com/baeldung/collection/CollectionConfig.java @@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Arrays; +import java.util.HashSet; import java.util.List; @Configuration @@ -11,11 +12,11 @@ public class CollectionConfig { @Bean public CollectionsBean getCollectionsBean() { - return new CollectionsBean(); + return new CollectionsBean(new HashSet<>(Arrays.asList("John", "Adam", "Harry"))); } @Bean public List nameList(){ - return Arrays.asList("John", "Adam", "Harry"); + return Arrays.asList("John", "Adam", "Harry", null); } } diff --git a/spring-core/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java b/spring-core/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java index 10eb6203a9..81d804bce9 100644 --- a/spring-core/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java +++ b/spring-core/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java @@ -9,8 +9,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext public class CollectionInjectionDemo { public static void main(String[] args) { + ApplicationContext context = new AnnotationConfigApplicationContext(CollectionConfig.class); - CollectionsBean collectionsBean = (CollectionsBean)context.getBean(CollectionsBean.class); + CollectionsBean collectionsBean = context.getBean(CollectionsBean.class); collectionsBean.printNameList(); + collectionsBean.printNameSet(); } } diff --git a/spring-core/src/main/java/com/baeldung/collection/CollectionsBean.java b/spring-core/src/main/java/com/baeldung/collection/CollectionsBean.java index 80db4ae6c3..2d86a45f70 100644 --- a/spring-core/src/main/java/com/baeldung/collection/CollectionsBean.java +++ b/spring-core/src/main/java/com/baeldung/collection/CollectionsBean.java @@ -3,6 +3,7 @@ package com.baeldung.collection; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; +import java.util.Set; /** * Created by Gebruiker on 5/18/2018. @@ -12,7 +13,20 @@ public class CollectionsBean { @Autowired private List nameList; + private Set nameSet; + + public CollectionsBean() { + } + + public CollectionsBean(Set strings) { + this.nameSet = strings; + } + public void printNameList() { System.out.println(nameList); } + + public void printNameSet() { + System.out.println(nameSet); + } }