讀取spring配置文件的方法(spring讀取資源文件)
1.spring配置文件
<bean id="configproperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
2.讀取屬性方法
ApplicationContext c=new ClassPathXmlApplicationContext("classpath:applicationContext-datasource.xml");
Properties p=(Properties)c.getBean("configproperties");
System.out.println(p.getProperty("jdbcOrcale.driverClassName"));
另一個朋友提供的讀取spring配置文件的方法,也分享一下吧
直接讀取方式:
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配置方式讀?。?BR>
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配置:
<!-- 可以直接將一個文件路徑賦值給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>
相關(guān)文章
如何使用 Spring Boot 和 Canal 實現(xiàn) My
本文介紹了如何使用SpringBoot和Canal實現(xiàn)MySQL數(shù)據(jù)庫之間的數(shù)據(jù)同步,通過配置主庫、創(chuàng)建Canal用戶、配置CanalServer以及開發(fā)SpringBoot客戶端,實現(xiàn)了將主庫的數(shù)據(jù)實時同步到多個從庫,感興趣的朋友跟隨小編一起看看吧2025-02-02如何解決UnsupportedOperationException異常問題
這篇文章主要介紹了如何解決UnsupportedOperationException異常問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05完美解決gson將Integer默認(rèn)轉(zhuǎn)換成Double的問題
下面小編就為大家?guī)硪黄昝澜鉀Qgson將Integer默認(rèn)轉(zhuǎn)換成Double的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03spring boot jpa寫原生sql報Cannot resolve table錯誤解決方法
在本篇文章里小編給大家整理的是關(guān)于spring boot jpa寫原生sql報Cannot resolve table錯誤的解決方法,需要的朋友學(xué)習(xí)下。2019-11-11