Java讀取配置檔案的幾種方法以及路徑問題
阿新 • • 發佈:2019-01-26
在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據筆者工作中用到的讀取配置檔案的方法小小總結一下,主要敘述的是spring讀取配置檔案的方法。
一.讀取xml配置檔案
(一)新建一個java bean(HelloBean.java)
java 程式碼:
(二)構造一個配置檔案(beanConfig.xml)public class HelloBean { private String helloWorld; public String getHelloWorld() { return helloWorld; } public void setHelloWorld(String helloWorld) { this.helloWorld = helloWorld; } }
xml 程式碼:
<!---->xml version="1.0" encoding="UTF-8"?> <!---->> <beans> <bean id="helloBean" class="chb.demo.vo.HelloBean"> <property name="helloWorld"> <value>Hello!chb!value> property> bean> beans>
(三)讀取xml檔案
1.利用ClassPathXmlApplicationContext
java 程式碼:
ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");
HelloBean helloBean = (HelloBean)context.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
2.利用FileSystemResource讀取java 程式碼:
值得注意的是:利用FileSystemResource,則配置檔案必須放在project直接目錄下,或者寫明絕對路徑,否則就會丟擲找不到檔案的異常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());
二.讀取properties配置檔案
這裡介紹兩種技術:利用spring讀取properties 檔案和利用java.util.Properties讀取
(一)利用spring讀取properties 檔案我們還利用上面的HelloBean.java檔案,構造如下beanConfig.properties檔案:
properties 程式碼:
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
屬性檔案中的"helloBean"名稱即是Bean的別名設定,.class用於指定類來源。然後利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader來讀取屬性檔案
java 程式碼:
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());
(二)利用java.util.Properties讀取屬性檔案比如,我們構造一個ipConfig.properties來儲存伺服器ip地址和埠,如:
properties 程式碼
ip=192.168.0.1
port=8080
則,我們可以用如下程式來獲得伺服器配置資訊:
java 程式碼:
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"));
(三)利用Spring註解讀取屬性檔案
第一步:在applicationContext.xml配置
<!-- 載入屬性配置檔案 -->
<context:property-placeholder location="classpath:config/config.properties"/>
第二步:建立配置檔案內容report.page.image=E\:/resource/template/image/cover/logo/
第三步:在Controller中使用註解獲得配置項內容/** 報告封面圖片路徑 */
@Value("${report.page.image}")
private String reportPageImage;
public String getReportPageImage() {
return reportPageImage;
}
public void setReportPageImage(String reportPageImage) {
this.reportPageImage = reportPageImage;
}
第四步:直接使用reportPageImage這個私用變數獲取配置檔案的內容/** 公共屬性配置類 */
@Resource
public PublicConfig publicConfig;
String imagePath = publicConfig.getReportPageImage();
三.關於JAVA讀取配置檔案的路徑問題
在軟體開發中經常遇到讀取配置檔案,以及檔案定位問題。今天做個總結。1、在java project中的配置檔案讀取
在java project中的配置檔案讀取java project專案的目錄結構只有兩個src和bin。src中存放的是原始檔
bin中存放的是*.class的位元組碼檔案。
原始檔進過編譯就放進bin目錄中,其中也包括沒有編譯的配置檔案。我們需要的是對bin中的配置檔案進行讀取,因為專案最終打包成*.jar的就是bin目錄下的檔案。採用相對路勁進行讀取的時候,需要確定此時相對的是哪一個基路徑。其實這個基路徑就是java.io預設定位到的當前使用者目錄("user.dir")(即工程根目錄)。相對於這個路徑在定位我們的配置檔案。
如下所示:
File file=new File("test/login.conf");
JVM就可以據"user.dir"與"test/login.conf" 得到完整的路徑(即絕對路徑)"D:\DecisionTree\test\login.conf",從而找到login.conf檔案。
通常我們使用“類名”.class.getClassLoader().getResource("login.conf").openStream()來載入classes目錄下的配置檔案。得到檔案的輸入流。
“類名”.class.getClassLoader().getResource("")定位到的絕對路徑是WEB-INF\classes目錄下。getResource(String filepath)。引數filepath是classes目錄下的相對路徑。
2、在java web project中的配置檔案讀取
如果我們把配置檔案定義在了classpath路徑下。也就是WEB-INF\classes路徑下,那麼就可以像讀取java project中的配置檔案那樣讀取了。如果我們把配置檔案定義在了WEB-INF的直接目錄下。而不是classes目錄下,那麼是classpath之外的路徑了,通過上面的那種方式是讀取不到了。
這是在應用環境下,要用到ServletContext
ServletActionContext.getServletContext().getResource("/")可以定位到當前web應用目錄 如:jndi:/server/TEST/ TEST為當前的web應用系統名。getResource(String filepath) 注意其中filepath必須以“/”開始。
ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/config/login.conf");就可以得到/WEB-INF/config/login.conf檔案的輸入流了。