O'Reilly logo

Hands-On Enterprise Java Microservices with Eclipse MicroProfile by Scott Stark, Antoine Sabot-Durand, Pavol Loffay, Jeff Mesnil, Heiko W. Rupp, Cesar Saavedra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The @ConfigProperty annotation

The @ConfigProperty annotation can be used to inject configuration values in Java fields or method parameters using CDI, as shown:

@Inject
@ConfigProperty(name="my.url")
private URL myURL;

The @ConfigProperty annotation can have defaultValue, which is used to configure the field if the configuration property is not found in the underlying Config:

@Inject
@ConfigProperty(name="my.url", defaultValue="http://localhost/")
private URL myURL;

If defaultValue is not set and no property is found, the application will throw DeploymentException as it cannot be properly configured.

It is possible to use Optional if a configuration property might not be present, as shown in the following code block:

@Inject @ConfigProperty(name="my.url") ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required