Proper way to register and use provider in module

1,564 views
Skip to first unread message

Paul Fullbright

unread,
Feb 23, 2018, 2:27:10 PM2/23/18
to Play Framework
I'm trying to figure out the proper way to configure providers and then also use them in modules.

I have this class:

public class Initializer
extends AbstractModule {

private static final Logger logger = LoggerFactory.getLogger(Initializer.class);

private Environment env;
private Config config;
public Initializer(Environment env, Config config) {
this.env = env;
this.config = config;
}
@Override
protected void configure() {
bind(ClientFactoryBean.class).toProvider(ClientFactoryBeanProvider.class).asEagerSingleton();
        
try {
ClientFactoryBean clientFactory = getProvider(ClientFactoryBean.class).get();
Client client = clientFactory.create(config.getString("adminUser"), config.getString("adminPassword"));
// do client configuration
} catch (Exception e) {
logger.error("Error initializing", e);
}
}
}

But I get this error:

[error] c.g.u.m.Initializer - Error initializing
java.lang.IllegalStateException: This Provider cannot be used until the Injector has been created.
        at com.google.common.base.Preconditions.checkState(Preconditions.java:456)
        at com.google.inject.spi.ProviderLookup$1.get(ProviderLookup.java:102)
        at c.g.u.m.Initializer.configure(Initializer.java:39)
        at com.google.inject.AbstractModule.configure(AbstractModule.java:62)
        at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340)
        at com.google.inject.spi.Elements.getElements(Elements.java:110)
        at com.google.inject.util.Modules$OverrideModule.configure(Modules.java:177)
        at com.google.inject.AbstractModule.configure(AbstractModule.java:62)
        at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340)
        at com.google.inject.spi.Elements.getElements(Elements.java:110)


How am I supposed to properly initialize and use a provider as part of a module configuration?

Thanks,

Greg Methvin

unread,
Feb 23, 2018, 2:38:34 PM2/23/18
to play-framework
You should not be doing any initialization in the configure method. That's only for configuring bindings.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/a8062af3-0709-4573-93ca-0c224fd5174b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Fullbright

unread,
Feb 23, 2018, 3:37:19 PM2/23/18
to Play Framework
OK, that is basically how I have implemented my provider for my Client class. Question though, if I'm not supposed to do any initialization in my module, how am I supposed to do initialization? Isn't that how Flyway does db initialization?

Greg Methvin

unread,
Feb 23, 2018, 3:53:25 PM2/23/18
to play-fr...@googlegroups.com
You do the initialization in a bound Provider or in a @Provides method.

Greg

From: play-fr...@googlegroups.com <play-fr...@googlegroups.com> on behalf of Paul Fullbright <pwfull...@gmail.com>
Sent: Friday, February 23, 2018 12:37:18 PM
To: Play Framework
Subject: Re: [play-framework] Proper way to register and use provider in module
 
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/0e649347-1266-40c3-8ae7-6c81b5a45d8c%40googlegroups.com.

Paul Fullbright

unread,
Feb 23, 2018, 4:29:17 PM2/23/18
to Play Framework
So how do you make sure it runs at startup? Or how do you control the order of invocation of Providers?

Greg Methvin

unread,
Feb 23, 2018, 5:34:45 PM2/23/18
to play-framework
You can bind your Provider as an eager singleton to force it to run on startup.

The order of invocation is determined by dependencies. If A depends on B, then B's provider is called first.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/10582653-5cf9-475e-be4b-c48c0fe8e6bc%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages