commit
d389000afd
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue