map injection
This commit is contained in:
		
							parent
							
								
									d73f653eb0
								
							
						
					
					
						commit
						4cdf7fe9ef
					
				| @ -3,9 +3,7 @@ package com.baeldung.collection; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.HashSet; | ||||
| import java.util.List; | ||||
| import java.util.*; | ||||
| 
 | ||||
| @Configuration | ||||
| public class CollectionConfig { | ||||
| @ -19,4 +17,13 @@ public class CollectionConfig { | ||||
|     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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -14,5 +14,6 @@ public class CollectionInjectionDemo { | ||||
|         CollectionsBean collectionsBean = context.getBean(CollectionsBean.class); | ||||
|         collectionsBean.printNameList(); | ||||
|         collectionsBean.printNameSet(); | ||||
|         collectionsBean.printNameMap(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -3,6 +3,7 @@ package com.baeldung.collection; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| 
 | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| /** | ||||
| @ -15,6 +16,8 @@ public class CollectionsBean { | ||||
| 
 | ||||
|     private Set<String> nameSet; | ||||
| 
 | ||||
|     private Map<Integer, String> nameMap; | ||||
| 
 | ||||
|     public CollectionsBean() { | ||||
|     } | ||||
| 
 | ||||
| @ -22,6 +25,11 @@ public class CollectionsBean { | ||||
|         this.nameSet = strings; | ||||
|     } | ||||
| 
 | ||||
|     @Autowired | ||||
|     public void setNameMap(Map<Integer, String> nameMap) { | ||||
|         this.nameMap = nameMap; | ||||
|     } | ||||
| 
 | ||||
|     public void printNameList() { | ||||
|         System.out.println(nameList); | ||||
|     } | ||||
| @ -29,4 +37,8 @@ public class CollectionsBean { | ||||
|     public void printNameSet() { | ||||
|         System.out.println(nameSet); | ||||
|     } | ||||
| 
 | ||||
|     public void printNameMap() { | ||||
|         System.out.println(nameMap); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user