讀取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"));
另一個(gè)朋友提供的讀取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配置方式讀取:
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配置:
<!-- 可以直接將一個(gè)文件路徑賦值給Resource類型的resource屬性,spring會(huì)根據(jù)路徑自動(dòng)轉(zhuǎn)換成對(duì)應(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)文章
JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)原理解析
這篇文章主要介紹了JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
spring cloud中微服務(wù)之間的調(diào)用以及eureka的自我保護(hù)機(jī)制詳解
這篇文章主要介紹了spring cloud中微服務(wù)之間的調(diào)用以及eureka的自我保護(hù)機(jī)制詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Java實(shí)現(xiàn)短信驗(yàn)證碼詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)短信驗(yàn)證碼的相關(guān)資料, 在業(yè)務(wù)需求中我們經(jīng)常會(huì)用到短信驗(yàn)證碼,比如手機(jī)號(hào)登錄、綁定手機(jī)號(hào)、忘記密碼、敏感操作等,需要的朋友可以參考下2023-09-09
Java實(shí)現(xiàn)Excel百萬(wàn)級(jí)數(shù)據(jù)導(dǎo)入功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)Excel百萬(wàn)級(jí)數(shù)據(jù)導(dǎo)入功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-04-04
SpringBoot實(shí)現(xiàn)嵌入式 Servlet容器
傳統(tǒng)的Spring MVC工程部署時(shí)需要將WAR文件放置在servlet容器的文檔目錄內(nèi),而Spring Boot工程使用嵌入式servlet容器省去了這一步驟,本文就來設(shè)置一下相關(guān)配置,感興趣的可以了解一下2023-12-12
java調(diào)用Oracle存儲(chǔ)過程的方法實(shí)例
這篇文章介紹了java調(diào)用Oracle存儲(chǔ)過程的方法實(shí)例,有需要的朋友可以參考一下2013-09-09
詳解如何實(shí)現(xiàn)SpringBoot的底層注解
今天給大家?guī)淼奈恼率侨绾螌?shí)現(xiàn)SpringBoot的底層注解,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴很有幫助,需要的朋友可以參考下2021-06-06

