-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathServicesModule.java
More file actions
31 lines (26 loc) · 1.17 KB
/
ServicesModule.java
File metadata and controls
31 lines (26 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.uid2.admin.vertx.service;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
import lombok.val;
import java.util.Arrays;
/*
This is a temporary module which accepts an array of pre-created singleton services and makes them available as a module.
Over time, we would ideally move to letting the DI framework create the services as well - this temporary solution
is being used to support a strangler-pattern introduction of DI.
*/
public class ServicesModule extends AbstractModule {
private final IService[] services;
public ServicesModule(IService[] services) {
this.services = services;
}
@Override
protected void configure() {
Multibinder<IService> serviceInterfaceBinder = Multibinder.newSetBinder(binder(), IService.class);
Arrays.stream(services).forEach(s -> {
bind((Class<IService>)s.getClass()).toInstance(s);
serviceInterfaceBinder.addBinding().toInstance(s);
val interfaces = Arrays.stream(s.getClass().getInterfaces()).filter(i -> i != IService.class);
interfaces.forEach(i -> bind((Class)i).toInstance(s));
});
}
}