讀取spring設置裝備擺設文件的辦法(spring讀取資本文件)。本站提示廣大學習愛好者:(讀取spring設置裝備擺設文件的辦法(spring讀取資本文件))文章只能為提供參考,不一定能成為您想要的結果。以下是讀取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設置裝備擺設方法讀取:
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會依據途徑主動轉換成對應的Resource -->
<bean id="resourceBean" class="com.springdemo.resource.ResourceBean" >
<property name="resource" value="classpath:/com/springdemo/resource/test.txt" ></property>
</bean>