list, set injection

This commit is contained in:
mherbaghinyan 2018-05-19 12:50:48 +04:00
parent c7cd7d0744
commit d73f653eb0
3 changed files with 20 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
@Configuration @Configuration
@ -11,11 +12,11 @@ public class CollectionConfig {
@Bean @Bean
public CollectionsBean getCollectionsBean() { public CollectionsBean getCollectionsBean() {
return new CollectionsBean(); return new CollectionsBean(new HashSet<>(Arrays.asList("John", "Adam", "Harry")));
} }
@Bean @Bean
public List<String> nameList(){ public List<String> nameList(){
return Arrays.asList("John", "Adam", "Harry"); return Arrays.asList("John", "Adam", "Harry", null);
} }
} }

View File

@ -9,8 +9,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
public class CollectionInjectionDemo { public class CollectionInjectionDemo {
public static void main(String[] args) { public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(CollectionConfig.class); ApplicationContext context = new AnnotationConfigApplicationContext(CollectionConfig.class);
CollectionsBean collectionsBean = (CollectionsBean)context.getBean(CollectionsBean.class); CollectionsBean collectionsBean = context.getBean(CollectionsBean.class);
collectionsBean.printNameList(); collectionsBean.printNameList();
collectionsBean.printNameSet();
} }
} }

View File

@ -3,6 +3,7 @@ package com.baeldung.collection;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Created by Gebruiker on 5/18/2018. * Created by Gebruiker on 5/18/2018.
@ -12,7 +13,20 @@ public class CollectionsBean {
@Autowired @Autowired
private List<String> nameList; private List<String> nameList;
private Set<String> nameSet;
public CollectionsBean() {
}
public CollectionsBean(Set<String> strings) {
this.nameSet = strings;
}
public void printNameList() { public void printNameList() {
System.out.println(nameList); System.out.println(nameList);
} }
public void printNameSet() {
System.out.println(nameSet);
}
} }