Comments (7)
I don't understand the question, you're trying to bind an HTTP client in Guice? Or access things from Guice when building the HTTP client? What type of HTTP client? Are you sure this is related to the dropwizard-guice library?
from dropwizard-guice.
Hi, Jonathan.
My case is to create managed instance, like here:
http://www.dropwizard.io/0.9.1/docs/manual/client.html
and use it injecting in Guice objects. Type of client is not important.
On 14 December 2015 at 21:45, Jonathan Haber [email protected]
wrote:
I don't understand the question, you're trying to bind an HTTP client in
Guice? Or access things from Guice when building the HTTP client? What type
of HTTP client? Are you sure this is related to the dropwizard-guice
library?—
Reply to this email directly or view it on GitHub
#78 (comment)
.
from dropwizard-guice.
Ah I think I understand now. This is possible, but because the Guice injector is created during dropwizard's initialize phase and the Environment isn't available until the run phase, the binding can't be created eagerly otherwise it will fail.
Assuming you're running Guice in Stage.DEVELOPMENT
and followed this documentation for adding a HttpClientConfiguration
field to your dropwizard configuration object, you should be able to add something like this to your Guice module: (replacing ExampleConfiguration
with your configuration class)
@Provides
@Singleton
public HttpClient providesHttpClient(ExampleConfiguration config, Environment environment) {
return new HttpClientBuilder(environment).using(config.getHttpClientConfiguration()).build();
}
After which you should be able to inject HttpClient
into other classes
from dropwizard-guice.
Hi, thanks for quick responses!
Going to try this!
-----Исходное сообщение-----
От: "Jonathan Haber" [email protected]
Отправлено: 15.12.2015 0:52
Кому: "HubSpot/dropwizard-guice" [email protected]
Копия: "Oleksii Burov" [email protected]
Тема: Re: [dropwizard-guice] [question] Injecting http client (#78)
Ah I think I understand now. This is possible, but because the Guice injector is created during dropwizard's initialize phase and the Environment isn't available until the run phase, the binding can't be created eagerly otherwise it will fail.
Assuming you're running Guice in Stage.DEVELOPMENT and followed this documentation for adding a HttpClientConfiguration field to your configuration object, you should be able to add something like this to your Guice module:
@provides
@singleton
public HttpClient providesHttpClient(ExampleConfiguration config, Environment environment) {
return new HttpClientBuilder(environment).using(config.getHttpClientConfiguration()).build()
}—
Reply to this email directly or view it on GitHub.
from dropwizard-guice.
Is there any better approach to this than running in "development" ? There seems to be a lot of posts about inability to use Environment during the bootstrap phase, but no clearly documented solution - just in-passing mentions of governator's LazySingleton or then reconfiguring to run in "development" stage, which smacks of a hack.
Did I miss some documentation that solves this issue 'properly'? TIA
from dropwizard-guice.
@mrwilby with dropwizard-guice the binding needs to be created lazily (since the Injector is creating during the initialize phase and the environment isn't available until the run phase). AFAIK there is no way to do what you want 'properly', the only workarounds I know of are the ones you mention. However, you should check out dropwizard-guicier which creates the injector during the run phase (with Stage.PRODUCTION
by default), ditches AutoConfig
entirely, and is just generally better designed in my opinion. With dropwizard-guicier you could add a module like this:
public class ExampleModule extends DropwizardAwareModule<ExampleConfiguration> {
@Override
public void configure(Binder binder) {
HttpClient httpClient = new HttpClientBuilder(getEnvironment())
.using(getConfiguration().getHttpClientConfiguration())
.build();
binder.bind(HttpClient.class).toInstance(httpClient);
}
}
from dropwizard-guice.
Thanks @jhaber . I actually discovered guicer the other day from reading another of your posts, and made the switch already. Guicier is much better for my use cases! Thanks!
from dropwizard-guice.
Related Issues (20)
- Can not register jersey client in guice
- Binding annotations on providers don't seem to work HOT 1
- AutoConfig should ignore anonymous class HOT 1
- Dropwizard Not Starting Due to HK2 Failure HOT 4
- dropwizrd-guice not working with Dropwizard-core 1.1.0
- LinkageError ... attempted duplicate class definition for name: "org/glassfish/hk2/api/ServiceLocatorState"
- jersey2-guice is defunct HOT 1
- DropWizard 0.9.1 support? HOT 10
- Are there plans to add DropWizard 1.0.0 support? HOT 5
- No License inside Jar file HOT 4
- Getting a warning at startup: "It appears jersey2-guice-spi is either not present or in conflict with some other Jar" HOT 14
- com.google.inject.OutOfScopeException on inject javax.servlet.http.HttpServletRequest HOT 11
- Binding annotations aren't being consistently honored HOT 3
- Integration Test using mocked beans HOT 1
- @BeanParam + @QueryParam don't work with Dropwizard-guice HOT 6
- when i add a filter to jersey , then Jersey error HOT 5
- Compatiblity with Dropwizard 1.0? HOT 27
- AutoConfig not honouring Guice Singleton annotation on resource classes HOT 1
- How exactly HelloWorldConfiguration and Environment get injected in the example HOT 1
- Deprecated API WARNing in v1.0.0.2 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dropwizard-guice.