I want to translate the following Guice-based DI configuration to Spring Java-config based DI.
public class UserModule extends AbstractModule {
private final User user;
public UserModule(User user) {
this.user = user;
}
@Override
protected void configure() {
// don't need to do any configuration
}
@Provides @Singleton
User provideUser() {
return user;
}
@Provides @Singleton @Inject
UserStorageService provideUserStorageService(User u) {
return new UserStorageServiceImpl(u);
}
}
The intended use is then
Injector userOneInjector = Guice.createInjector(new UserModule(userOne));
Injector userTwoInjector = Guice.createInjector(new UserModule(userTwo));
Unfortunately, Spring’s AnnotationConfigApplicationContext takes in a configuration class rather than an object, so I’m not sure how to inject the a User object into its configuration.
A separate
@Configurationclass to isolate the ‘non-standard’ object?