Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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<Integer> marks) {

boolean result = marks.stream()
.anyMatch(mark -> mark < 45);
if (result) {
return "FAIL";
}
return "PASS";
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> marks;

public SchoolNotification(String name) {
this.name = name;
this.marks = new ArrayList<Integer>();
}

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<Integer> getMarks() {
return marks;
}

public void setMarks(Collection<Integer> marks) {
this.marks = marks;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, SchoolNotification> 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);
}
}
15 changes: 15 additions & 0 deletions spring-core/src/main/resources/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.baeldung.methodinjections" />

<bean id="schoolNotification" class="com.baeldung.methodinjections.SchoolNotification" scope="prototype"/>

<bean id="studentServices" class="com.baeldung.methodinjections.StudentServices" scope="prototype" >
<lookup-method name="getNotification" bean="schoolNotification"/>
</bean>
</beans>
Original file line number Diff line number Diff line change
@@ -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();
}
}