web項目讀取資源目錄下的.properties配置文件

package org.springframework.beans;

import java.util.Properties;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


public class StudentTest {
	
	@Test
	public void getProperties()throws Exception{
        //資源文件位置,resources目錄下
		String path = "student.properties";
        //方法一:
		Properties p1 = PropertiesLoaderUtils.loadAllProperties(path);
        Object name = p1.get("name");
        
        //方式二
		Resource resource = new ClassPathResource(path);
		Properties p2 = PropertiesLoaderUtils.loadProperties(resource);
		Object object = p2.get("name");
	}
	
	
	
    

}

 

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