I'm trying to read deployment specific information from a properties file in my wildfly configuration folder. I tried this:
@Singleton
@Startup
public class DeploymentConfiguration {
protected Properties props;
@PostConstruct
public void readConfig() {
props = new Properties();
try {
props.load(getClass().getClassLoader().getResourceAsStream("my.properties"));
} catch (IOException e) {
// ... whatever
}
}
But apparently this is not working since the configuration folder is not in the classpath anymore. Now I can't find an easy way to do it. My favorite would be something like this:
@InjectProperties("my.properties")
protected Properties props;
The only solution I found on the web so far involves making my own OSGi module, but I believe there must be an easier way to do it (one without OSGi!). Can anyone show me how?
To avoid this kind of problem the issue is to set the
jboss.server.config.dir
in VM arguments like that :If you want to explicitly read a file from the configuration directory (e.g.
$WILDFLY_HOME/standalone/configuration
ordomain/configuration
) there's a system property with the path in it. Simply doSystem.getProperty("jboss.server.config.dir");
and append your file name to that to get the file.You wouldn't read it as a resource though, so...
Then the file would be loaded for you.
Also, since WildFly doesn't ship with OSGi support anymore, I don't know how creating an OSGi module would help you here.
The simplest thing you can do is to run
standalone.sh
with a-P
option referencing your properties file (you need a URLfile:/path/to/my.properties
, or put the file in$WILDFLY_HOME/bin
).Then all properties from the file will be loaded as system properties.
For injecting configuration properties into your application classes, have a look at DeltaSpike Configuration, which supports different property sources like system properties, environment variables, JNDI entries and hides the specific source from your application.
Alternatively, to avoid setting system properties (which will be global in the sense of being visible to all applications deployed to your WildFly instance), you can also define a custom property source for DeltaSpike reading a properties file from any given location, and these properties will be local to your application.
If you have in standalone.xml property:
you can wasily read it with:
Or if you specify context param in web.xml like:
You can read it in Java bean:
Here is a full example using just CDI, taken from this site.
Create and populate a properties file inside the WildFly configuration folder
Add a system property to the WildFly configuration file.
This will add the following to your server configuration file (standalone.xml or domain.xml):
Create the singleton session bean that loads and stores the application wide properties
Create the CDI Qualifier. We will use this annotation on the Java variables we wish to inject into.
Create the producer method; this generates the object to be injected
Lastly inject the property into one of your CDI beans