加载文件的几种方式

  一、通过文件路径加载
  
  InputStream ips = new FileInputStream("config.properties");
  
  二、直接通过getResourceAsStream进行加载
  
  //配置文件需要放在当前包目录下
  
  InputStream ips = ReflectCollection.class
  
  .getResourceAsStream("resources/config.properties");
  
  这种方式也可以使用绝对路径,绝对路径需要加上斜杠(/)。不管是绝对还是相对路径,底层调用都是类加载器。
  
  //绝对路径的方式
  
  InputStream ips = ReflectCollection.class
  
  .getResourceAsStream(
  
  "/cn/sunft/day01/reflect/resources/config.properties");
  
  三、通过类加载的方式进行加载
  
  在classpath所在的根目录下查找文件,文件需要直接放在classpath指定的目录下,另外目录最前面不要加斜杠(/)。
  
  //在classpath所在的根目录下查找文件
  
  //注意这里的文件需要直接放在classpath指定的目录下,
  
  //另外目录最前面不要加/,通过这种方式文件通常不需要修改
  
  InputStream ips = ReflectCollection.class
  
  .getClassLoader()
  
  .getResourceAsStream("cn/sunft/day01/reflect/config.properties")
  
  四、Spring加载Properties配置文件
  
  一、通过 context:property-placeholder 标签实现配置文件加载
  
  <context:property-placeholder ignore-unresolvable="true" location="classpath:redis-key.properties"/>
  
  2、在 spring.xml 中使用配置文件属性:
  
  <!-- 基本属性 url、user、password -->
  
  <property name="url" value="${jdbc.url}" />
  
  <property name="username" value="${jdbc.username}" />
  
  <property name="password" value="${jdbc.password}" />
  
  3、在java文件中使用:
  
  @Value("${jdbc_url}")
  
  private String jdbcUrl; // 注意:这里变量不能定义成static
  
  二、通过 util:properties 标签实现配置文件加载
  
  1、用法示例: 在spring.xml配置文件中添加标签
  
  <util:properties id="util_Spring" local-override="true" location www.frgjyL.cn="classpath:jeesite.properties"/>
  
  2、在spring.xml 中使用配置文件属性:
  
  <property name="username" value="#{util_Spring['jdbc.username']}" />
  
  <property name="password" value="#{util_Spring['jdbc.password']}" />
  
  3、在java文件中使用:
  
  @Value(value="#{util_Spring['UTIL_SERVICE_ONE']}")
  
  private String UTIL_SERVICE_ONE;
  
  三、通过 @PropertySource 注解实现配置文件加载
  
  1、用法示例:在java类文件中使用 PropertySource 注解:
  
  @PropertySource(value={www.huishenggw.cn"classpath:redis-key.properties"})
  
  public class ReadProperties {
  
  @Value(value="${jdbc.username}"www.chengmyuLegw.cn)
  
  private String USER_NAME;
  
  }
  
  2、在java文件中使用:
  
  @Value(value="${jdbc.username}")
  
  private String USER_NAME;
  
  四、通过 PropertyPlaceholderConfigurer 类读取配置文件
  
  1、用法示例:在 spring.xml 中使用 <bean>标签进行配置
  
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  
  <property name="locations">
  
  <list>
  
  <value>classpath:redis-key.properties</value>
  
  </list>
  
  </property>
  
  </bean>
  
  2、 PropertyPlaceholderConfigurer 配置方法,等价于 方式一,用法参考方法一。
  
  <!-- 基本属性 url、user、www.honglpt.cn password -->
  
  <property name="url"www.cmyLgw.cn value="${jdbc.url}" />
  
  <property name="username" value="${jdbc.username}" />
  
  <property name="password" value="${jdbc.password}" />

转载于:https://www.cnblogs.com/qwangxiao/p/10960278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值