From 54bd8a5cddf6d2825e1ee874b6a6fe75c3bfe9da Mon Sep 17 00:00:00 2001 From: Syed Munawwer Ali Date: Sat, 24 Feb 2018 21:51:16 +0530 Subject: [PATCH] evaluation article, different types of bean injection. (#3511) * Different types of bean injection in spring, initial write-up * Evaluation article code example, different types of bean injection. --- .../dependencyinjectiontypes/Student.java | 21 +++++ .../dependencyinjectiontypes/Student2.java | 20 ++++ .../TeacherFinder.java | 16 ++++ .../java/com/baeldung/setterdi/Config.java | 91 ++++++++++++------- .../DependencyInjectionTest.java | 30 +++++- 5 files changed, 142 insertions(+), 36 deletions(-) create mode 100644 spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student.java create mode 100644 spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student2.java create mode 100644 spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/TeacherFinder.java diff --git a/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student.java b/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student.java new file mode 100644 index 0000000000..9bd218c332 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student.java @@ -0,0 +1,21 @@ +package com.baeldung.dependencyinjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Student { + + private TeacherFinder teacherFinder; + + @Autowired + public Student(TeacherFinder teacherFinder) { + this.teacherFinder = teacherFinder; + } + + public String getTeacher() { + return teacherFinder.getTeacherFinder(); + } + // business logic that actually uses the injected teacherFinders is omitted... +} + diff --git a/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student2.java b/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student2.java new file mode 100644 index 0000000000..e6724d82ec --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/Student2.java @@ -0,0 +1,20 @@ +package com.baeldung.dependencyinjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Student2 { + + private TeacherFinder teacherFinder; + + @Autowired + public void setTeacherFinder(TeacherFinder teacherFinder) { + this.teacherFinder = teacherFinder; + } + + public String getTeacher() { + return teacherFinder.getTeacherFinder(); + } + +} \ No newline at end of file diff --git a/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/TeacherFinder.java b/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/TeacherFinder.java new file mode 100644 index 0000000000..b349dc19d0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/dependencyinjectiontypes/TeacherFinder.java @@ -0,0 +1,16 @@ +package com.baeldung.dependencyinjectiontypes; +public class TeacherFinder { + + private String teacherFinder; + + public String getTeacherFinder() { + return teacherFinder; + } + + public void setTeacherFinder(String teacherFinder) { + this.teacherFinder = teacherFinder; + } + + +} + diff --git a/spring-core/src/main/java/com/baeldung/setterdi/Config.java b/spring-core/src/main/java/com/baeldung/setterdi/Config.java index 68c1ae12a2..d61510971c 100644 --- a/spring-core/src/main/java/com/baeldung/setterdi/Config.java +++ b/spring-core/src/main/java/com/baeldung/setterdi/Config.java @@ -1,35 +1,58 @@ -package com.baeldung.setterdi; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -import com.baeldung.setterdi.domain.Engine; -import com.baeldung.setterdi.domain.Trailer; -import com.baeldung.setterdi.domain.Transmission; - -@Configuration -@ComponentScan("com.baeldung.setterdi") -public class Config { - - @Bean - public Engine engine() { - Engine engine = new Engine(); - engine.setType("v8"); - engine.setVolume(5); - return engine; - } - - @Bean - public Transmission transmission() { - Transmission transmission = new Transmission(); - transmission.setType("sliding"); - return transmission; - } - - @Bean - public Trailer trailer() { - Trailer trailer = new Trailer(); - return trailer; - } +package com.baeldung.setterdi; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependencyinjectiontypes.Student; +import com.baeldung.dependencyinjectiontypes.Student2; +import com.baeldung.dependencyinjectiontypes.TeacherFinder; +import com.baeldung.setterdi.domain.Engine; +import com.baeldung.setterdi.domain.Trailer; +import com.baeldung.setterdi.domain.Transmission; + +@Configuration +@ComponentScan("com.baeldung.setterdi") +public class Config { + + @Bean + public Engine engine() { + Engine engine = new Engine(); + engine.setType("v8"); + engine.setVolume(5); + return engine; + } + + @Bean + public Transmission transmission() { + Transmission transmission = new Transmission(); + transmission.setType("sliding"); + return transmission; + } + + @Bean + public Trailer trailer() { + Trailer trailer = new Trailer(); + return trailer; + } + + @Bean + public TeacherFinder teacherFinder(){ + TeacherFinder teacherFinder =new TeacherFinder(); + teacherFinder.setTeacherFinder("author"); + return teacherFinder; + } + + @Bean + public Student student() { + return new Student(teacherFinder()); + } + + @Bean + public Student2 student2() { + Student2 student2 = new Student2(); + student2.setTeacherFinder(teacherFinder()); + return student2; + } + } \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/dependencyinjectiontypes/DependencyInjectionTest.java b/spring-core/src/test/java/com/baeldung/dependencyinjectiontypes/DependencyInjectionTest.java index 57c1927e58..7ec477d2e2 100644 --- a/spring-core/src/test/java/com/baeldung/dependencyinjectiontypes/DependencyInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/dependencyinjectiontypes/DependencyInjectionTest.java @@ -2,13 +2,25 @@ package com.baeldung.dependencyinjectiontypes; import static org.junit.Assert.assertTrue; +import org.apache.log4j.Logger; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes=com.baeldung.setterdi.Config.class, loader=AnnotationConfigContextLoader.class) public class DependencyInjectionTest { + + @Autowired + private ApplicationContext appContext; + + Logger logger = Logger.getLogger(this.getClass()); - @Test + /* @Test public void givenAutowiredAnnotation_WhenSetOnSetter_ThenDependencyValid() { ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml"); @@ -30,6 +42,20 @@ public class DependencyInjectionTest { String formattedArticle = article.format(originalText); assertTrue(originalText.toUpperCase().equals(formattedArticle)); + }*/ + + @Test + public void givenAutowiredAnnotation_OnSetter_ThenDependencyValid() { + Student student = (Student) appContext.getBean("student"); + String teacherFound = student.getTeacher(); + assertTrue(teacherFound.equals("author")); + } + + @Test + public void givenAutowiredAnnotation_OnConstructor_ThenDependencyValid() { + Student2 student2 = (Student2) appContext.getBean("student2"); + String teacherFound = student2.getTeacher(); + assertTrue(teacherFound.equals("author")); } }