Skip to content

Commit 806d2d8

Browse files
authored
BAEL-415: Custom Scope in Spring (eugenp#1578)
* Add files via upload * Update pom.xml * Update RunGuice.java * Update Communication.java * Update CommunicationMode.java * Update DefaultCommunicator.java * Update EmailCommunicationMode.java * Update IMCommunicationMode.java * Update SMSCommunicationMode.java * Update MessageLogger.java * Update MessageSentLoggable.java * Update AOPModule.java * Update BasicModule.java * Update CommunicationModel.java * Update Communicator.java * Update BasicModule.java * Update RunGuice.java * Update MessageLogger.java * Update Communicator.java * Update pom.xml * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * Update pom.xml * Update pom.xml * Update pom.xml * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom * BAEL-9: Updated README.md * BAEL-157: README.md updated * Changed project name * Update RunGuice.java Removed references to message logging and output * Update Communication.java Removed message logging-related code * BAEL-566: Updated README.md * New project name * BAEL-393: removing guice-intro directory * BAEL-393: renamed module guice-intro to guice in root pom.xml * BAEL-393 and BAEL-541 README.md files * BAEL-731: Updated README.md * BAEL-680: renamed test methods * BAEL-714: Updated README.md * BAEL-737: Updated README.md * BAEL-680 and BAEL-756 README.md updates * BAEL-666: Updated README * BAEL-415: Custom Scope * BAEL-415: Custom Scope - renamed classes to reflect TenantScope
1 parent bbcafe2 commit 806d2d8

6 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.customscope;
2+
3+
public class TenantBean {
4+
5+
private final String name;
6+
7+
public TenantBean(String name) {
8+
this.name = name;
9+
}
10+
11+
public void sayHello() {
12+
System.out.println(String.format("Hello from %s of type %s",
13+
this.name,
14+
this.getClass().getName()));
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.customscope;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
5+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6+
7+
public class TenantBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
8+
9+
@Override
10+
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
11+
factory.registerScope("tenant", new TenantScope());
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.customscope;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Scope;
6+
7+
@Configuration
8+
public class TenantBeansConfig {
9+
10+
@Scope(scopeName = "tenant")
11+
@Bean
12+
public TenantBean foo() {
13+
return new TenantBean("foo");
14+
}
15+
16+
@Scope(scopeName = "tenant")
17+
@Bean
18+
public TenantBean bar() {
19+
return new TenantBean("bar");
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.baeldung.customscope;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.springframework.beans.factory.ObjectFactory;
8+
import org.springframework.beans.factory.config.Scope;
9+
10+
public class TenantScope implements Scope {
11+
12+
private Map<String, Object> scopedObjects = Collections.synchronizedMap(new HashMap<String, Object>());
13+
private Map<String, Runnable> destructionCallbacks = Collections.synchronizedMap(new HashMap<String, Runnable>());
14+
15+
@Override
16+
public Object get(String name, ObjectFactory<?> objectFactory) {
17+
if(!scopedObjects.containsKey(name)) {
18+
scopedObjects.put(name, objectFactory.getObject());
19+
}
20+
return scopedObjects.get(name);
21+
}
22+
23+
@Override
24+
public Object remove(String name) {
25+
destructionCallbacks.remove(name);
26+
return scopedObjects.remove(name);
27+
}
28+
29+
@Override
30+
public void registerDestructionCallback(String name, Runnable callback) {
31+
destructionCallbacks.put(name, callback);
32+
}
33+
34+
@Override
35+
public Object resolveContextualObject(String key) {
36+
return null;
37+
}
38+
39+
@Override
40+
public String getConversationId() {
41+
return "tenant";
42+
}
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.customscope;
2+
3+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class TenantScopeConfig {
9+
10+
@Bean
11+
public static BeanFactoryPostProcessor beanFactoryPostProcessor() {
12+
return new TenantBeanFactoryPostProcessor();
13+
}
14+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.baeldung.customscope;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.CoreMatchers.not;
5+
import static org.junit.Assert.assertThat;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.Map;
9+
10+
import org.junit.Test;
11+
import org.springframework.beans.factory.config.BeanDefinition;
12+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
13+
14+
public class TenantScopeTest {
15+
16+
@Test
17+
public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() {
18+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
19+
try{
20+
ctx.register(TenantScopeConfig.class);
21+
ctx.register(TenantBeansConfig.class);
22+
ctx.refresh();
23+
24+
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
25+
foo.sayHello();
26+
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
27+
bar.sayHello();
28+
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
29+
30+
assertThat(foo, not(equalTo(bar)));
31+
assertThat(foos.size(), equalTo(2));
32+
assertTrue(foos.containsValue(foo));
33+
assertTrue(foos.containsValue(bar));
34+
35+
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
36+
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
37+
38+
assertThat(fooDefinition.getScope(), equalTo("tenant"));
39+
assertThat(barDefinition.getScope(), equalTo("tenant"));
40+
}
41+
finally {
42+
ctx.close();
43+
}
44+
}
45+
46+
@Test
47+
public final void whenComponentScan_thenContextContainsFooAndBar() {
48+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
49+
try{
50+
ctx.scan("org.baeldung.customscope");
51+
ctx.refresh();
52+
53+
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
54+
foo.sayHello();
55+
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
56+
bar.sayHello();
57+
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
58+
59+
assertThat(foo, not(equalTo(bar)));
60+
assertThat(foos.size(), equalTo(2));
61+
assertTrue(foos.containsValue(foo));
62+
assertTrue(foos.containsValue(bar));
63+
64+
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
65+
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
66+
67+
assertThat(fooDefinition.getScope(), equalTo("tenant"));
68+
assertThat(barDefinition.getScope(), equalTo("tenant"));
69+
}
70+
finally {
71+
ctx.close();
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)