From 38b009ed68b9725433ce05c28270c1ffbe4cef05 Mon Sep 17 00:00:00 2001 From: gangadkho Date: Wed, 28 Mar 2018 16:20:35 +0800 Subject: [PATCH 1/4] Updated --- .../baeldung/methodinjections/AppConfig.java | 10 ++++++ .../methodinjections/SchoolNotification.java | 20 ++++++++++++ .../baeldung/methodinjections/Student.java | 27 ++++++++++++++++ .../methodinjections/StudentService.java | 12 +++++++ spring-core/src/main/resources/beans.xml | 17 ++++++++++ .../methodinjections/StudentTest.java | 32 +++++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/AppConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java create mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/Student.java create mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java create mode 100644 spring-core/src/main/resources/beans.xml create mode 100644 spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/AppConfig.java b/spring-core/src/main/java/com/baeldung/methodinjections/AppConfig.java new file mode 100644 index 000000000000..7c1e209383fa --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/AppConfig.java @@ -0,0 +1,10 @@ +package com.baeldung.methodinjections; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.methodinjections") + +public class AppConfig { +} diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java new file mode 100644 index 000000000000..e38e3c6792a1 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java @@ -0,0 +1,20 @@ +package com.baeldung.methodinjections; + +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component("schoolNotification") +@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class SchoolNotification { + + private String message = "SCHOOL_OPEN"; + + public String getMessage() { + return this.message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java new file mode 100644 index 000000000000..b25bf31390dd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java @@ -0,0 +1,27 @@ +package com.baeldung.methodinjections; + +import org.springframework.beans.factory.annotation.Lookup; +import org.springframework.stereotype.Component; + +@Component("studentBean") +public class Student { + + private String id; + + /** + * Injects a prototype bean SchoolNotification into Singleton student + */ + @Lookup + public SchoolNotification getNotification() { + // spring overrides and returns a SchoolNotification instance + return null; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +} diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java new file mode 100644 index 000000000000..e62bab035a7c --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java @@ -0,0 +1,12 @@ +package com.baeldung.methodinjections; + +import org.springframework.beans.factory.annotation.Lookup; +import org.springframework.stereotype.Component; + +@Component("studentService") +public abstract class StudentService { + + @Lookup(value = "schoolNotification") + public abstract SchoolNotification getSchoolNotification(); + +} diff --git a/spring-core/src/main/resources/beans.xml b/spring-core/src/main/resources/beans.xml new file mode 100644 index 000000000000..81e8b90fa87d --- /dev/null +++ b/spring-core/src/main/resources/beans.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java new file mode 100644 index 000000000000..b028b07c9bda --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java @@ -0,0 +1,32 @@ +package com.baeldung.methodinjections; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class StudentTest { + + @Test + public void testInjectPrototypeIntoSingleton() { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); + Student student1 = context.getBean("studentBean", Student.class); + Student student2 = context.getBean("studentBean", Student.class); + + Assert.assertEquals(student1, student2); + Assert.assertNotEquals(student1.getNotification(), student2.getNotification()); + context.close(); + } + + @Test + public void testLookupWithGetterMethod() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + StudentService service = context.getBean("studentService", StudentService.class); + SchoolNotification notification = service.getSchoolNotification(); + + Assert.assertNotNull(notification); + Assert.assertEquals("SCHOOL_OPEN", notification.getMessage()); + context.close(); + } +} From d518a476a5d55e9cb7721f014cb077b6f2845145 Mon Sep 17 00:00:00 2001 From: Gangadharan Khoteeswarun Date: Sat, 31 Mar 2018 11:34:12 +0800 Subject: [PATCH 2/4] Changed Test Names --- .../methodinjections/SchoolNotification.java | 7 +++++-- .../java/com/baeldung/methodinjections/Student.java | 2 +- .../baeldung/methodinjections/StudentService.java | 4 ++-- spring-core/src/main/resources/beans.xml | 3 ++- .../com/baeldung/methodinjections/StudentTest.java | 13 ++++++------- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java index e38e3c6792a1..6414fa1dd455 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java @@ -7,8 +7,11 @@ @Component("schoolNotification") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class SchoolNotification { - - private String message = "SCHOOL_OPEN"; + private String message; + + public SchoolNotification(String message) { + this.message = message; + } public String getMessage() { return this.message; diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java index b25bf31390dd..844630a1ccdd 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java @@ -12,7 +12,7 @@ public class Student { * Injects a prototype bean SchoolNotification into Singleton student */ @Lookup - public SchoolNotification getNotification() { + public SchoolNotification getNotification(String message) { // spring overrides and returns a SchoolNotification instance return null; } diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java index e62bab035a7c..30a791c33ecf 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java @@ -6,7 +6,7 @@ @Component("studentService") public abstract class StudentService { - @Lookup(value = "schoolNotification") - public abstract SchoolNotification getSchoolNotification(); + @Lookup + public abstract SchoolNotification getSchoolNotification(String message); } diff --git a/spring-core/src/main/resources/beans.xml b/spring-core/src/main/resources/beans.xml index 81e8b90fa87d..ab128a5d0350 100644 --- a/spring-core/src/main/resources/beans.xml +++ b/spring-core/src/main/resources/beans.xml @@ -12,6 +12,7 @@ - + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java index b028b07c9bda..0975f146869d 100644 --- a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java +++ b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java @@ -8,25 +8,24 @@ public class StudentTest { @Test - public void testInjectPrototypeIntoSingleton() { - + public void whenPrototypeInjectedToSingleton_thenNewPrototypeInstanceEverytime() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Student student1 = context.getBean("studentBean", Student.class); Student student2 = context.getBean("studentBean", Student.class); Assert.assertEquals(student1, student2); - Assert.assertNotEquals(student1.getNotification(), student2.getNotification()); + Assert.assertNotEquals(student1.getNotification("Message1"), student2.getNotification("Message2")); context.close(); } @Test - public void testLookupWithGetterMethod() { + public void whenAbstractGetterMethodInjects_thenNewPrototypeBean() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); StudentService service = context.getBean("studentService", StudentService.class); - SchoolNotification notification = service.getSchoolNotification(); - + SchoolNotification notification = service.getSchoolNotification("Test"); + Assert.assertNotNull(notification); - Assert.assertEquals("SCHOOL_OPEN", notification.getMessage()); + Assert.assertEquals("Test", notification.getMessage()); context.close(); } } From a281de11b39c8a84431921e0700c0c45ecea2bad Mon Sep 17 00:00:00 2001 From: gangadkho Date: Tue, 3 Apr 2018 16:32:23 +0800 Subject: [PATCH 3/4] Updated code for method injection section 3.2 --- .../methodinjections/SchoolNotification.java | 35 ++++++++++++++----- .../baeldung/methodinjections/Student.java | 2 +- .../methodinjections/StudentService.java | 5 ++- spring-core/src/main/resources/beans.xml | 9 ++--- .../methodinjections/StudentTest.java | 16 +++++---- 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java index 6414fa1dd455..b48523e18455 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java @@ -7,17 +7,36 @@ @Component("schoolNotification") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class SchoolNotification { - private String message; - - public SchoolNotification(String message) { - this.message = message; + private String name; + private int marks; + + public SchoolNotification(String name, int marks) { + this.setName(name); + this.setMarks(marks); + } + + public String checkResult() { + if (marks >= 70) { + return this.name + ":FIRST_CLASS"; + } else if (marks < 70 && marks > 45) { + return this.name + ":SECOND_CLASS"; + } + return this.name + ":FAIL"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; } - public String getMessage() { - return this.message; + public int getMarks() { + return marks; } - public void setMessage(String message) { - this.message = message; + public void setMarks(int marks) { + this.marks = marks; } } diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java index 844630a1ccdd..2be6234c1c20 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java @@ -12,7 +12,7 @@ public class Student { * Injects a prototype bean SchoolNotification into Singleton student */ @Lookup - public SchoolNotification getNotification(String message) { + public SchoolNotification getNotification(String name, int marks) { // spring overrides and returns a SchoolNotification instance return null; } diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java index 30a791c33ecf..848d2d2fe484 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java @@ -7,6 +7,9 @@ public abstract class StudentService { @Lookup - public abstract SchoolNotification getSchoolNotification(String message); + public abstract SchoolNotification getSchoolNotification(String name, int marks); + public String checkResult(String name, int marks) { + return getSchoolNotification(name, marks).checkResult(); + } } diff --git a/spring-core/src/main/resources/beans.xml b/spring-core/src/main/resources/beans.xml index ab128a5d0350..0dc48ddf0b62 100644 --- a/spring-core/src/main/resources/beans.xml +++ b/spring-core/src/main/resources/beans.xml @@ -7,12 +7,9 @@ + + - - - - - - + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java index 0975f146869d..4943ec63ceb6 100644 --- a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java +++ b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java @@ -8,24 +8,26 @@ public class StudentTest { @Test - public void whenPrototypeInjectedToSingleton_thenNewPrototypeInstanceEverytime() { + public void whenLookupMethodCalled_thenNewInstanceReturned() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Student student1 = context.getBean("studentBean", Student.class); Student student2 = context.getBean("studentBean", Student.class); Assert.assertEquals(student1, student2); - Assert.assertNotEquals(student1.getNotification("Message1"), student2.getNotification("Message2")); + Assert.assertNotEquals(student1.getNotification("Sam", 76), student2.getNotification("Paul", 44)); context.close(); } @Test - public void whenAbstractGetterMethodInjects_thenNewPrototypeBean() { + public void whenAbstractGetterMethodInjects_thenNewInstanceReturned() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); StudentService service = context.getBean("studentService", StudentService.class); - SchoolNotification notification = service.getSchoolNotification("Test"); - - Assert.assertNotNull(notification); - Assert.assertEquals("Test", notification.getMessage()); + String name = "Alex"; + int marks = 96; + String expectedResult = name + ":FIRST_CLASS"; + + String actualResult = service.checkResult(name, marks); + Assert.assertEquals(expectedResult, actualResult); context.close(); } } From a638ed65f2be8b36b784441a5a09faab623fd7e4 Mon Sep 17 00:00:00 2001 From: gangadkho Date: Thu, 5 Apr 2018 14:55:51 +0800 Subject: [PATCH 4/4] Added Grader bean and changes for it --- .../com/baeldung/methodinjections/Grader.java | 19 ++++++++++++ .../methodinjections/SchoolNotification.java | 29 ++++++++++--------- .../baeldung/methodinjections/Student.java | 2 +- .../methodinjections/StudentService.java | 15 ---------- .../methodinjections/StudentServices.java | 21 ++++++++++++++ spring-core/src/main/resources/beans.xml | 4 +-- .../methodinjections/StudentTest.java | 12 ++++---- 7 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/Grader.java delete mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java create mode 100644 spring-core/src/main/java/com/baeldung/methodinjections/StudentServices.java diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/Grader.java b/spring-core/src/main/java/com/baeldung/methodinjections/Grader.java new file mode 100644 index 000000000000..10dc77484c19 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/Grader.java @@ -0,0 +1,19 @@ +package com.baeldung.methodinjections; + +import java.util.Collection; + +import org.springframework.stereotype.Component; + +@Component +public class Grader { + + public String grade(Collection marks) { + + boolean result = marks.stream() + .anyMatch(mark -> mark < 45); + if (result) { + return "FAIL"; + } + return "PASS"; + } +} diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java index b48523e18455..752eb8893f9a 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java @@ -1,5 +1,9 @@ package com.baeldung.methodinjections; +import java.util.ArrayList; +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -7,21 +11,20 @@ @Component("schoolNotification") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class SchoolNotification { + @Autowired + Grader grader; + private String name; - private int marks; + private Collection marks; - public SchoolNotification(String name, int marks) { - this.setName(name); - this.setMarks(marks); + public SchoolNotification(String name) { + this.name = name; + this.marks = new ArrayList(); } - public String checkResult() { - if (marks >= 70) { - return this.name + ":FIRST_CLASS"; - } else if (marks < 70 && marks > 45) { - return this.name + ":SECOND_CLASS"; - } - return this.name + ":FAIL"; + public String addMark(Integer mark) { + this.marks.add(mark); + return this.grader.grade(this.marks); } public String getName() { @@ -32,11 +35,11 @@ public void setName(String name) { this.name = name; } - public int getMarks() { + public Collection getMarks() { return marks; } - public void setMarks(int marks) { + public void setMarks(Collection marks) { this.marks = marks; } } diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java index 2be6234c1c20..ceeebc3d0d5b 100644 --- a/spring-core/src/main/java/com/baeldung/methodinjections/Student.java +++ b/spring-core/src/main/java/com/baeldung/methodinjections/Student.java @@ -12,7 +12,7 @@ public class Student { * Injects a prototype bean SchoolNotification into Singleton student */ @Lookup - public SchoolNotification getNotification(String name, int marks) { + public SchoolNotification getNotification(String name) { // spring overrides and returns a SchoolNotification instance return null; } diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java b/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java deleted file mode 100644 index 848d2d2fe484..000000000000 --- a/spring-core/src/main/java/com/baeldung/methodinjections/StudentService.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.methodinjections; - -import org.springframework.beans.factory.annotation.Lookup; -import org.springframework.stereotype.Component; - -@Component("studentService") -public abstract class StudentService { - - @Lookup - public abstract SchoolNotification getSchoolNotification(String name, int marks); - - public String checkResult(String name, int marks) { - return getSchoolNotification(name, marks).checkResult(); - } -} diff --git a/spring-core/src/main/java/com/baeldung/methodinjections/StudentServices.java b/spring-core/src/main/java/com/baeldung/methodinjections/StudentServices.java new file mode 100644 index 000000000000..20d631b120ca --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/StudentServices.java @@ -0,0 +1,21 @@ +package com.baeldung.methodinjections; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Lookup; +import org.springframework.stereotype.Component; + +@Component("studentService") +public abstract class StudentServices { + + private Map notes = new HashMap<>(); + + @Lookup + protected abstract SchoolNotification getNotification(String name); + + public String appendMark(String name, Integer mark) { + SchoolNotification notification = notes.computeIfAbsent(name, exists -> getNotification(name)); + return notification.addMark(mark); + } +} diff --git a/spring-core/src/main/resources/beans.xml b/spring-core/src/main/resources/beans.xml index 0dc48ddf0b62..4002fa63bd24 100644 --- a/spring-core/src/main/resources/beans.xml +++ b/spring-core/src/main/resources/beans.xml @@ -9,7 +9,7 @@ - - + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java index 4943ec63ceb6..8c04ef472e28 100644 --- a/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java +++ b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java @@ -14,20 +14,18 @@ public void whenLookupMethodCalled_thenNewInstanceReturned() { Student student2 = context.getBean("studentBean", Student.class); Assert.assertEquals(student1, student2); - Assert.assertNotEquals(student1.getNotification("Sam", 76), student2.getNotification("Paul", 44)); + Assert.assertNotEquals(student1.getNotification("Alex"), student2.getNotification("Bethany")); context.close(); } @Test public void whenAbstractGetterMethodInjects_thenNewInstanceReturned() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); - StudentService service = context.getBean("studentService", StudentService.class); - String name = "Alex"; - int marks = 96; - String expectedResult = name + ":FIRST_CLASS"; + StudentServices services = context.getBean("studentServices", StudentServices.class); - String actualResult = service.checkResult(name, marks); - Assert.assertEquals(expectedResult, actualResult); + Assert.assertEquals("PASS", services.appendMark("Alex", 76)); + Assert.assertEquals("FAIL", services.appendMark("Bethany", 44)); + Assert.assertEquals("PASS", services.appendMark("Claire", 96)); context.close(); } }