一、spring标签读取
@Value读取properties文件并转成map
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.*;
/**
* Created by qxr4383 on 2019/6/4.
*/
@Component
@Data
public class WeatherPropertiesInit {
@Value("classpath:zh_CN.properties")
private Resource zhResource;
private static Map<String, String> weather_zh;
@PostConstruct
public void initWeatherProperties() throws Exception {
try{
Properties properties = new Properties();
properties1.load(zhResource.getInputStream());
weather_zh = new HashMap<String, String>((Map) properties1);
} catch (Exception e) {
log.error("news categories init error {} ", e);
}
}
}
二、java代码读取
代码实例:
package com.self.cn.weather.env;
import org.apache.log4j.Logger;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ServerEnvironment {
private static String apiUser = null;
private static String apiKey = null;
private static String apiGateway = null;
private static String sessionUrl = null;
private static Properties weatherMap = null;
static {
Properties properties = new Properties();
InputStream input = null;
ClassLoader classLoader = ServerEnvironment.class.getClassLoader();
try {
//根目录路径
System.out.println(ServerEnvironment.class.getResource("").getPath());
input = new FileInputStream(classLoader.getResource("configWeather.properties").getFile());
//input = new FileInputStream(classLoader.getResource("com/self/cn/weather/env/configWeather2.properties").getFile());
//获取class下指定文件夹中的配置文件
//方式一
//input = ServerEnvironment.class.getResourceAsStream("/properties/jdbc.properties");
//方式为
//input = new FileInputStream(classLoader.getResource("properties/jdbc.properties").getFile());
properties.load(input);
input.close();
} catch (IOException e) {
Logger.getLogger(ServerEnvironment.class).fatal("No Config file!", e);
}
ServerEnvironment.apiUser = properties.getProperty("api.user");
ServerEnvironment.apiKey = properties.getProperty("api.key");
ServerEnvironment.apiGateway = properties.getProperty("api.gateway");
ServerEnvironment.apiGateway = properties.getProperty("api.gateway");
ServerEnvironment.sessionUrl = properties.getProperty("id5.servlet.session");
InputStream mapInput = null;
try {
mapInput = new FileInputStream(classLoader.getResource("weather_map.properties").getFile());
weatherMap = new Properties();
weatherMap.load(mapInput);
mapInput.close();
} catch (IOException e) {
Logger.getLogger(ServerEnvironment.class).fatal("No Weather Map file!", e);
}
}
public static String getApiUser() {
return apiUser;
}
public static String getApiKey() {
return apiKey;
}
public static String getApiGateway() {
return apiGateway;
}
public static String getSessionUrl() {
return sessionUrl;
}
public static Properties getWeatherMap() {
return weatherMap;
}
public static void main(String[] args) {
System.out.println(ServerEnvironment.getApiUser());
}
}
文件路径:
##二 方法类 ##