java读取配置文件.properties数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_17522211/article/details/84559885

util.properties

testkey=test123

java_web:

import java.util.Properties;
import org.springframework.stereotype.Component;

/**
 * 获取配置文件信息
 */
@Component
public class PropertiesUtil {
	public static void main(String args[]){
		System.err.println(getProperties("util.properties", "testkey"));
	}
	/**
	 * 获取配置文件的value
	 * @param file : 配置文件名称
	 * @param key : 配置文件内容的key
	 * @return : 返回value
	 */
	public static String getProperties(String file,String key){
		try{
			Properties prop = new Properties();
			prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(file));
			return new String(prop.getProperty(key).getBytes("ISO-8859-1"),"UTF-8");
		}catch(Exception e){
			return "";
		}
	}
}

springboot:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DBPayConfig {
	@Value("${testkey}")
	private String testkey;
}

猜你喜欢

转载自blog.csdn.net/qq_17522211/article/details/84559885