我有一个类,其中有我的主要方法,我在其中调用单例模式的两个静态 getter 调用。也就是说,我有 2 个类,它们具有静态 block ,并且每个类都有 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,它们的类使用静态 block 加载 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;
}
}
URL响应
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方法,该方法通过静态 block 加载属性文件,文件加载也有静态 block ,然后由main方法使用静态 block 来加载文件。
这里观察到静态 block 是在主方法调用之后执行,当调用转到 getter 方法时,而不是在主方法开始执行之前。静态 block 中添加的打印语句还表明控件首先转到主方法,并且从他们的 then 移动到静态 block 并执行静态 block 。
我的要求是在类加载期间在 main 方法之前加载 XML 文件和属性文件以及其他文件,并仅使用 getter 来检索我们存储它的集合,就像单例一样。另外,我读到该类首先加载,然后链接,然后初始化。因此,静态 block 在加载期间执行,但在我的情况下没有发生。
请指导我我的方法错误在哪里以及它是如何发生的,因此在我的代码中静态 block 在主方法调用之后执行。
最佳答案
嗯,在 Java 中,静态 block 在类加载时执行。在您的情况下,删除当前拥有的所有静态 block 并将它们放置在您的方法中。然后在主类中创建一个静态 block 并执行需要在主方法启动之前完成的必要调用。下面给出了示例代码。
package com.demo;
public class Sample {
public static void print(String msg){
System.out.println(msg);
}
}
package com.demo;
public class Test {
static{
Sample.print("Invokes Before Main ...");
}
public static void main(String[] args) {
System.out.println("Inside Main ....");
}
}
希望这有帮助。快乐编码。
关于java - 进入main方法后执行的静态 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338061/