讀取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ù)路徑自動轉換成對應的Resource -->
<bean id="resourceBean" class="com.springdemo.resource.ResourceBean" >
<property name="resource" value="classpath:/com/springdemo/resource/test.txt" ></property>
</bean>
相關文章
spring cloud中微服務之間的調用以及eureka的自我保護機制詳解
這篇文章主要介紹了spring cloud中微服務之間的調用以及eureka的自我保護機制詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Java實現(xiàn)Excel百萬級數(shù)據(jù)導入功能的示例代碼
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)Excel百萬級數(shù)據(jù)導入功能,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下2024-04-04
SpringBoot實現(xiàn)嵌入式 Servlet容器
傳統(tǒng)的Spring MVC工程部署時需要將WAR文件放置在servlet容器的文檔目錄內(nèi),而Spring Boot工程使用嵌入式servlet容器省去了這一步驟,本文就來設置一下相關配置,感興趣的可以了解一下2023-12-12

