Skip to content

Commit 504bf36

Browse files
committed
#237 - ENH: Support custom class loader like: BeanScope.builder().classLoader(customClassLoader)
1 parent 4d754f3 commit 504bf36

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example.coffee;
2+
3+
import io.avaje.inject.BeanScope;
4+
import org.example.coffee.provider.ProvOther;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class ClassLoaderTest {
10+
11+
@Test
12+
void testUsingClassLoader() {
13+
try (BeanScope context = BeanScope.builder()
14+
.classLoader(Thread.currentThread().getContextClassLoader())
15+
.build()) {
16+
ProvOther bean = context.get(ProvOther.class);
17+
String other = bean.other();
18+
assertThat(other).isEqualTo("mush mush beans");
19+
}
20+
}
21+
}

inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ default <D> BeanScopeBuilder withBean(Type type, D bean) {
212212
return bean(type, bean);
213213
}
214214

215+
/**
216+
* Set the ClassLoader to use when loading modules.
217+
*
218+
* @param classLoader The ClassLoader to use
219+
*/
220+
BeanScopeBuilder classLoader(ClassLoader classLoader);
221+
215222
/**
216223
* Use the given BeanScope as the parent. This becomes an additional
217224
* source of beans that can be wired and accessed in this scope.

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.avaje.inject.spi.EnrichBean;
55
import io.avaje.inject.spi.Module;
66
import io.avaje.inject.spi.SuppliedBean;
7+
import io.avaje.lang.NonNullApi;
78
import io.avaje.lang.Nullable;
89

910
import java.lang.System.Logger.Level;
@@ -14,21 +15,20 @@
1415
/**
1516
* Build a bean scope with options for shutdown hook and supplying test doubles.
1617
*/
18+
@NonNullApi
1719
class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
1820

1921
private static final System.Logger log = System.getLogger("io.avaje.inject");
2022

2123
@SuppressWarnings("rawtypes")
2224
private final List<SuppliedBean> suppliedBeans = new ArrayList<>();
23-
2425
@SuppressWarnings("rawtypes")
2526
private final List<EnrichBean> enrichBeans = new ArrayList<>();
26-
2727
private final Set<Module> includeModules = new LinkedHashSet<>();
28-
2928
private BeanScope parent;
3029
private boolean parentOverride = true;
3130
private boolean shutdownHook;
31+
private ClassLoader classLoader;
3232

3333
/**
3434
* Create a BeanScopeBuilder to ultimately load and return a new BeanScope.
@@ -83,6 +83,12 @@ public <D> BeanScopeBuilder bean(@Nullable String name, Type type, D bean) {
8383
return this;
8484
}
8585

86+
@Override
87+
public BeanScopeBuilder classLoader(ClassLoader classLoader) {
88+
this.classLoader = classLoader;
89+
return this;
90+
}
91+
8692
@Override
8793
public BeanScopeBuilder parent(BeanScope parent) {
8894
this.parent = parent;
@@ -139,7 +145,8 @@ public BeanScope build() {
139145
// sort factories by dependsOn
140146
FactoryOrder factoryOrder = new FactoryOrder(parent, includeModules, !suppliedBeans.isEmpty());
141147
if (factoryOrder.isEmpty()) {
142-
ServiceLoader.load(Module.class).forEach(factoryOrder::add);
148+
var loader = classLoader != null ? classLoader : Thread.currentThread().getContextClassLoader();
149+
ServiceLoader.load(Module.class, loader).forEach(factoryOrder::add);
143150
}
144151

145152
Set<String> moduleNames = factoryOrder.orderFactories();
@@ -277,7 +284,7 @@ private void processQueue() {
277284
}
278285
sb.append(" - none of the loaded modules ").append(moduleNames).append(" have this in their @InjectModule( provides = ... ). ");
279286
if (parent != null) {
280-
sb.append("The parent BeanScope " + parent + " also does not provide this dependency. ");
287+
sb.append("The parent BeanScope ").append(parent).append(" also does not provide this dependency. ");
281288
}
282289
sb.append("Either @InjectModule requires/provides are not aligned? or add external dependencies via BeanScopeBuilder.bean()?");
283290
throw new IllegalStateException(sb.toString());

0 commit comments

Comments
 (0)