【发布时间】:2016-12-27 01:52:05
【问题描述】:
我有一个类,我有我的 main 方法,我从中调用两个单例模式的静态 getter 调用。也就是说,我有 2 个具有静态块的类,并且每个类都有 getter。以下供参考。
public class RestAPIMain {
public static void main(String[] args) throws MalformedURLException, IOException, SAXException, XMLStreamException {
System.out.println("Starting main");
LinkedHashMap<String, String> urlresponses = URLResponse.getURLResponselist();
LinkedList<String> xmllist = XMLLoad.getXMLFiles();
System.out.println(xmllist);
上面的代码有 getURLResponselist 和 getXMLFiles,它们的类使用静态块加载 XML 文件和 Properties 文件,然后使用 List/Map 使用 getter 存储和检索值,如下所示:
文件加载:
public class FileLoad {
public static Map<String, Object> jsonload;
public static Map<String, Object> executioncontrol;
static {
System.out.println("loading static for fileload");
URL path = FileLoad.class.getResource("FileLoad.class");
try {
Enumeration<URL> e = ClassLoader.getSystemResources("/resources/json/");
while (e.hasMoreElements()) {
URL u = e.nextElement();
String fileName = u.getFile();
File folder = new File(fileName);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
InputStream fis = new FileInputStream(file);
if (file.getName().equals("RestURLS.json")) {
jsonload = CommonUtil.jsonToMap(IOUtils.toString(fis, "UTF-8"));
} else if (file.getName().equals("RestExecution.json")) {
executioncontrol = CommonUtil.jsonToMap(IOUtils.toString(fis, "UTF-8"));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static Map<String, Object> getJSONFiles() {
return jsonload;
}
public static Map<String, Object> getRestExecution() {
return executioncontrol;
}
属性加载
public class PropertiesLoad {
private static Properties prop = new Properties();
public static LinkedHashMap<String, String> allkeyvalues = new LinkedHashMap<String, String>();
static {
System.out.println("loading static for properties");
InputStream ins;
try {
ins = new PropertiesLoad().getClass().getResourceAsStream("/resources/Rest.properties");
prop.load(ins);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getPropValue(String key) {
String propkey = prop.getProperty(key);
return propkey;
}
public static LinkedHashMap<String, String> getAllKeysValues() {
String value = null;
String key = null;
for (Entry<Object, Object> keyvalues : prop.entrySet()) {
key = (String) keyvalues.getKey();
value = (String) keyvalues.getValue();
allkeyvalues.put(key, value);
}
return allkeyvalues;
}
}
网址响应
public class URLResponse {
public static LinkedHashMap<String, String> urlresponses = new LinkedHashMap<String, String>();
@SuppressWarnings("unused")
public static LinkedHashMap<String, String> getURLResponselist() throws MalformedURLException, IOException {
LinkedHashMap<String, String> allkeyvalues = PropertiesLoad.getAllKeysValues();
for (String urlkey : allkeyvalues.keySet()) {
String urls = allkeyvalues.get(urlkey);
System.out.println(urls);
URL url = new URL(urls);
InputStream urlins = new URL(urls).openStream();
if (!(urlins.read() == -1)) {
String xmlresponse = IOUtils.toString(urlins, "UTF-8");
urlins.close();
urlresponses.put(urlkey, xmlresponse);
} else {
HttpURLConnection http = (HttpURLConnection) ((URL) url).openConnection();
int statusCode = http.getResponseCode();
// TODO:add log
}
}
// TODO:add log
return urlresponses;
}
}
URLResponse 调用 Propertiesload 中的 get 方法,该方法通过静态块加载属性文件,文件加载也有静态块,然后 main 方法使用该静态块来加载文件。
这里的观察是静态块是在调用 getter 方法时在 main 方法调用之后执行,而不是在 main 方法开始执行之前执行。在静态块中添加的 print 语句也表明控制首先进入 main 方法和然后从它们移动到静态块并执行静态块。
我的要求是在类加载期间在 main 方法之前加载 XML 文件和属性文件和其他文件,并仅使用 getter 来检索我们存储它的集合,例如单例。另外,我读到该类是首先加载的,然后是链接的,然后是初始化的。因此,静态块在加载期间执行,但在我的情况下不会发生。
请指导我了解我的方法错误的地方以及它是如何发生的,所以在我的代码中,静态块在 main 方法调用之后执行。
【问题讨论】:
-
在 Java 中,
static块在类初始化时运行,不一定在main开始之前。 (考虑到程序可以在运行时生成自己的 .class 文件,其中包含static块。它们将如何在main之前运行?)也许您来自 C++ 或其他在之前初始化全局静态对象的语言main? -
为什么有
main之前要做的事情?您不能只编写代码先加载 XML 和 Properties 文件,然后再进行其他处理main必须做的事情吗?您应该将main视为any Java 程序的起点。做一些不同的事情是令人困惑的。 -
感谢您的意见,现在,我明白了。
标签: java static static-block