在应用程序之外放置配置文件的位置

时间:2016-11-22 17:05:45

标签: java spring jboss

我有一个Spring应用程序,在启动时需要从文件中读取一些基本属性但不敏感(超时值,目录位置等)。需要在启动应用程序之前对其进行编辑,具体取决于所需的目标服务器。

我的第一个简单想法是将文件放在主目标服务器下,然后将其加载到Spring

<context:property-placeholder location="file:${JBOSS_HOME}/standalone/config/application.properties" />.

我发现其他来源提到System propertiesJBoss modules的使用情况。

使用彼此有任何优点/缺点吗?在我的案例中选择合适的一个时我还应该考虑什么?

1 个答案:

答案 0 :(得分:0)

根据我的理解,Spring会在应用程序目录中查看默认属性文件,或者您可以使用XML配置或Java配置(注释)注册属性文件:

<context:property-placeholder location="classpath*:my.properties"/>
Then you refer to the properties in your beans:
@Component
class MyClass {
  @Value("${my.property.name}")
  private String[] myValues;
}

@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
   @Autowired
   Environment env;

   @Bean
   public TestBean testBean() {
       TestBean testBean = new TestBean();
       testBean.setName(env.getProperty("testbean.name"));
       return testBean;
   }
}

可以在apps服务器,tomcat,jboss等中随时更新属性文件(只是停止serv)。 请注意确定这是否是您想要的,

相关问题