|
4 | 4 | import io.avaje.inject.spi.EnrichBean;
|
5 | 5 | import io.avaje.inject.spi.Module;
|
6 | 6 | import io.avaje.inject.spi.SuppliedBean;
|
| 7 | +import io.avaje.lang.NonNullApi; |
7 | 8 | import io.avaje.lang.Nullable;
|
8 | 9 |
|
9 | 10 | import java.lang.System.Logger.Level;
|
|
14 | 15 | /**
|
15 | 16 | * Build a bean scope with options for shutdown hook and supplying test doubles.
|
16 | 17 | */
|
| 18 | +@NonNullApi |
17 | 19 | class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
|
18 | 20 |
|
19 | 21 | private static final System.Logger log = System.getLogger("io.avaje.inject");
|
20 | 22 |
|
21 | 23 | @SuppressWarnings("rawtypes")
|
22 | 24 | private final List<SuppliedBean> suppliedBeans = new ArrayList<>();
|
23 |
| - |
24 | 25 | @SuppressWarnings("rawtypes")
|
25 | 26 | private final List<EnrichBean> enrichBeans = new ArrayList<>();
|
26 |
| - |
27 | 27 | private final Set<Module> includeModules = new LinkedHashSet<>();
|
28 |
| - |
29 | 28 | private BeanScope parent;
|
30 | 29 | private boolean parentOverride = true;
|
31 | 30 | private boolean shutdownHook;
|
| 31 | + private ClassLoader classLoader; |
32 | 32 |
|
33 | 33 | /**
|
34 | 34 | * 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) {
|
83 | 83 | return this;
|
84 | 84 | }
|
85 | 85 |
|
| 86 | + @Override |
| 87 | + public BeanScopeBuilder classLoader(ClassLoader classLoader) { |
| 88 | + this.classLoader = classLoader; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
86 | 92 | @Override
|
87 | 93 | public BeanScopeBuilder parent(BeanScope parent) {
|
88 | 94 | this.parent = parent;
|
@@ -139,7 +145,8 @@ public BeanScope build() {
|
139 | 145 | // sort factories by dependsOn
|
140 | 146 | FactoryOrder factoryOrder = new FactoryOrder(parent, includeModules, !suppliedBeans.isEmpty());
|
141 | 147 | 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); |
143 | 150 | }
|
144 | 151 |
|
145 | 152 | Set<String> moduleNames = factoryOrder.orderFactories();
|
@@ -277,7 +284,7 @@ private void processQueue() {
|
277 | 284 | }
|
278 | 285 | sb.append(" - none of the loaded modules ").append(moduleNames).append(" have this in their @InjectModule( provides = ... ). ");
|
279 | 286 | 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. "); |
281 | 288 | }
|
282 | 289 | sb.append("Either @InjectModule requires/provides are not aligned? or add external dependencies via BeanScopeBuilder.bean()?");
|
283 | 290 | throw new IllegalStateException(sb.toString());
|
|
0 commit comments