JAVA-628: Moved 3 articles to spring-di-2, added README
This commit is contained in:
@@ -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,21 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Another Arbitrary Dependency";
|
||||
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "autowiredFieldDependency")
|
||||
public class ArbitraryDependency {
|
||||
|
||||
private final String label = "Arbitrary Dependency";
|
||||
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class YetAnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Yet Another Arbitrary Dependency";
|
||||
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.staticvalue.injection;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
|
||||
@PropertySource("/application.properties")
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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) ;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user