这里只使用CDI,从这个site采取一个完整的例子。
此配置也适用于JBoss AS7。
创建并填充属性文件的WildFly配置文件夹
$ echo 'docs.dir=/var/documents' >> .standalone/configuration/application.properties
内的系统属性添加到WildFly配置文件。
$ ./bin/jboss-cli.sh --connect
[[email protected]:9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties)
这将以下内容添加到你的服务器的配置文件(或standalone.xml domain.xml的):
<system-properties>
<property name="application.properties" value="${jboss.server.config.dir}/application.properties"/>
</system-properties>
创建单加载和存储应用程序宽属性的会话bean
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 {
//matches the property name as defined in the system-properties element in WildFly
String propertyFile = System.getProperty("application.properties");
File file = new File(propertyFile);
Properties properties = new Properties();
try {
properties.load(new FileInputStream(file));
} catch (IOException e) {
logger.error("Unable to load properties file", e);
}
HashMap hashMap = new HashMap<>(properties);
this.properties.putAll(hashMap);
}
public String getProperty(String key) {
return properties.get(key);
}
}
创建CDI限定符。我们将在我们希望注入的Java变量上使用这个注释。
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();
}
创建生产者方法;此产生的对象将被注入
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);
}
}
最后的财产注入到你的CDI豆之一
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;
}
}