diff --git a/spring-di/README.md b/spring-di/README.md index 7571b12916..d470768f16 100644 --- a/spring-di/README.md +++ b/spring-di/README.md @@ -13,3 +13,5 @@ This module contains articles about dependency injection with Spring - [Controlling Bean Creation Order with @DependsOn Annotation](https://www.baeldung.com/spring-depends-on) - [Unsatisfied Dependency in Spring](https://www.baeldung.com/spring-unsatisfied-dependency) - [Circular Dependencies in Spring](https://www.baeldung.com/circular-dependencies-in-spring) +- [XML-Based Injection in Spring](https://www.baeldung.com/spring-xml-injection) +- More articles: [[next -->]](/spring-di-2) diff --git a/spring-di/src/main/java/com/baeldung/dependency/AnotherArbitraryDependency.java b/spring-di/src/main/java/com/baeldung/dependency/AnotherArbitraryDependency.java deleted file mode 100644 index 0e19523b7e..0000000000 --- a/spring-di/src/main/java/com/baeldung/dependency/AnotherArbitraryDependency.java +++ /dev/null @@ -1,13 +0,0 @@ -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/src/main/java/com/baeldung/dependency/ArbitraryDependency.java b/spring-di/src/main/java/com/baeldung/dependency/ArbitraryDependency.java deleted file mode 100644 index 3c90492d2c..0000000000 --- a/spring-di/src/main/java/com/baeldung/dependency/ArbitraryDependency.java +++ /dev/null @@ -1,13 +0,0 @@ -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/src/main/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java b/spring-di/src/main/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java deleted file mode 100644 index a88abd0924..0000000000 --- a/spring-di/src/main/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java +++ /dev/null @@ -1,13 +0,0 @@ -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/src/test/java/com/baeldung/autowired/FieldAutowiredIntegrationTest.java b/spring-di/src/test/java/com/baeldung/autowired/FieldAutowiredIntegrationTest.java deleted file mode 100644 index a78799f1d9..0000000000 --- a/spring-di/src/test/java/com/baeldung/autowired/FieldAutowiredIntegrationTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.autowired; - -import com.baeldung.configuration.ApplicationContextTestAutowiredType; -import com.baeldung.dependency.ArbitraryDependency; -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 = 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/src/test/java/com/baeldung/autowired/FieldAutowiredNameIntegrationTest.java b/spring-di/src/test/java/com/baeldung/autowired/FieldAutowiredNameIntegrationTest.java deleted file mode 100644 index 8f09e73c33..0000000000 --- a/spring-di/src/test/java/com/baeldung/autowired/FieldAutowiredNameIntegrationTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.autowired; - -import com.baeldung.configuration.ApplicationContextTestAutowiredName; -import com.baeldung.dependency.ArbitraryDependency; -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/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredIntegrationTest.java b/spring-di/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredIntegrationTest.java deleted file mode 100644 index 01317aef6f..0000000000 --- a/spring-di/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredIntegrationTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.autowired; - -import com.baeldung.configuration.ApplicationContextTestAutowiredQualifier; -import com.baeldung.dependency.ArbitraryDependency; -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 static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@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/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredName.java b/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredName.java deleted file mode 100644 index 48c4495465..0000000000 --- a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredName.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.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/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredQualifier.java b/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredQualifier.java deleted file mode 100644 index ef6690ab4b..0000000000 --- a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredQualifier.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.configuration; - -import com.baeldung.dependency.AnotherArbitraryDependency; -import com.baeldung.dependency.ArbitraryDependency; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@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/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredType.java b/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredType.java deleted file mode 100644 index 240bc466b7..0000000000 --- a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredType.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.configuration; - -import com.baeldung.dependency.ArbitraryDependency; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class ApplicationContextTestAutowiredType { - - @Bean - public ArbitraryDependency autowiredFieldDependency() { - ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency(); - return autowiredFieldDependency; - } -} diff --git a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectName.java b/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectName.java deleted file mode 100644 index 851aa0b8ee..0000000000 --- a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectName.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.configuration; - -import com.baeldung.dependency.ArbitraryDependency; -import com.baeldung.dependency.YetAnotherArbitraryDependency; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class ApplicationContextTestInjectName { - - @Bean - public ArbitraryDependency yetAnotherFieldInjectDependency() { - ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency(); - return yetAnotherFieldInjectDependency; - } -} diff --git a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectQualifier.java b/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectQualifier.java deleted file mode 100644 index 59af5a91bb..0000000000 --- a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectQualifier.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.configuration; - -import com.baeldung.dependency.AnotherArbitraryDependency; -import com.baeldung.dependency.ArbitraryDependency; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@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/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectType.java b/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectType.java deleted file mode 100644 index 1e1f01f269..0000000000 --- a/spring-di/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectType.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.configuration; - -import com.baeldung.dependency.ArbitraryDependency; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class ApplicationContextTestInjectType { - - @Bean - public ArbitraryDependency injectDependency() { - ArbitraryDependency injectDependency = new ArbitraryDependency(); - return injectDependency; - } -} diff --git a/spring-di/src/test/java/com/baeldung/inject/FieldByNameInjectIntegrationTest.java b/spring-di/src/test/java/com/baeldung/inject/FieldByNameInjectIntegrationTest.java deleted file mode 100644 index f5897febab..0000000000 --- a/spring-di/src/test/java/com/baeldung/inject/FieldByNameInjectIntegrationTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.inject; - -import com.baeldung.configuration.ApplicationContextTestInjectName; -import com.baeldung.dependency.ArbitraryDependency; -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 javax.inject.Inject; -import javax.inject.Named; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@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/src/test/java/com/baeldung/inject/FieldInjectIntegrationTest.java b/spring-di/src/test/java/com/baeldung/inject/FieldInjectIntegrationTest.java deleted file mode 100644 index 45b7c8015c..0000000000 --- a/spring-di/src/test/java/com/baeldung/inject/FieldInjectIntegrationTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.inject; - -import com.baeldung.configuration.ApplicationContextTestInjectType; -import com.baeldung.dependency.ArbitraryDependency; -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 javax.inject.Inject; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@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/src/test/java/com/baeldung/inject/FieldQualifierInjectIntegrationTest.java b/spring-di/src/test/java/com/baeldung/inject/FieldQualifierInjectIntegrationTest.java deleted file mode 100644 index 0fd6a0e4c1..0000000000 --- a/spring-di/src/test/java/com/baeldung/inject/FieldQualifierInjectIntegrationTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.inject; - -import com.baeldung.configuration.ApplicationContextTestInjectQualifier; -import com.baeldung.dependency.ArbitraryDependency; -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 javax.inject.Inject; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@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()); - } -}