DIV CSS 佈局教程網

Java 讀取配置檔的完整做法:Spring XML、properties 與原生 Properties
編輯:JavaScript綜合知識     

專案裡總有一些不想寫死在程式碼中的設定:伺服器位址、連接埠、開關參數。把這些放進配置檔是標準做法。以下整理四種常見的讀取方式,並指出各自的適用時機與陷阱。

準備一個 JavaBean

四種做法都會用到同一個目標物件:

package chb.demo.vo;

public class HelloBean {
  private String helloWorld;

  public String getHelloWorld() {
    return helloWorld;
  }

  public void setHelloWorld(String helloWorld) {
    this.helloWorld = helloWorld;
  }
}

做法一:用 ClassPathXmlApplicationContext 讀取 XML

最直覺的方式,適合配置檔放在 classpath 上的情況:

ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");
HelloBean helloBean = (HelloBean) context.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());

做法二:用 FileSystemResource 讀取 XML

需要從檔案系統明確路徑載入時使用:

Resource rs = new FileSystemResource(
  "D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (HelloBean) factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());

要特別注意:用 FileSystemResource 時,配置檔必須放在專案直接目錄下,或者寫出絕對路徑,否則會丟出找不到檔案的例外。相對路徑的解析基準是 JVM 的啟動目錄,換環境部署時很容易出錯,因此正式環境建議優先用 classpath 方式。

對照用的 XML 配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "https://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="helloBean" class="chb.demo.vo.HelloBean">
    <property name="helloWorld">
      <value>Hello!chb!</value>
    </property>
  </bean>
</beans>

做法三:用 Spring 讀取 properties

先準備屬性檔,命名規則是「別名.class 指定類別來源,其餘行是屬性」:

helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!

然後用 PropertiesBeanDefinitionReader 載入:

BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));
BeanFactory factory = (BeanFactory) reg;
HelloBean helloBean = (HelloBean) factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());

其中的 helloBean 就是 Bean 的別名設定,.class 用於指定類別來源。

做法四:用 java.util.Properties(最常用)

如果只是讀幾個設定值,不需要 Spring 的 Bean 生命週期,直接用 JDK 的 Properties 最簡單。假設有個 ipConfig.properties:

ip=192.168.0.1
port=8080

讀取程式:

InputStream inputStream = this.getClass().getClassLoader()
  .getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
try {
  p.load(inputStream);
} catch (IOException e1) {
  e1.printStackTrace();
}
System.out.println("ip:" + p.getProperty("ip") + ",port:" + p.getProperty("port"));

用 getClassLoader().getResourceAsStream() 而非絕對路徑,是讓程式碼在不同部署環境都能找到檔案的關鍵。

怎麼選

  • Spring XML:適合需要容器管理生命週期、注入依賴的元件設定。
  • Spring properties 載入 Bean 定義:Bean 數量多且希望用屬性檔集中管理時適用。
  • 原生 Properties:純設定值讀取的首選,不引入框架依賴,測試也容易。
  • YAML:Spring Boot 專案的原生選擇,支援巢狀結構與型別轉換,可讀性優於平坦的 properties。

三個實務提醒

  • 使用 try-with-resources:InputStream 一定要關閉,寫成 try (InputStream in = ...) 可避免漏關造成的檔案描述符耗盡。
  • 敏感資訊不要進版控:密碼、金鑰應使用環境變數或專用的設定中心,而非提交到原始碼庫。
  • 注意編碼:properties 檔在 Java 8 之前預設以 ISO-8859-1 讀取,中文值會亂碼。需要中文時,可改用 Reader 指定 UTF-8,或直接使用 YAML 搭配 Spring Boot 的設定機制。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved