Java讀取外部(第三方)jar包配置文件

讀取外部jar包配置文件

涉及到框架封裝,或其他用途時,可能會要讀取外部jar包的配置文件 , 使用方式記錄如下。

spring-boot  、 spring 項目同樣適用,最主要路徑寫法是,classpath:/jdbc.properties ,不能用classpath*:/jdbc.properties,外部jar包配置文件在classpath路徑下,具體代碼如下

Resource resource = resourceLoader.getResource("classpath:/jdbc.properties");

package com.example.mongodemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.Properties;

@SpringBootApplication
public class MongoDemoApplication {

	public static void main(String[] args) throws IOException {
		SpringApplication.run(MongoDemoApplication.class, args);
        
           /*重點讀取代碼*/
		ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
		Resource resource = resourceLoader.getResource("classpath:/jdbc.properties");
		Properties inPro = PropertiesLoaderUtils.loadProperties(resource);
		System.out.println(inPro.getProperty("jdbc.url"));
	}

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章