1. 程式人生 > >spingmvc的外置properties文件讀取(java循環利用properties內容)

spingmvc的外置properties文件讀取(java循環利用properties內容)

bank return nco ping throw ssp per exce oid

既然已經有了applicationContext.xml的properties路徑,java不必再properties路徑。

applicationContext.xml

改前:

<!-- properties -->

<context:property-placeholder location="file:/sb/env.properties" />

改後:

<!-- properties -->
<bean class="jp.co.softbank.faqapi.common.conf.AppConfig">
  <property name="locations">
    <list>
      <value>file:/sb/env.properties</value>
    </list>
  </property>
</bean>

java文件:

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.stereotype.Component;

/**
* コンフィギュレーション.
*/
@Component
public class AppConfig extends PropertyPlaceholderConfigurer {

  private Properties properties;

  @Override
  protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties properties)throws BeansException {
    super.processProperties(beanFactoryToProcess, properties);
    this.properties = properties;
  }

  /**
  * ENVのプロパティ取得
  * @param key キー
  * @return 値
  */
  public String getEnvProperty(String key){
    return properties.getProperty(key);
  }

}

spingmvc的外置properties文件讀取(java循環利用properties內容)