读取spring配置文件的方法(spring读取资源文件)
发布时间:2022-10-08 11:24:21
标签:java,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>
0
投稿
猜你喜欢
- 一、前言在学习分治算法之前,问你一个问题,相信大家小时候都有存钱罐的经历,父母亲人如果给钱都会往自己的宝藏中存钱,我们每隔一段时间都会清点清
- 首先下载newtonsoft.json,增加引用using newtonsoft.json.linq;把jobject的内容提取出来,job
- 判断是否为飞行模式: boolean isAirplaneMode = Settings.System.getInt(mConte
- 本文实例为大家分享了Java基于Socket实现简易版多人聊天室的具体代码,供大家参考,具体内容如下一、 聊天室需求1、一个服务端,多个客户
- 好程序员Java教程分享MyBatis Plus介绍:1.MyBatis Plus 介绍MyBatis Plus 是国内人员开发的 MyBa
- 在前面的文章中也有关于 HorizontalScrollView 的使用:Android使用HorizontalScrollView实现水平
- 概念Java中的集合就是一种容器,可以容纳不同种类的数据,这些容纳是建立在未知的基础上。优点1.可以动态保存任意多个对象,使用比较方便。2.
- Java继承方法重写是Java语言多态的特性,必须满足以下条件在子类中,方法名称与父类方法名称完全相同方法的参数个数和类型完全相同,返回类型
- 在Springboot中默认的静态资源路径有:classpath:/METAINF/resources/,classpath:/resour
- Consul是什么Consul是一个基于HTTP的服务发现工具,用于配置和管理系统和服务之间的依赖关系。它提供了一个简单的方式来注册、发现和
- 本文实例为大家分享了C#实现订单管理程序的具体代码,供大家参考,具体内容如下订单管理的控制台程序,能够实现添加订单、删除订单、修改订单、查询
- System_Server进程运行在system server进程中的服务比较多,这是整个android框架的基础Native服务Surfa
- 当屏幕转动切换的时候 Android 机制是:销毁当前屏幕的 Activity ,然后重新开启一个新的适应屏幕改变的 Activity 。那
- java就业前需要掌握的专业技能1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、
- 有很多制作精良的APP都自带点击音效,那么如何简单的来实现这一效果,这里需要使用到的一个概念叫做SoundPool,这个类主要用于播放一些比
- 本文实例为大家分享了Unity3D Ui利用shader添加效果的具体代码,供大家参考,具体内容如下// Upgrade NOTE: rep
- 本文实例讲述了C#实现多线程的Web代理服务器。分享给大家供大家参考。具体如下:/**Proxy.cs:C# Programming Tip
- 在Android tv中的开发中,经常要跟焦点打交道,一个常见的需求是要有焦点记忆功能,焦点移动到列表中的某一项中,焦点移出去,在回来时焦点
- 1. kotlin 数值型fun main() { // 整数型 val a: Byte
- 要求:如下图,使用线程操作 1、实时显示当前时间 2、输入加数和被加数,自动出现结果 分析:两个问题解决的方式一致,使用子线程进