0
# Core Application Framework
1
2
The core application framework provides the essential classes for building Dropwizard applications, including the main Application class, Configuration base class, and Bootstrap setup.
3
4
## Capabilities
5
6
### Application Class
7
8
The abstract base class that all Dropwizard applications must extend. Provides application lifecycle management, command registration, and configuration handling.
9
10
```java { .api }
11
/**
12
* The base class for Dropwizard applications.
13
* @param <T> the type of configuration class for this application
14
*/
15
public abstract class Application<T extends Configuration> {
16
/**
17
* Returns the name of the application.
18
* @return the application's name
19
*/
20
public String getName();
21
22
/**
23
* Returns the Class of the configuration class type parameter.
24
* @return the configuration class
25
*/
26
public Class<T> getConfigurationClass();
27
28
/**
29
* Initializes the application bootstrap.
30
* @param bootstrap the application bootstrap
31
*/
32
public void initialize(Bootstrap<T> bootstrap);
33
34
/**
35
* When the application runs, this is called after the bundles are run.
36
* Override to add providers, resources, etc. for your application.
37
* @param configuration the parsed Configuration object
38
* @param environment the application's Environment
39
* @throws Exception if something goes wrong
40
*/
41
public abstract void run(T configuration, Environment environment) throws Exception;
42
43
/**
44
* Parses command-line arguments and runs the application.
45
* Call this method from a public static void main entry point.
46
* @param arguments the command-line arguments
47
* @throws Exception if something goes wrong
48
*/
49
public void run(String... arguments) throws Exception;
50
51
/**
52
* The log level at which to bootstrap logging on application startup.
53
* @return the bootstrap log level (default: Level.WARN)
54
*/
55
protected Level bootstrapLogLevel();
56
57
/**
58
* Called by run(String...) to add the standard "server" and "check" commands
59
* @param bootstrap the bootstrap instance
60
*/
61
protected void addDefaultCommands(Bootstrap<T> bootstrap);
62
63
/**
64
* Called by run(String...) to indicate there was a fatal error running the requested command.
65
* @param t The Throwable instance which caused the command to fail
66
*/
67
protected void onFatalError(Throwable t);
68
}
69
```
70
71
**Usage Examples:**
72
73
```java
74
public class MyApplication extends Application<MyConfiguration> {
75
public static void main(String[] args) throws Exception {
76
new MyApplication().run(args);
77
}
78
79
@Override
80
public String getName() {
81
return "my-service";
82
}
83
84
@Override
85
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
86
// Add bundles during initialization
87
bootstrap.addBundle(new AssetsBundle("/assets/", "/", "index.html"));
88
bootstrap.addBundle(new SslReloadBundle());
89
}
90
91
@Override
92
public void run(MyConfiguration configuration, Environment environment) {
93
// Register resources
94
environment.jersey().register(new MyResource());
95
96
// Register health checks
97
environment.healthChecks().register("database", new DatabaseHealthCheck());
98
99
// Register admin tasks
100
environment.admin().addTask(new MyAdminTask());
101
}
102
}
103
```
104
105
### Configuration Class
106
107
Base configuration class that applications extend to define their configuration schema. Supports YAML-based configuration with Jackson binding and Bean Validation.
108
109
```java { .api }
110
/**
111
* An object representation of the YAML configuration file.
112
* Extend this with your own configuration properties.
113
*/
114
public class Configuration {
115
/**
116
* Returns the server-specific section of the configuration file.
117
* @return server-specific configuration parameters
118
*/
119
@JsonProperty("server")
120
public ServerFactory getServerFactory();
121
122
/**
123
* Sets the HTTP-specific section of the configuration file.
124
* @param factory the server factory
125
*/
126
@JsonProperty("server")
127
public void setServerFactory(ServerFactory factory);
128
129
/**
130
* Returns the logging-specific section of the configuration file.
131
* @return logging-specific configuration parameters
132
*/
133
@JsonProperty("logging")
134
public synchronized LoggingFactory getLoggingFactory();
135
136
/**
137
* Sets the logging-specific section of the configuration file.
138
* @param factory the logging factory
139
*/
140
@JsonProperty("logging")
141
public synchronized void setLoggingFactory(LoggingFactory factory);
142
143
/**
144
* Returns the metrics-specific section of the configuration file.
145
* @return metrics configuration parameters
146
*/
147
@JsonProperty("metrics")
148
public MetricsFactory getMetricsFactory();
149
150
/**
151
* Sets the metrics-specific section of the configuration file.
152
* @param metrics the metrics factory
153
*/
154
@JsonProperty("metrics")
155
public void setMetricsFactory(MetricsFactory metrics);
156
157
/**
158
* Returns the admin interface-specific section of the configuration file.
159
* @return admin interface-specific configuration parameters
160
*/
161
@JsonProperty("admin")
162
public AdminFactory getAdminFactory();
163
164
/**
165
* Sets the admin interface-specific section of the configuration file.
166
* @param admin the admin factory
167
*/
168
@JsonProperty("admin")
169
public void setAdminFactory(AdminFactory admin);
170
171
/**
172
* Returns the health interface-specific section of the configuration file.
173
* @return health interface-specific configuration parameters
174
*/
175
@JsonProperty("health")
176
public Optional<HealthFactory> getHealthFactory();
177
178
/**
179
* Sets the health interface-specific section of the configuration file.
180
* @param health the health factory
181
*/
182
@JsonProperty("health")
183
public void setHealthFactory(HealthFactory health);
184
}
185
```
186
187
**Usage Examples:**
188
189
```java
190
public class MyConfiguration extends Configuration {
191
@NotEmpty
192
private String databaseUrl;
193
194
@Min(1)
195
@Max(100)
196
private int connectionPoolSize = 10;
197
198
@Valid
199
@NotNull
200
private RedisConfiguration redis = new RedisConfiguration();
201
202
@JsonProperty
203
public String getDatabaseUrl() {
204
return databaseUrl;
205
}
206
207
@JsonProperty
208
public void setDatabaseUrl(String databaseUrl) {
209
this.databaseUrl = databaseUrl;
210
}
211
212
@JsonProperty
213
public int getConnectionPoolSize() {
214
return connectionPoolSize;
215
}
216
217
@JsonProperty
218
public void setConnectionPoolSize(int connectionPoolSize) {
219
this.connectionPoolSize = connectionPoolSize;
220
}
221
222
@JsonProperty
223
public RedisConfiguration getRedis() {
224
return redis;
225
}
226
227
@JsonProperty
228
public void setRedis(RedisConfiguration redis) {
229
this.redis = redis;
230
}
231
}
232
```
233
234
## Configuration File Format
235
236
Configuration is defined in YAML format and automatically parsed:
237
238
```yaml
239
# Server configuration
240
server:
241
type: default
242
applicationConnectors:
243
- type: http
244
port: 8080
245
adminConnectors:
246
- type: http
247
port: 8081
248
249
# Logging configuration
250
logging:
251
level: INFO
252
loggers:
253
com.example: DEBUG
254
255
# Custom application configuration
256
databaseUrl: "jdbc:postgresql://localhost/mydb"
257
connectionPoolSize: 20
258
redis:
259
host: localhost
260
port: 6379
261
```
262
263
## Types
264
265
```java { .api }
266
// Log level enumeration used by bootstrapLogLevel()
267
public enum Level {
268
ERROR, WARN, INFO, DEBUG, TRACE
269
}
270
```