Merge pull request #4326 from MherBaghinyan/BAEL-1799

Bael 1799
This commit is contained in:
Eric Martin 2018-06-06 20:40:35 -05:00 committed by GitHub
commit d389000afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package com.baeldung.collection;
/**
* Created by Gebruiker on 5/22/2018.
*/
public class BaeldungBean {
private String name;
public BaeldungBean(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.collection;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.*;
@Configuration
public class CollectionConfig {
@Bean
public CollectionsBean getCollectionsBean() {
return new CollectionsBean(new HashSet<>(Arrays.asList("John", "Adam", "Harry")));
}
@Bean
public List<String> nameList(){
return Arrays.asList("John", "Adam", "Harry", null);
}
@Bean
public Map<Integer, String> nameMap(){
Map<Integer, String> nameMap = new HashMap<>();
nameMap.put(1, "John");
nameMap.put(2, "Adam");
nameMap.put(3, "Harry");
return nameMap;
}
@Bean
@Qualifier("CollectionsBean")
@Order(2)
public BaeldungBean getElement() {
return new BaeldungBean("John");
}
@Bean
@Order(3)
public BaeldungBean getAnotherElement() {
return new BaeldungBean("Adam");
}
@Bean
@Order(1)
public BaeldungBean getOneMoreElement() {
return new BaeldungBean("Harry");
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.collection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Gebruiker on 5/18/2018.
*/
public class CollectionInjectionDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(CollectionConfig.class);
CollectionsBean collectionsBean = context.getBean(CollectionsBean.class);
collectionsBean.printNameList();
collectionsBean.printNameSet();
collectionsBean.printNameMap();
collectionsBean.printBeanList();
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by Gebruiker on 5/18/2018.
*/
public class CollectionsBean {
@Autowired
private List<String> nameList;
private Set<String> nameSet;
private Map<Integer, String> nameMap;
@Autowired(required = false)
@Qualifier("CollectionsBean")
private List<BaeldungBean> beanList = new ArrayList<>();
public CollectionsBean() {
}
public CollectionsBean(Set<String> strings) {
this.nameSet = strings;
}
@Autowired
public void setNameMap(Map<Integer, String> nameMap) {
this.nameMap = nameMap;
}
public void printNameList() {
System.out.println(nameList);
}
public void printNameSet() {
System.out.println(nameSet);
}
public void printNameMap() {
System.out.println(nameMap);
}
public void printBeanList() {
System.out.println(beanList);
}
}