Skip to content

Commit df973aa

Browse files
author
Dan Jasek
committed
Target DropWizard 0.8.
Break out configuration code. Implement InjectedCommands. Added tests. Various bug fixes and refactorings.
1 parent 1f3c92c commit df973aa

59 files changed

Lines changed: 2131 additions & 392 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ target/
33
.project
44
.settings
55
.idea
6-
*.iml
6+
*.iml

README.md

Lines changed: 157 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,175 @@ the dropwizard environment upon service start.
77

88
### Usage
99

10+
```xml
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.hubspot.dropwizard</groupId>
14+
<artifactId>dropwizard-guice</artifactId>
15+
<version>0.7.0.2</version>
16+
</dependency>
17+
</dependencies>
18+
```
19+
1020
Simply install a new instance of the bundle during your service initialization
1121
```java
12-
public class HelloWorldService extends Application<HelloWorldConfiguration> {
22+
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
23+
24+
private GuiceBundle<HelloWorldConfiguration> guiceBundle;
1325

1426
public static void main(String[] args) throws Exception {
15-
new HelloWorldService().run(args);
16-
}
17-
18-
@Override
19-
public String getName() {
20-
return "hello-world";
21-
}
22-
23-
@Override
24-
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
25-
bootstrap.addBundle(GuiceBundle.newBuilder()
26-
.addModule(new HelloWorldModule())
27-
.build()
28-
);
29-
}
30-
31-
@Override
32-
public void run(HelloWorldConfiguration configuration, final Environment environment) {
33-
environment.jersey().register(HelloWorldResource.class);
34-
environment.healthChecks().register("Template", TemplateHealthCheck.class);
35-
}
27+
new HelloWorldApplication().run(args);
28+
}
29+
30+
@Override
31+
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
32+
33+
guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
34+
.addModule(new HelloWorldModule())
35+
.setConfigClass(HelloWorldConfiguration.class)
36+
.build();
37+
38+
bootstrap.addBundle(guiceBundle);
39+
}
3640

41+
@Override
42+
public String getName() {
43+
return "hello-world";
44+
}
45+
46+
@Override
47+
public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
48+
environment.jersey().register(HelloWorldResource.class);
49+
environment.lifecycle().manage(guiceBundle.getInjector().getInstance(TemplateHealthCheck.class));
50+
}
3751
}
3852
```
3953

40-
Lastly, you can enable auto configuration via package scanning.
54+
You can enable auto configuration via package scanning.
55+
```java
56+
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
57+
58+
public static void main(String[] args) throws Exception {
59+
new HelloWorldApplication().run(args);
60+
}
61+
62+
@Override
63+
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
64+
65+
GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
66+
.addModule(new HelloWorldModule())
67+
.enableAutoConfig(getClass().getPackage().getName())
68+
.setConfigClass(HelloWorldConfiguration.class)
69+
.build();
70+
71+
bootstrap.addBundle(guiceBundle);
72+
// with AutoConfig enabled you don't need to add bundles or commands explicitly here.
73+
// inherit from one of InjectedCommand, InjectedConfiguredCommand, or InjectedEnvironmentCommand
74+
// to get access to all modules during injection.
75+
}
76+
77+
@Override
78+
public String getName() {
79+
return "hello-world";
80+
}
81+
82+
@Override
83+
public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
84+
// now you don't need to add resources, tasks, healthchecks or providers
85+
// you must have your health checks inherit from InjectableHealthCheck in order for them to be injected
86+
}
87+
}
88+
89+
Modules will also be injected before being added. Field injections only, constructor based injections will not be available.
90+
Configuration data and initialization module data will be available for injecting into modules.
4191
```java
42-
public class HelloWorldService extends Service<HelloWorldConfiguration> {
92+
93+
94+
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
4395

4496
public static void main(String[] args) throws Exception {
45-
new HelloWorldService().run(args);
46-
}
47-
48-
@Override
49-
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
50-
bootstrap.setName("hello-world");
51-
bootstrap.addBundle(GuiceBundle.newBuilder()
52-
.addModule(new HelloWorldModule())
53-
.enableAutoConfig(getClass().getPackage().getName())
54-
.build()
55-
);
56-
}
57-
58-
@Override
59-
public void run(HelloWorldConfiguration configuration, final Environment environment) {
60-
// now you don't need to add resources, tasks, healthchecks or providers
61-
// you must have your health checks inherit from InjectableHealthCheck in order for them to be injected
62-
}
97+
new HelloWorldApplication().run(args);
98+
}
99+
100+
@Override
101+
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
102+
103+
GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
104+
.addInitModule(new BaseModule())
105+
// bindings defined in the BaseModule or any configuration data is available for
106+
// injection into HelloWorldModule fields
107+
.addModule(new HelloWorldModule())
108+
//Any resource, task, bundle, etc within this class path will be included automatically.
109+
.enableAutoConfig(getClass().getPackage().getName())
110+
//The contents of any config objects within this package path will be auto-injected.
111+
.addConfigPackages(getClass().getPackage().getName())
112+
.setConfigClass(HelloWorldConfiguration.class)
113+
.build();
114+
115+
bootstrap.addBundle(guiceBundle);
116+
}
117+
118+
@Override
119+
public String getName() {
120+
return "hello-world";
121+
}
122+
123+
@Override
124+
public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
125+
}
126+
}
127+
```
128+
129+
If you are having trouble accessing your Configuration or Environment inside a Guice Module, you could try using a provider.
130+
131+
```java
132+
public class HelloWorldModule extends AbstractModule {
133+
134+
@Override
135+
protected void configure() {
136+
// anything you'd like to configure
137+
}
138+
139+
@Provides
140+
public SomePool providesSomethingThatNeedsConfiguration(HelloWorldConfiguration configuration) {
141+
return new SomePool(configuration.getPoolName());
142+
}
143+
144+
@Provides
145+
public SomeManager providesSomenthingThatNeedsEnvironment(Environment env) {
146+
return new SomeManager(env.getSomethingFromHere()));
147+
}
148+
}
149+
```
150+
151+
You can also replace the default Guice `Injector` by implementing your own `InjectorFactory`. For example if you want
152+
to use [Governator](https://github.com/Netflix/governator) you can create the following implementation:
153+
154+
```java
155+
public class GovernatorInjectorFactory implements InjectorFactory {
156+
157+
@Override
158+
public Injector create( final Stage stage, final List<Module> modules ) {
159+
return LifecycleInjector.builder().inStage( stage ).withModules( modules ).build()
160+
.createInjector();
161+
}
162+
}
163+
```
164+
165+
and then set the InjectorFactory when initializing the GuiceBundle:
166+
167+
```java
168+
@Override
169+
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
170+
171+
GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
172+
.addModule(new HelloWorldModule())
173+
.enableAutoConfig(getClass().getPackage().getName())
174+
.setConfigClass(HelloWorldConfiguration.class)
175+
.setInjectorFactory( new GovernatorInjectorFactory() )
176+
.build();
63177

178+
bootstrap.addBundle(guiceBundle);
64179
}
65180
```
66181

0 commit comments

Comments
 (0)