prabello
prabello

Reputation: 556

How to @Inject values from a .properties file with CDI

How can i inject some values from a properties file? I have a .properties file with some keys and values, i would like to inject those to use on some classes, something like:

@Inject(file = "/WEB-INF/abc.properties",key ="path")
private String path;

So when i use the path it has the value from the properties file, instead of going thorough the entire process of reading the properties and getting the value.

Upvotes: 2

Views: 2884

Answers (1)

Antonin Stefanutti
Antonin Stefanutti

Reputation: 1071

While there has been active discussion about integrating configuration in CDI, the current status is that there won't be a standard Java configuration JSR anytime soon.

In the meantime, configuration integration is provided by third-parties like DeltaSpike, see http://deltaspike.apache.org/documentation/configuration.html so that it is possible to write:

@Inject
@ConfigProperty(name = "endpoint.poll.interval")
private Integer pollInterval;

That relies on the service loader meachanism to extend the configuration sourcing.

The Apache Tamaya project plans to have CDI integration as well.

Upvotes: 5

Related Questions