国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

讀取spring配置文件的方法(spring讀取資源文件)

2019-11-26 15:40:20
字體:
供稿:網(wǎng)友

1.spring配置文件

復(fù)制代碼 代碼如下:

<bean id="configproperties"
         class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="location" value="classpath:jdbc.properties"/>
    </bean>

2.讀取屬性方法

復(fù)制代碼 代碼如下:

ApplicationContext c=new ClassPathXmlApplicationContext("classpath:applicationContext-datasource.xml");
Properties p=(Properties)c.getBean("configproperties");
System.out.println(p.getProperty("jdbcOrcale.driverClassName"));


另一個朋友提供的讀取spring配置文件的方法,也分享一下吧

直接讀取方式:
復(fù)制代碼 代碼如下:

public void test() throws IOException
 {
  Resource resource = ApplicationContextFactory.getApplicationContext().getResource("classpath:com/springdemo/resource/test.txt");

  File file = resource.getFile();
  byte[] buffer =new byte[(int) file.length()];
  FileInputStream is =new FileInputStream(file);

  is.read(buffer, 0, buffer.length);

  is.close();
  String str = new String(buffer);
  System.out.println(str);

 }

通過spring配置方式讀取:

復(fù)制代碼 代碼如下:

package com.springdemo.resource;

import org.springframework.core.io.Resource;

public class ResourceBean {

 private Resource resource;

 public Resource getResource() {
  return resource;
 }

 public void setResource(Resource resource) {
  this.resource = resource;
 }
}

spring bean配置:

復(fù)制代碼 代碼如下:

 <!-- 可以直接將一個文件路徑賦值給Resource類型的resource屬性,spring會根據(jù)路徑自動轉(zhuǎn)換成對應(yīng)的Resource -->
 <bean id="resourceBean" class="com.springdemo.resource.ResourceBean" >
  <property name="resource" value="classpath:/com/springdemo/resource/test.txt" ></property>
 </bean>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 池州市| 宁晋县| 鲁山县| 商河县| 扎兰屯市| 聂拉木县| 丰原市| 安阳市| 巴林左旗| 平谷区| 抚顺市| 宁海县| 定远县| 怀来县| 陕西省| 自治县| 扬中市| 江津市| 竹溪县| 巩留县| 孟村| 兴隆县| 陆良县| 墨竹工卡县| 黄龙县| 衡南县| 南通市| SHOW| 四川省| 左贡县| 交城县| 同仁县| 化德县| 东莞市| 深州市| 安丘市| 驻马店市| 乌兰察布市| 仙桃市| 永济市| 上饶市|