简体   繁体   中英

Where to place a configuration file outside application war

I have a Spring application that at startup needs to read some basic properties from a file but nothing sensitive (timeout values, directory locations, etc.). It needs to be edited before starting the application depending on the desired target server.

My first easy idea was to place the file under home target server, then load it into Spring

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

I found other sources mentioning the usage of System properties or JBoss modules .

Are there any advantages/disadvantages for using one on another? What else should I consider when choosing the appropriate one in my case?

In my understanding Spring looks by default properties file in your app directory, or you can register a properties file with XML config or Java config (Annotations):

<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;
}

or

@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;
   }
}

the properties file can be updated at any time (just stoping serv) in the apps server, tomcat, jboss, etc. note sure if that's what you want,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM