我有一个具有util函数的类,可以按需调用。
复制步骤:
TestMainWebApp> TestMainServlet.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();
}
}
TestMainImpl> Util.java和TLSErrorRedirectListener.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");
}
}
TLSErrorRedirectionListener.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;
}
}
}
TestMainInterface> 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();
}
}
答案 0 :(得分:0)
这是不正确的。您不应将abstract
关键字添加到接口或其方法中。不需要接口方法上的public
;他们必须公开。
这是正确的:
package test.intf;
public interface ConfigurationListener {
void valueChanged(String paramString1, String paramString2);
}
确保test.intf.ConfigurationListener.class
文件出现在您的部署中。否则,类加载器将永远找不到它。