zoukankan      html  css  js  c++  java
  • java读取项目中模板文件(src/main/resources)

    在springboot项目的开发中,当需要读取src/下的配置文件时,该怎么做?

    Resources下有一个文件名为acceptsNum.xls的模板文件 

    1.在java类中读取
    若配置文件不在src/main/resources目录下,可以直接使用

    1. Properties prop = new properties();  
    2. prop.load(new InputStream("acceptsNum.xls"));  

    当配置文件放在src/main/resources的目录下时,只能使用Class.getResourceAsStream()方法来加载

     
    1. Properties prop = new properties();  
    2. prop.load(this.getClass().getResourceAsStream("/acceptsNum.xls"));  
    InputStream is = null;
    is = this.getClass().getResourceAsStream("/static/xls/acceptsNum.xls");

    此时,getResourceAsStream(String name)方法中参数路径的写法:

    1).若写成"acceptsNum.xls",则是去当前类的class文件同一目录下找(但是显然在正常项目不会有人将配置文件放在这种位置)。

    2).若写成"/acceptsNum.xls",则是去整个项目的classes目录下去找,即target/classes

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    2.在spring框架的xml文件中读取配置文件kafka.properties

     

    以下就有两种方法来调用

    1).首先可以在spring的bean中配置

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

    <property name="locations">  

    <list

    <span style="white-space:pre">  </span><value>/kafka.properties</value

    </list>  

    </property

    </bean>  

    这里还可以在list标签中配置多个value,这样就可以在bean中读取一个甚至多个配置文件。

     
    1. <bean id="kafkaService" class="com.wws.service.impl.KafkaServiceImpl">  
    2.     <!-- <property name="topic"><value>topic</value></property> -->  
    3.     <property name="topic"><value>${kafka.topic}</value></property>  
    4. </bean>  

    这样就可以在后面的bean中成功调用配置文件中的参数,以上被注释的那段property和被注释掉的那行是同样效果
    2).或者也可以使用如下方法

     
    1. <context:property-placholder location="classpath:kafka.properties"/>  

    直接在spring配置文件中配置context:property-placeholder,有多个配置文件可以用逗号隔开,例如

      1. <context:property-placeholder location="classpath:kafka.properties,classpath:jdbc.properties"/> 
    当能力支撑不了野心时,就该静下心来学习!
  • 相关阅读:
    flex兼容写法
    多行文字,最后一行省略号(适用于移动端)
    checkbox样式修改
    响应式布局
    微信常用的页面跳转
    css小技巧(清除滚动条)
    JS学习---PHP浅识
    qml 画页迁移
    list滚动条Scroll 偏移和长度计算公式总结
    qml listview关键字高亮
  • 原文地址:https://www.cnblogs.com/1234cjq/p/7798844.html
Copyright © 2011-2022 走看看