導航:首頁 > 編程語言 > java獲取bean的屬性

java獲取bean的屬性

發布時間:2024-09-24 15:07:26

java 怎麼讀取配置文件

一.讀取xml配置文件
(一)新建一個java bean(HelloBean. java)

java代碼
(二)構造一個配置文件(beanConfig.xml)
xml 代碼
(三)讀取xml文件
1.利用

java代碼
2.利用FileSystemResource讀取
java代碼
二.讀取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.來讀取屬性文件

java代碼

(二)利用java.util.Properties讀取屬性文件
比如,我們構造一個ipConfig.properties來保存伺服器ip地址和埠,如:
properties 代碼
ip=192.168.0.1
port=8080
三.讀取位於Jar包之外的properties配置文件

下面僅僅是列出讀取文件的過程,剩下的解析成為properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");

2 num = reader.read(byteStream);

3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);

四.要讀取的配置文件和類文件一起打包到一個Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //獲取當前Jar文件名,並對其解碼,防止出現中文亂碼
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名

修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //寫配置文件
。。。

out.close();

閱讀全文

與java獲取bean的屬性相關的資料

熱點內容
安卓系統藍牙耳機如何用 瀏覽:719
為什麼微信不能給appstore充值 瀏覽:493
程序員的保護動物 瀏覽:272
程序員遇到問題去哪個網站 瀏覽:529
安卓手機空格鍵連續輸入怎麼取消 瀏覽:518
壓縮空氣管道流量計 瀏覽:562
ug編程高級教程 瀏覽:175
什麼叫做伺服器已滿 瀏覽:37
暑假哪有教演算法的 瀏覽:136
密碼學的根基是加密 瀏覽:662
stata方差檢驗命令 瀏覽:337
解壓後文件夾里的內容丟失 瀏覽:715
解壓無敵視頻 瀏覽:690
什麼是伺服器辨認不了 瀏覽:129
java如何調用類方法 瀏覽:483
管理孩子的app叫什麼 瀏覽:546
壓縮活動軌跡 瀏覽:674
6米梁加密筋 瀏覽:79
怎麼學好ps如何學好編程 瀏覽:301
c編譯器廠商 瀏覽:114