Here is a complete example using only CDI taken from this site .
This configuration will also work for JBoss AS7.
Create and fill in the properties file inside the WildFly configuration folder
$ echo 'docs.dir=/var/documents' >> .standalone/configuration/application.properties
Add a system property to the WildFly configuration file.
$ ./bin/jboss-cli.sh --connect [ standalone@localhost :9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties)
This will add the following to your server configuration file (standalone.xml or domain.xml):
<system-properties> <property name="application.properties" value="${jboss.server.config.dir}/application.properties"/> </system-properties>
Create a singleton bean session that downloads and saves application properties in general
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.annotation.PostConstruct; import javax.ejb.Singleton; @Singleton public class PropertyFileResolver { private Logger logger = Logger.getLogger(PropertyFileResolver.class); private String properties = new HashMap<>(); @PostConstruct private void init() throws IOException {
Create a CDI qualifier. We will use this annotation for the Java variables that we want to introduce.
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.inject.Qualifier; @Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR }) public @interface ApplicationProperty { // no default meaning a value is mandatory @Nonbinding String name(); }
Create a producer method; this generates the object to be inserted
import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; public class ApplicaitonPropertyProducer { @Inject private PropertyFileResolver fileResolver; @Produces @ApplicationProperty(name = "") public String getPropertyAsString(InjectionPoint injectionPoint) { String propertyName = injectionPoint.getAnnotated().getAnnotation(ApplicationProperty.class).name(); String value = fileResolver.getProperty(propertyName); if (value == null || propertyName.trim().length() == 0) { throw new IllegalArgumentException("No property found with name " + value); } return value; } @Produces @ApplicationProperty(name="") public Integer getPropertyAsInteger(InjectionPoint injectionPoint) { String value = getPropertyAsString(injectionPoint); return value == null ? null : Integer.valueOf(value); } }
Finally, add the property to one of your CDI beans
import javax.ejb.Stateless; import javax.inject.Inject; @Stateless public class MySimpleEJB { @Inject @ApplicationProperty(name = "docs.dir") private String myProperty; public String getProperty() { return myProperty; } }
Chris ritchie
source share