JAVA-628: Moved 2 articles from spring-core-3
This commit is contained in:
parent
cf4ed21884
commit
c3e41be3c4
|
@ -7,8 +7,8 @@ This module contains articles about core Spring functionality
|
|||
- [Understanding getBean() in Spring](https://www.baeldung.com/spring-getbean)
|
||||
- [Guide to the Spring BeanFactory](https://www.baeldung.com/spring-beanfactory)
|
||||
- [How to use the Spring FactoryBean?](https://www.baeldung.com/spring-factorybean)
|
||||
- [Spring – Injecting Collections](https://www.baeldung.com/spring-injecting-collections)
|
||||
- [Design Patterns in the Spring Framework](https://www.baeldung.com/spring-framework-design-patterns)
|
||||
- [Injecting a Value in a Static Field in Spring](https://www.baeldung.com/spring-inject-static-field)
|
||||
- [Difference Between BeanFactory and ApplicationContext](https://www.baeldung.com/spring-beanfactory-vs-applicationcontext)
|
||||
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
||||
- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope)
|
||||
- More articles: [[<-- prev]](/spring-core-2)
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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();
|
||||
collectionsBean.printNameListWithDefaults();
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package com.baeldung.collection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* 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<>();
|
||||
|
||||
@Value("${names.list:}#{T(java.util.Collections).emptyList()}")
|
||||
private List<String> nameListWithDefaultValue;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void printNameListWithDefaults() {
|
||||
System.out.println(nameListWithDefaultValue);
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package com.baeldung.staticvalue.injection;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("/application.properties")
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package com.baeldung.staticvalue.injection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class PropertyController {
|
||||
|
||||
@Value("${name}")
|
||||
private String name;
|
||||
|
||||
@Value("${name}")
|
||||
private static String NAME_NULL;
|
||||
|
||||
private static String NAME_STATIC;
|
||||
|
||||
@Value("${name}")
|
||||
public void setNameStatic(String name){
|
||||
PropertyController.NAME_STATIC = name;
|
||||
}
|
||||
|
||||
@GetMapping("/properties")
|
||||
public List<String> getProperties(){
|
||||
return Arrays.asList(this.name, NAME_STATIC, NAME_NULL) ;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue