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/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 new file mode 100644 index 000000000000..752eb8893f9a --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/methodinjections/SchoolNotification.java @@ -0,0 +1,45 @@ +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; + +@Component("schoolNotification") +@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class SchoolNotification { + @Autowired + Grader grader; + + private String name; + private Collection marks; + + public SchoolNotification(String name) { + this.name = name; + this.marks = new ArrayList(); + } + + public String addMark(Integer mark) { + this.marks.add(mark); + return this.grader.grade(this.marks); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Collection getMarks() { + return 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 new file mode 100644 index 000000000000..ceeebc3d0d5b --- /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(String name) { + // 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/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 new file mode 100644 index 000000000000..4002fa63bd24 --- /dev/null +++ b/spring-core/src/main/resources/beans.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ 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..8c04ef472e28 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/methodinjections/StudentTest.java @@ -0,0 +1,31 @@ +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 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("Alex"), student2.getNotification("Bethany")); + context.close(); + } + + @Test + public void whenAbstractGetterMethodInjects_thenNewInstanceReturned() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + StudentServices services = context.getBean("studentServices", StudentServices.class); + + Assert.assertEquals("PASS", services.appendMark("Alex", 76)); + Assert.assertEquals("FAIL", services.appendMark("Bethany", 44)); + Assert.assertEquals("PASS", services.appendMark("Claire", 96)); + context.close(); + } +}