java - What is the equivalent of @Value in CDI world? -
what way 1 inject property value property placeholder cdi bean?
in spring 1 write:
@org.springframework.beans.factory.annotation.value("${webservice.user}") private string webserviceuser;
what sets webserviceuser
field property webservice.user
property file/property placeholder.
how cdi? i've tried find answer, couldn't find equivalent. however, people write, can use cdi spring substitute on application servers, , use case basic, surely there must easy way, unfortunately i've failed find it.
cdi specification dependecy injection , context doesn't have such configuration things out of box. provides powerful extension mechanism allows third party projects add new portable features (i.e works cdi implementation , not tied server). important project providing cdi extensions apache deltaspike , news, provides need.
so need add deltaspike-core in project. if use maven, need add dependencies pom.xml
<dependency> <groupid>org.apache.deltaspike.core</groupid> <artifactid>deltaspike-core-api</artifactid> <version>0.4</version> </dependency> <dependency> <groupid>org.apache.deltaspike.core</groupid> <artifactid>deltaspike-core-impl</artifactid> <version>0.4</version> </dependency>
after if don't care properties filename, add meta-inf/apache-deltaspike.properties
project , put properties in it. if need more 1 file or want choose name you'll have implement propertyfileconfig
interface each file :
public class mycustompropertyfileconfig implements propertyfileconfig { @override public string getpropertyfilename() { return "myconfig.properties"; } }
after you'll able inject values
@applicationscoped public class somerandomservice { @inject @configproperty(name = "endpoint.poll.interval") private integer pollinterval; @inject @configproperty(name = "endpoint.poll.servername") private string pollurl; ... }
as see in example taken deltaspike documentation, can inject value in string in integer, long, float, boolean fields. provide own type if need more specific. deltaspike config documentation can found here.
Comments
Post a Comment