From 3a4cddb740638446496efc906e2d71d2b0d74c4a Mon Sep 17 00:00:00 2001 From: Dassi Orleando Date: Sat, 19 Aug 2017 20:41:55 +0100 Subject: [PATCH] Different types of bean injection in Spring --- pom.xml | 1 + spring-types-bean-injection/README.md | 3 + spring-types-bean-injection/pom.xml | 81 +++++++++++++++++++ .../java/com/baeldung/config/AppConfig.java | 25 ++++++ .../com/baeldung/service/MegaTestService.java | 22 +++++ .../baeldung/service/SuperTestService.java | 22 +++++ .../com/baeldung/service/TestService.java | 14 ++++ .../MegaTestServiceIntegrationTest.java | 32 ++++++++ .../SuperTestServiceIntegrationTest.java | 32 ++++++++ .../service/TestServiceIntegrationTest.java | 32 ++++++++ 10 files changed, 264 insertions(+) create mode 100644 spring-types-bean-injection/README.md create mode 100644 spring-types-bean-injection/pom.xml create mode 100644 spring-types-bean-injection/src/main/java/com/baeldung/config/AppConfig.java create mode 100644 spring-types-bean-injection/src/main/java/com/baeldung/service/MegaTestService.java create mode 100644 spring-types-bean-injection/src/main/java/com/baeldung/service/SuperTestService.java create mode 100644 spring-types-bean-injection/src/main/java/com/baeldung/service/TestService.java create mode 100644 spring-types-bean-injection/src/test/java/com/baeldung/service/MegaTestServiceIntegrationTest.java create mode 100644 spring-types-bean-injection/src/test/java/com/baeldung/service/SuperTestServiceIntegrationTest.java create mode 100644 spring-types-bean-injection/src/test/java/com/baeldung/service/TestServiceIntegrationTest.java diff --git a/pom.xml b/pom.xml index b76c9b8ddd..eef32f884e 100644 --- a/pom.xml +++ b/pom.xml @@ -170,6 +170,7 @@ spring-social-login spring-spel spring-thymeleaf + spring-types-bean-injection spring-userservice spring-zuul spring-reactor diff --git a/spring-types-bean-injection/README.md b/spring-types-bean-injection/README.md new file mode 100644 index 0000000000..45f8216849 --- /dev/null +++ b/spring-types-bean-injection/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring) \ No newline at end of file diff --git a/spring-types-bean-injection/pom.xml b/spring-types-bean-injection/pom.xml new file mode 100644 index 0000000000..75fee65d80 --- /dev/null +++ b/spring-types-bean-injection/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.baeldung + spring-types-bean-injection + 0.0.1-SNAPSHOT + jar + + spring-types-bean-injection + Types of bean injection in spring + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-test + ${spring.version} + test + + + junit + junit + ${junit.version} + test + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + 1.8 + 4.3.10.RELEASE + 4.12 + + + diff --git a/spring-types-bean-injection/src/main/java/com/baeldung/config/AppConfig.java b/spring-types-bean-injection/src/main/java/com/baeldung/config/AppConfig.java new file mode 100644 index 0000000000..45be32fa06 --- /dev/null +++ b/spring-types-bean-injection/src/main/java/com/baeldung/config/AppConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.config; + +import com.baeldung.service.MegaTestService; +import com.baeldung.service.SuperTestService; +import com.baeldung.service.TestService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class AppConfig { + @Bean + public TestService TestService() { + return new TestService(); + } + + @Bean + public SuperTestService SuperTestService() { + return new SuperTestService(new TestService()); + } + + @Bean + public MegaTestService MegaTestService() { + return new MegaTestService(); + } +} diff --git a/spring-types-bean-injection/src/main/java/com/baeldung/service/MegaTestService.java b/spring-types-bean-injection/src/main/java/com/baeldung/service/MegaTestService.java new file mode 100644 index 0000000000..45deeb8592 --- /dev/null +++ b/spring-types-bean-injection/src/main/java/com/baeldung/service/MegaTestService.java @@ -0,0 +1,22 @@ +package com.baeldung.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class MegaTestService { + private TestService testService; + + @Autowired + public void setTestService(TestService testService) { + this.testService = testService; + } + + public String getMegaTestOne() { + return "Mega" + testService.getTestOne(); + } + + public String getMegaTestTwo() { + return "Mega" + testService.getTestTwo(); + } +} diff --git a/spring-types-bean-injection/src/main/java/com/baeldung/service/SuperTestService.java b/spring-types-bean-injection/src/main/java/com/baeldung/service/SuperTestService.java new file mode 100644 index 0000000000..edf1f2b1c5 --- /dev/null +++ b/spring-types-bean-injection/src/main/java/com/baeldung/service/SuperTestService.java @@ -0,0 +1,22 @@ +package com.baeldung.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class SuperTestService { + private TestService testService; + + @Autowired + public SuperTestService(TestService testService) { + this.testService = testService; + } + + public String getSuperTestOne() { + return "Super" + testService.getTestOne(); + } + + public String getSuperTestTwo() { + return "Super" + testService.getTestTwo(); + } +} diff --git a/spring-types-bean-injection/src/main/java/com/baeldung/service/TestService.java b/spring-types-bean-injection/src/main/java/com/baeldung/service/TestService.java new file mode 100644 index 0000000000..3e7c327dbd --- /dev/null +++ b/spring-types-bean-injection/src/main/java/com/baeldung/service/TestService.java @@ -0,0 +1,14 @@ +package com.baeldung.service; + +import org.springframework.stereotype.Service; + +@Service +public class TestService { + public String getTestOne() { + return "TestOne"; + } + + public String getTestTwo() { + return "TestTwo"; + } +} diff --git a/spring-types-bean-injection/src/test/java/com/baeldung/service/MegaTestServiceIntegrationTest.java b/spring-types-bean-injection/src/test/java/com/baeldung/service/MegaTestServiceIntegrationTest.java new file mode 100644 index 0000000000..ced897c70c --- /dev/null +++ b/spring-types-bean-injection/src/test/java/com/baeldung/service/MegaTestServiceIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.service; + +import com.baeldung.config.AppConfig; +import org.junit.Assert; +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.SpringRunner; + +import static org.hamcrest.CoreMatchers.is; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = {AppConfig.class}) +public class MegaTestServiceIntegrationTest { + @Autowired + MegaTestService megaTestService; + + @Test + public void whenCallingGetMegaTestOne_thenWeGetMegaTestOneString() { + String resultOne = megaTestService.getMegaTestOne(); + + Assert.assertThat(resultOne, is("MegaTestOne")); + } + + @Test + public void whenCallingGetMegaTestTwo_thenWeGetMegaTestTwoString() { + String resultTwo = megaTestService.getMegaTestTwo(); + + Assert.assertThat(resultTwo, is("MegaTestTwo")); + } +} diff --git a/spring-types-bean-injection/src/test/java/com/baeldung/service/SuperTestServiceIntegrationTest.java b/spring-types-bean-injection/src/test/java/com/baeldung/service/SuperTestServiceIntegrationTest.java new file mode 100644 index 0000000000..a037d70c40 --- /dev/null +++ b/spring-types-bean-injection/src/test/java/com/baeldung/service/SuperTestServiceIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.service; + +import com.baeldung.config.AppConfig; +import org.junit.Assert; +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.SpringRunner; + +import static org.hamcrest.CoreMatchers.is; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = {AppConfig.class}) +public class SuperTestServiceIntegrationTest { + @Autowired + SuperTestService superTestService; + + @Test + public void whenCallingGetSuperTestOne_thenWeGetSuperTestOneString() { + String resultOne = superTestService.getSuperTestOne(); + + Assert.assertThat(resultOne, is("SuperTestOne")); + } + + @Test + public void whenCallingGetSuperTestTwo_thenWeGetSuperTestTwoString() { + String resultTwo = superTestService.getSuperTestTwo(); + + Assert.assertThat(resultTwo, is("SuperTestTwo")); + } +} diff --git a/spring-types-bean-injection/src/test/java/com/baeldung/service/TestServiceIntegrationTest.java b/spring-types-bean-injection/src/test/java/com/baeldung/service/TestServiceIntegrationTest.java new file mode 100644 index 0000000000..2059e87e04 --- /dev/null +++ b/spring-types-bean-injection/src/test/java/com/baeldung/service/TestServiceIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.service; + +import com.baeldung.config.AppConfig; +import org.junit.Assert; +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.SpringRunner; + +import static org.hamcrest.CoreMatchers.is; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = { AppConfig.class }) +public class TestServiceIntegrationTest { + @Autowired + TestService testService; + + @Test + public void whenCallingGetTestOne_thenWeGetTestOneString() { + String resultOne = testService.getTestOne(); + + Assert.assertThat(resultOne, is("TestOne")); + } + + @Test + public void whenCallingGetTestTwo_thenWeGetTestTwoString() { + String resultTwo = testService.getTestTwo(); + + Assert.assertThat(resultTwo, is("TestTwo")); + } +} \ No newline at end of file