Best ways to deal with properties values in XML file in Spring, Maven and Eclipses

前端 未结 2 1218
失恋的感觉
失恋的感觉 2020-12-13 10:48

I am working on a Spring WebFlow project which has a lot of property values in XML files, as any Spring programmer knows. I have database user names, password, URLs, etc.

相关标签:
2条回答
  • 2020-12-13 11:15

    Most SA are more willing and confident to deal with .properties file rather than .xml.

    Spring provide PropertyPlaceholderConfigurer to let you define everything into one or several .properties file and substitute the placeholder in applicationContext.xml.

    Create a app.properties under src/main/resources/ folder:

    ... ...
    
    # Dadabase connection settings:
    jdbc.driverClassName=org.postgresql.Driver
    jdbc.url=jdbc:postgresql://localhost:5432/app_db
    jdbc.username=app_admin
    jdbc.password=password
    
    ... ...
    

    And use PropertyPlaceholderConfigurer in applicationContext.xml like so:

    ... ...
    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location">
        <value>app.properties</value>
      </property>
    </bean>
    
    ... ...
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
    </bean>
    

    Check out Spring PropertyPlaceholderConfigurer Example for more details.

    In addition, from application deployment perspective, we usually package app in some executable format and the .properties files are usually packed inside the executable war or ear file. A simple solution is to configure your PropertyPlaceholderConfigurer bean to resolve properties from multiple location in a pre-defined order, so in the deployment environment, you can use a fixed location or environment variable to specify the properties file, also note that in order to simplify the deploy/configure task for SA, we usually use a single external .properties file define all runtime configuration, like so:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
        <list>
          <!-- Default location inside war file -->
          <value>classpath:app.properties</value>
          <!-- Environment specific location, a fixed path on server -->
          <value>file:///opt/my-app/conf/app.properties</value>
        </list>
      </property>
      <property name="ignoreResourceNotFound" value="true"/>
    </bean>
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-13 11:21

    Also you can define a propertyConfigurer programmatically in configuration class:

    @Configuration
    @PropertySource("classpath:application.properties")
    public class PropertiesConfiguration {
    
        @Bean
        public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(Environment env) {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            configurer.setEnvironment(env);
            return configurer;
        }
    }
    
    0 讨论(0)
提交回复
热议问题