wildfly:从配置目录读取属性

wtlkbnrh  于 2021-06-30  发布在  Java
关注(0)|答案(7)|浏览(412)

我正在尝试从wildfly配置文件夹中的属性文件中读取特定于部署的信息。我试过这个:

  1. @Singleton
  2. @Startup
  3. public class DeploymentConfiguration {
  4. protected Properties props;
  5. @PostConstruct
  6. public void readConfig() {
  7. props = new Properties();
  8. try {
  9. props.load(getClass().getClassLoader().getResourceAsStream("my.properties"));
  10. } catch (IOException e) {
  11. // ... whatever
  12. }
  13. }

但是很明显,由于配置文件夹不在类路径中,所以这不起作用。现在我找不到一个简单的方法。我最喜欢的是这样:

  1. @InjectProperties("my.properties")
  2. protected Properties props;

到目前为止,我在web上找到的唯一解决方案是制作自己的osgi模块,但我相信一定有更简单的方法(没有osgi的方法!)。有人能告诉我怎么做吗?

sc4hvdpw

sc4hvdpw1#

下面是一个使用cdi的完整示例,来自这个站点。
在wildfly配置文件夹中创建并填充属性文件

  1. $ echo 'docs.dir=/var/documents' >> .standalone/configuration/application.properties

向wildfly配置文件添加系统属性。

  1. $ ./bin/jboss-cli.sh --connect
  2. [standalone@localhost:9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties)

这将向服务器配置文件(standalone.xml或domain.xml)添加以下内容:

  1. <system-properties>
  2. <property name="application.properties" value="${jboss.server.config.dir}/application.properties"/>
  3. </system-properties>

创建加载和存储应用程序范围属性的单例会话bean

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Properties;
  7. import javax.annotation.PostConstruct;
  8. import javax.ejb.Singleton;
  9. @Singleton
  10. public class PropertyFileResolver {
  11. private Logger logger = Logger.getLogger(PropertyFileResolver.class);
  12. private String properties = new HashMap<>();
  13. @PostConstruct
  14. private void init() throws IOException {
  15. //matches the property name as defined in the system-properties element in WildFly
  16. String propertyFile = System.getProperty("application.properties");
  17. File file = new File(propertyFile);
  18. Properties properties = new Properties();
  19. try {
  20. properties.load(new FileInputStream(file));
  21. } catch (IOException e) {
  22. logger.error("Unable to load properties file", e);
  23. }
  24. HashMap hashMap = new HashMap<>(properties);
  25. this.properties.putAll(hashMap);
  26. }
  27. public String getProperty(String key) {
  28. return properties.get(key);
  29. }
  30. }

创建cdi限定符。我们将在希望注入的java变量上使用这个注解。

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. import javax.inject.Qualifier;
  6. @Qualifier
  7. @Retention(RetentionPolicy.RUNTIME)
  8. @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR })
  9. public @interface ApplicationProperty {
  10. // no default meaning a value is mandatory
  11. @Nonbinding
  12. String name();
  13. }

创建生产者方法;这将生成要注入的对象

  1. import javax.enterprise.inject.Produces;
  2. import javax.enterprise.inject.spi.InjectionPoint;
  3. import javax.inject.Inject;
  4. public class ApplicaitonPropertyProducer {
  5. @Inject
  6. private PropertyFileResolver fileResolver;
  7. @Produces
  8. @ApplicationProperty(name = "")
  9. public String getPropertyAsString(InjectionPoint injectionPoint) {
  10. String propertyName = injectionPoint.getAnnotated().getAnnotation(ApplicationProperty.class).name();
  11. String value = fileResolver.getProperty(propertyName);
  12. if (value == null || propertyName.trim().length() == 0) {
  13. throw new IllegalArgumentException("No property found with name " + value);
  14. }
  15. return value;
  16. }
  17. @Produces
  18. @ApplicationProperty(name="")
  19. public Integer getPropertyAsInteger(InjectionPoint injectionPoint) {
  20. String value = getPropertyAsString(injectionPoint);
  21. return value == null ? null : Integer.valueOf(value);
  22. }
  23. }

