diff --git a/spring-di-2/README.md b/spring-di-2/README.md new file mode 100644 index 0000000000..15249efa7c --- /dev/null +++ b/spring-di-2/README.md @@ -0,0 +1,11 @@ +## Spring Dependency Injection + +This module contains articles about dependency injection with Spring + +### Relevant Articles + +- [Injecting Spring Beans into Unmanaged Objects](https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects) +- [Injecting a Value in a Static Field in Spring](https://www.baeldung.com/spring-inject-static-field) +- [Spring – Injecting Collections](https://www.baeldung.com/spring-injecting-collections) +- [Wiring in Spring: @Autowired, @Resource and @Inject](https://www.baeldung.com/spring-annotations-resource-inject-autowire) +- More articles: [[<-- prev]](/spring-di) \ No newline at end of file diff --git a/spring-di-2/pom.xml b/spring-di-2/pom.xml index 9b703d55d9..4dd92ca18c 100644 --- a/spring-di-2/pom.xml +++ b/spring-di-2/pom.xml @@ -25,11 +25,26 @@ spring-boot-starter-data-jpa ${spring-boot.version} + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + org.springframework spring-aspects ${spring.version} + + org.projectlombok + lombok + ${lombok.version} + + + javax.inject + javax.inject + ${javax.inject.version} + @@ -61,5 +76,6 @@ 2.3.1.RELEASE 1.11 + 1 \ No newline at end of file diff --git a/spring-di-2/src/main/java/com/baeldung/collection/BaeldungBean.java b/spring-di-2/src/main/java/com/baeldung/collection/BaeldungBean.java new file mode 100644 index 0000000000..6d7351df02 --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/collection/BaeldungBean.java @@ -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; + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/collection/CollectionConfig.java b/spring-di-2/src/main/java/com/baeldung/collection/CollectionConfig.java new file mode 100644 index 0000000000..fbae2963e5 --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/collection/CollectionConfig.java @@ -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 nameList(){ + return Arrays.asList("John", "Adam", "Harry", null); + } + + @Bean + public Map nameMap(){ + Map 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"); + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java b/spring-di-2/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java new file mode 100644 index 0000000000..2ee265f134 --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/collection/CollectionInjectionDemo.java @@ -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(); + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/collection/CollectionsBean.java b/spring-di-2/src/main/java/com/baeldung/collection/CollectionsBean.java new file mode 100644 index 0000000000..fc90f2c6ff --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/collection/CollectionsBean.java @@ -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 nameList; + + private Set nameSet; + + private Map nameMap; + + @Autowired(required = false) + @Qualifier("CollectionsBean") + private List beanList = new ArrayList<>(); + + @Value("${names.list:}#{T(java.util.Collections).emptyList()}") + private List nameListWithDefaultValue; + + public CollectionsBean() { + } + + public CollectionsBean(Set strings) { + this.nameSet = strings; + } + + @Autowired + public void setNameMap(Map 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); + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/dependency/AnotherArbitraryDependency.java b/spring-di-2/src/main/java/com/baeldung/dependency/AnotherArbitraryDependency.java new file mode 100644 index 0000000000..0e19523b7e --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/dependency/AnotherArbitraryDependency.java @@ -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; + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/dependency/ArbitraryDependency.java b/spring-di-2/src/main/java/com/baeldung/dependency/ArbitraryDependency.java new file mode 100644 index 0000000000..3c90492d2c --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/dependency/ArbitraryDependency.java @@ -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; + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java b/spring-di-2/src/main/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java new file mode 100644 index 0000000000..a88abd0924 --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java @@ -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; + } +} diff --git a/spring-di-2/src/main/java/com/baeldung/staticvalue/injection/Application.java b/spring-di-2/src/main/java/com/baeldung/staticvalue/injection/Application.java new file mode 100644 index 0000000000..b4222ddcc9 --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/staticvalue/injection/Application.java @@ -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); + } + +} diff --git a/spring-di-2/src/main/java/com/baeldung/staticvalue/injection/PropertyController.java b/spring-di-2/src/main/java/com/baeldung/staticvalue/injection/PropertyController.java new file mode 100644 index 0000000000..f5910ea4f8 --- /dev/null +++ b/spring-di-2/src/main/java/com/baeldung/staticvalue/injection/PropertyController.java @@ -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 getProperties(){ + return Arrays.asList(this.name, NAME_STATIC, NAME_NULL) ; + } +} diff --git a/spring-di-2/src/main/resources/application.properties b/spring-di-2/src/main/resources/application.properties new file mode 100644 index 0000000000..828fa9cd2a --- /dev/null +++ b/spring-di-2/src/main/resources/application.properties @@ -0,0 +1 @@ +name = Inject a value to a static field diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredName.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredName.java new file mode 100644 index 0000000000..3046e68829 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredName.java @@ -0,0 +1,9 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = {"com.baeldung.dependency"}) +public class ApplicationContextTestAutowiredName { +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredQualifier.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredQualifier.java new file mode 100644 index 0000000000..33969ea69d --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredQualifier.java @@ -0,0 +1,25 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.AnotherArbitraryDependency; +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestAutowiredQualifier { + + @Bean + public ArbitraryDependency autowiredFieldDependency() { + ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency(); + + return autowiredFieldDependency; + } + + @Bean + public ArbitraryDependency anotherAutowiredFieldDependency() { + ArbitraryDependency anotherAutowiredFieldDependency = new AnotherArbitraryDependency(); + + return anotherAutowiredFieldDependency; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredType.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredType.java new file mode 100644 index 0000000000..24cdd978e4 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestAutowiredType.java @@ -0,0 +1,16 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestAutowiredType { + + @Bean + public ArbitraryDependency autowiredFieldDependency() { + ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency(); + return autowiredFieldDependency; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectName.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectName.java new file mode 100644 index 0000000000..cb465d0183 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectName.java @@ -0,0 +1,17 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.dependency.YetAnotherArbitraryDependency; + +@Configuration +public class ApplicationContextTestInjectName { + + @Bean + public ArbitraryDependency yetAnotherFieldInjectDependency() { + ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency(); + return yetAnotherFieldInjectDependency; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectQualifier.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectQualifier.java new file mode 100644 index 0000000000..c2a63dac9e --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectQualifier.java @@ -0,0 +1,23 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.AnotherArbitraryDependency; +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestInjectQualifier { + + @Bean + public ArbitraryDependency defaultFile() { + ArbitraryDependency defaultFile = new ArbitraryDependency(); + return defaultFile; + } + + @Bean + public ArbitraryDependency namedFile() { + ArbitraryDependency namedFile = new AnotherArbitraryDependency(); + return namedFile; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectType.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectType.java new file mode 100644 index 0000000000..15a75b8f2d --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestInjectType.java @@ -0,0 +1,16 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestInjectType { + + @Bean + public ArbitraryDependency injectDependency() { + ArbitraryDependency injectDependency = new ArbitraryDependency(); + return injectDependency; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestResourceNameType.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestResourceNameType.java new file mode 100644 index 0000000000..708ade7647 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestResourceNameType.java @@ -0,0 +1,16 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.File; + +@Configuration +public class ApplicationContextTestResourceNameType { + + @Bean(name = "namedFile") + public File namedFile() { + File namedFile = new File("namedFile.txt"); + return namedFile; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestResourceQualifier.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestResourceQualifier.java new file mode 100644 index 0000000000..87864b183e --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/ApplicationContextTestResourceQualifier.java @@ -0,0 +1,22 @@ +package com.baeldung.wiring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.File; + +@Configuration +public class ApplicationContextTestResourceQualifier { + + @Bean(name = "defaultFile") + public File defaultFile() { + File defaultFile = new File("defaultFile.txt"); + return defaultFile; + } + + @Bean(name = "namedFile") + public File namedFile() { + File namedFile = new File("namedFile.txt"); + return namedFile; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldAutowiredIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldAutowiredIntegrationTest.java new file mode 100644 index 0000000000..f3f065ed4d --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldAutowiredIntegrationTest.java @@ -0,0 +1,30 @@ +package com.baeldung.wiring.configuration.autowired; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.wiring.configuration.ApplicationContextTestAutowiredType; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestAutowiredType.class) +public class FieldAutowiredIntegrationTest { + + @Autowired + private ArbitraryDependency fieldDependency; + + @Test + public void givenAutowired_WhenSetOnField_ThenDependencyResolved() { + assertNotNull(fieldDependency); + assertEquals("Arbitrary Dependency", fieldDependency.toString()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldAutowiredNameIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldAutowiredNameIntegrationTest.java new file mode 100644 index 0000000000..199241c7e2 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldAutowiredNameIntegrationTest.java @@ -0,0 +1,30 @@ +package com.baeldung.wiring.configuration.autowired; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.wiring.configuration.ApplicationContextTestAutowiredName; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestAutowiredName.class) +public class FieldAutowiredNameIntegrationTest { + + @Autowired + private ArbitraryDependency autowiredFieldDependency; + + @Test + public void givenAutowiredAnnotation_WhenOnField_ThenDependencyValid() { + assertNotNull(autowiredFieldDependency); + assertEquals("Arbitrary Dependency", autowiredFieldDependency.toString()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldQualifierAutowiredIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldQualifierAutowiredIntegrationTest.java new file mode 100644 index 0000000000..081fbf24ad --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/autowired/FieldQualifierAutowiredIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.wiring.configuration.autowired; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.wiring.configuration.ApplicationContextTestAutowiredQualifier; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestAutowiredQualifier.class) +public class FieldQualifierAutowiredIntegrationTest { + + @Autowired + @Qualifier("autowiredFieldDependency") + private ArbitraryDependency fieldDependency1; + + @Autowired + @Qualifier("anotherAutowiredFieldDependency") + private ArbitraryDependency fieldDependency2; + + @Test + public void givenAutowiredQualifier_WhenOnField_ThenDep1Valid() { + assertNotNull(fieldDependency1); + assertEquals("Arbitrary Dependency", fieldDependency1.toString()); + } + + @Test + public void givenAutowiredQualifier_WhenOnField_ThenDep2Valid() { + assertNotNull(fieldDependency2); + assertEquals("Another Arbitrary Dependency", fieldDependency2.toString()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldByNameInjectIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldByNameInjectIntegrationTest.java new file mode 100644 index 0000000000..d1a75d73ea --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldByNameInjectIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.wiring.configuration.inject; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.wiring.configuration.ApplicationContextTestInjectName; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestInjectName.class) +public class FieldByNameInjectIntegrationTest { + + @Inject + @Named("yetAnotherFieldInjectDependency") + private ArbitraryDependency yetAnotherFieldInjectDependency; + + @Test + public void givenInjectQualifier_WhenSetOnField_ThenDependencyValid() { + assertNotNull(yetAnotherFieldInjectDependency); + assertEquals("Yet Another Arbitrary Dependency", yetAnotherFieldInjectDependency.toString()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldInjectIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldInjectIntegrationTest.java new file mode 100644 index 0000000000..995f560701 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldInjectIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.wiring.configuration.inject; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.wiring.configuration.ApplicationContextTestInjectType; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestInjectType.class) +public class FieldInjectIntegrationTest { + + @Inject + private ArbitraryDependency fieldInjectDependency; + + @Test + public void givenInjectAnnotation_WhenOnField_ThenValidDependency() { + assertNotNull(fieldInjectDependency); + assertEquals("Arbitrary Dependency", fieldInjectDependency.toString()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldQualifierInjectIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldQualifierInjectIntegrationTest.java new file mode 100644 index 0000000000..67fa2bf3d4 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/inject/FieldQualifierInjectIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.wiring.configuration.inject; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.wiring.configuration.ApplicationContextTestInjectQualifier; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestInjectQualifier.class) +public class FieldQualifierInjectIntegrationTest { + + @Inject + @Qualifier("defaultFile") + private ArbitraryDependency defaultDependency; + + @Inject + @Qualifier("namedFile") + private ArbitraryDependency namedDependency; + + @Test + public void givenInjectQualifier_WhenOnField_ThenDefaultFileValid() { + assertNotNull(defaultDependency); + assertEquals("Arbitrary Dependency", defaultDependency.toString()); + } + + @Test + public void givenInjectQualifier_WhenOnField_ThenNamedFileValid() { + assertNotNull(defaultDependency); + assertEquals("Another Arbitrary Dependency", namedDependency.toString()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/FieldResourceInjectionIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/FieldResourceInjectionIntegrationTest.java new file mode 100644 index 0000000000..938d557939 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/FieldResourceInjectionIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.wiring.configuration.resource; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceNameType; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceNameType.class) +public class FieldResourceInjectionIntegrationTest { + + @Resource(name = "namedFile") + private File defaultFile; + + @Test + public void givenResourceAnnotation_WhenOnField_ThenDependencyValid() { + assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodByQualifierResourceIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodByQualifierResourceIntegrationTest.java new file mode 100644 index 0000000000..f49bf70aba --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodByQualifierResourceIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.wiring.configuration.resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceQualifier; + +import javax.annotation.Resource; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceQualifier.class) +public class MethodByQualifierResourceIntegrationTest { + + private File arbDependency; + private File anotherArbDependency; + + @Test + public void givenResourceQualifier_WhenSetter_ThenValidDependencies() { + assertNotNull(arbDependency); + assertEquals("namedFile.txt", arbDependency.getName()); + assertNotNull(anotherArbDependency); + assertEquals("defaultFile.txt", anotherArbDependency.getName()); + } + + @Resource + @Qualifier("namedFile") + public void setArbDependency(File arbDependency) { + this.arbDependency = arbDependency; + } + + @Resource + @Qualifier("defaultFile") + public void setAnotherArbDependency(File anotherArbDependency) { + this.anotherArbDependency = anotherArbDependency; + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodByTypeResourceIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodByTypeResourceIntegrationTest.java new file mode 100644 index 0000000000..aecd02a1d5 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodByTypeResourceIntegrationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.wiring.configuration.resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceNameType; + +import javax.annotation.Resource; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceNameType.class) +public class MethodByTypeResourceIntegrationTest { + + private File defaultFile; + + @Resource + protected void setDefaultFile(File defaultFile) { + this.defaultFile = defaultFile; + } + + @Test + public void givenResourceAnnotation_WhenSetter_ThenValidDependency() { + assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodResourceInjectionIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodResourceInjectionIntegrationTest.java new file mode 100644 index 0000000000..4ef9368c28 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/MethodResourceInjectionIntegrationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.wiring.configuration.resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceNameType; + +import javax.annotation.Resource; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceNameType.class) +public class MethodResourceInjectionIntegrationTest { + + private File defaultFile; + + @Resource(name = "namedFile") + protected void setDefaultFile(File defaultFile) { + this.defaultFile = defaultFile; + } + + @Test + public void givenResourceAnnotation_WhenSetter_ThenDependencyValid() { + assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/NamedResourceIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/NamedResourceIntegrationTest.java new file mode 100644 index 0000000000..4339194f63 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/NamedResourceIntegrationTest.java @@ -0,0 +1,30 @@ +package com.baeldung.wiring.configuration.resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceNameType; + +import javax.annotation.Resource; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceNameType.class) +public class NamedResourceIntegrationTest { + + @Resource(name = "namedFile") + private File testFile; + + @Test + public void givenResourceAnnotation_WhenOnField_THEN_DEPENDENCY_Found() { + assertNotNull(testFile); + assertEquals("namedFile.txt", testFile.getName()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/QualifierResourceInjectionIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/QualifierResourceInjectionIntegrationTest.java new file mode 100644 index 0000000000..cc8c669757 --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/QualifierResourceInjectionIntegrationTest.java @@ -0,0 +1,43 @@ +package com.baeldung.wiring.configuration.resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceQualifier; + +import javax.annotation.Resource; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceQualifier.class) +public class QualifierResourceInjectionIntegrationTest { + + @Resource + @Qualifier("defaultFile") + private File dependency1; + + @Resource + @Qualifier("namedFile") + private File dependency2; + + @Test + public void givenResourceAnnotation_WhenField_ThenDependency1Valid() { + assertNotNull(dependency1); + assertEquals("defaultFile.txt", dependency1.getName()); + } + + @Test + public void givenResourceQualifier_WhenField_ThenDependency2Valid() { + assertNotNull(dependency2); + assertEquals("namedFile.txt", dependency2.getName()); + } +} diff --git a/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/SetterResourceInjectionIntegrationTest.java b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/SetterResourceInjectionIntegrationTest.java new file mode 100644 index 0000000000..90c8677bff --- /dev/null +++ b/spring-di-2/src/test/java/com/baeldung/wiring/configuration/resource/SetterResourceInjectionIntegrationTest.java @@ -0,0 +1,34 @@ +package com.baeldung.wiring.configuration.resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.wiring.configuration.ApplicationContextTestResourceNameType; + +import javax.annotation.Resource; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, + classes = ApplicationContextTestResourceNameType.class) +public class SetterResourceInjectionIntegrationTest { + + private File defaultFile; + + @Resource + protected void setDefaultFile(File defaultFile) { + this.defaultFile = defaultFile; + } + + @Test + public void givenResourceAnnotation_WhenOnSetter_THEN_MUST_INJECT_Dependency() { + assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); + } +}