java gets the configuration file under classes

public static void main(String[] args) {
    // TODO Auto-generated method stub
     Properties prop = new Properties();// 属性集合对象   
     FileInputStream fis;
    try {
         InputStream ra = TestPath.class.getClassLoader().getResourceAsStream("conf/resource.properties");  
        //fis = new FileInputStream("conf/prop.properties");
         prop.load(ra);// 将属性文件流装载到Properties对象中   
         ra.close();// 关闭流   
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }// 属性文件输入流   

    String STR_PATH=prop.getProperty("STR_PATH");
    System.out.println("文件夹路径--》"+STR_PATH);

}

If it is a web program that has already started the spring container first, you can use the way that spring supports @value annotation to get it. First, add it to
the spring configuration file and then in the code

<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:conf/*.properties" />



@Value("${REDIS_TIME}")
private Integer REDIS_TIME;

One thing to note is that in a multi-module web project, such as a web project, there is also a background module that is packaged as a jar package, so the configuration file of this background module should not have the same name as the web configuration file of the web project. If all are set to resource.properties, then when the background module reads the resource, it will read the resource file of the web, so that the content of the resource file required by the background module cannot be found! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901050&siteId=291194637