栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

读取spring配置文件的方法(spring读取资源文件)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

读取spring配置文件的方法(spring读取资源文件)

1.spring配置文件

复制代码 代码如下:
         class="org.springframework.beans.factory.config.PropertiesFactoryBean">
         
   

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配置:

复制代码 代码如下:
 
 
  
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152440.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号