最后,将属性注入一个cdibean

  1. import javax.ejb.Stateless;
  2. import javax.inject.Inject;
  3. @Stateless
  4. public class MySimpleEJB {
  5. @Inject
  6. @ApplicationProperty(name = "docs.dir")
  7. private String myProperty;
  8. public String getProperty() {
  9. return myProperty;
  10. }
  11. }
展开查看全部
3phpmpom

3phpmpom2#

如果要从配置目录显式读取文件(例如。 $WILDFLY_HOME/standalone/configuration 或者 domain/configuration )有一个包含路径的系统属性。简单地做 System.getProperty("jboss.server.config.dir"); 并将您的文件名附加到该文件以获取该文件。
你不会把它当作资源来读,所以。。。

  1. String fileName = System.getProperty("jboss.server.config.dir") + "/my.properties";
  2. try(FileInputStream fis = new FileInputStream(fileName)) {
  3. properties.load(fis);
  4. }

然后文件将为您加载。
另外,由于wildfly不再提供osgi支持,我不知道创建osgi模块会对您有什么帮助。

7ajki6be

7ajki6be3#

为了避免这种问题,问题是设置 jboss.server.config.dir 在类似的vm参数中:

  1. -Djboss.server.config.dir="[jboss_repository]/server/[default-all-standard-standalone]/conf" server
x8goxv8g

x8goxv8g4#

如果在standalone.xml属性中有:

  1. <property name="my.properties" value="propertyValue"/>

你可以通过以下方式阅读:

  1. static final String MY_PROPERTY = System.getProperty("my.properties");

或者在web.xml中指定context param,例如:

  1. <context-param>
  2. <param-name>MyProperty</param-name>
  3. <param-value>MyPropertyValue</param-value>
  4. </context-param>

您可以在java bean中阅读:

  1. String myProperty= getServletContext().getInitParameter("MyProperty");
展开查看全部
nwlls2ji

nwlls2ji5#

  1. InputStream in = null;
  2. File confDir = new File(System.getProperty("jboss.server.config.dir"));
  3. File fileProp = new File(confDir, "my.properties");
  4. try{
  5. //teste fileProp.exists etc.
  6. in = new FileInputStream(fileProp);
  7. Properties properties = new Properties();
  8. properties.load(in);
  9. //You should throws or handle FileNotFoundException and IOException
  10. }finally{
  11. try{
  12. in.close();
  13. }catch(Exception ignored){
  14. }
  15. }
gg58donl

gg58donl6#

听起来您试图解决的问题是管理不同(但可能相似)的配置文件,以便在不同的环境(即生产环境、qa环境,甚至不同的客户)中运行应用程序。如果是这样的话,看看jfighttp://jfig.sourceforge.net/ . 它将消除在类路径之外存储属性文件的需要(但仍然可以)。
我们需要的是配置文件的分层方法。90%不变的配置值可以保存在基本文件中。其他百分之十(或更少)可以保存在他们自己独特的配置文件中。在运行时,这些文件彼此层叠,以提供灵活、可管理的配置。例如,在开发环境中,myhost.config.xml与dev.config.xml和base.config.xml组合在一起形成我的唯一配置。
然后可以在版本控制中维护每个配置文件,因为它们具有唯一的名称。当基值更改时,只需要修改基文件,很容易看出版本之间的差异。另一个主要好处是,对基本配置文件的更改将在部署之前进行彻底的测试。

sqougxex

sqougxex7#

你能做的最简单的事就是跑步 standalone.sh 用一个 -P 选项引用您的属性文件(您需要一个url file:/path/to/my.properties ,或将文件放入 $WILDFLY_HOME/bin ).
然后文件中的所有属性将作为系统属性加载。
要将配置属性注入到应用程序类中,请查看deltaspike配置,它支持不同的属性源,如系统属性、环境变量、jndi条目,并对应用程序隐藏特定的源。
或者,为了避免设置系统属性(对于部署到wildfly示例的所有应用程序来说,这些属性都是全局的),您还可以为deltaspike定义一个自定义属性源,以便从任何给定位置读取属性文件,并且这些属性将是应用程序的本地属性。

相关问题