我有一个具有util函数的类,可以根据需要调用。
重现步骤 :
TestMapsBapp> TestMeServlet.java
package com.main;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.util.Util;
/**
* Servlet implementation class TestMainServlet
*/
@WebServlet("/TestMainServlet")
public class TestMainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("\nCurrent ClassLoader chain: "+JavaEETrainingUtil.getCurrentClassloaderDetail());
Util prov = new Util();
// prov.test();
}
}
Testmanymapple> util.java&talsarrarredredictalistner.java
package com.test.util;
public class Util {
private final static String CLAZZ = Util.class.getName();
static {
System.out.println("Classloading of "+CLAZZ+" in progress..."+JavaEETrainingUtil.getCurrentClassloaderDetail());
}
public boolean checkForTLSErrorRedirection(boolean b) {
test.intf.ConfigurationListener listener = new com.listener.TLSErrorRedirectListener();
listener.valueChanged("test", "test");
return true;
}
public void test() {
System.out.println(" test util");
}
}
TLS error redirection listener.Java
package com.listener;
import com.test.util.JavaEETrainingUtil;
public class TLSErrorRedirectListener implements test.intf.ConfigurationListener {
final static String CLAZZ = TLSErrorRedirectListener.class.getName();
static {
System.out.println("Classloading of "+CLAZZ+" in progress..."+JavaEETrainingUtil.getCurrentClassloaderDetail());
}
public void valueChanged(String key, String value) {
switch(key){
case "test1":
default : break;
}
}
}
TestMainterface> ConfigurationListener.java
package test.intf;
public abstract interface ConfigurationListener
{
public abstract void valueChanged(String paramString1, String paramString2);
}
案件 :
你能帮忙找出根本原因吗? java如何加载在方法中声明的Class?
注意:JavaEETrainingUtil.java用于调试目的
package noclassdef.example1;
import java.util.Stack;
import java.lang.ClassLoader;
/**
* JavaEETrainingUtil
* @author Pierre-Hugues Charbonneau
*
*/
public class JavaEETrainingUtil {
/**
* getCurrentClassloaderDetail
* @return
*/
public static String getCurrentClassloaderDetail() {
StringBuffer classLoaderDetail = new StringBuffer();
Stack<ClassLoader> classLoaderStack = new Stack<ClassLoader>();
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
classLoaderDetail.append("\n-----------------------------------------------------------------\n");
// Build a Stack of the current ClassLoader chain
while (currentClassLoader != null) {
classLoaderStack.push(currentClassLoader);
currentClassLoader = currentClassLoader.getParent();
}
// Print ClassLoader parent chain
while(classLoaderStack.size() > 0) {
ClassLoader classLoader = classLoaderStack.pop();
// Print current
classLoaderDetail.append(classLoader);
if (classLoaderStack.size() > 0) {
classLoaderDetail.append("\n--- delegation ---\n"); } else {
classLoaderDetail.append(" **Current ClassLoader**");
}
}
classLoaderDetail.append("\n-----\n");
return classLoaderDetail.toString();
}
}
这是不正确的。您不应将abstract
关键字添加到接口或其方法。接口方法不需要public
;他们必须是公开的。
这是对的:
package test.intf;
public interface ConfigurationListener {
void valueChanged(String paramString1, String paramString2);
}
确保test.intf.ConfigurationListener.class
文件出现在部署中。如果没有,类加载器将永远不会找到它。