不能从在Tomcat的自定义类加载器加载的类注释 [英] Can't get annotations from classes loaded by custom classloader in tomcat

查看:205
本文介绍了不能从在Tomcat的自定义类加载器加载的类注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于类 org.popper.example.pages.Login

@Page(name="Login")
public interface Login {
}

出口 C:\\ POS \\ example.jar 及以下的servlet

exported to c:\pos\example.jar and the following servlet

public class PopperServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
        URLClassLoader ucl = new URLClassLoader(new URL[] {new File("c:/pos/example.jar").toURI().toURL()});
        System.out.println(Arrays.asList(ucl.loadClass("org.popper.example.pages.Login").getAnnotations()));
    }

    public PopperServlet() throws MalformedURLException, ClassNotFoundException {
        URLClassLoader ucl = new URLClassLoader(new URL[] {new File("c:/pos/example.jar").toURI().toURL()});
        System.out.println(Arrays.asList(ucl.loadClass("org.popper.example.pages.Login").getAnnotations()));
   }
}

运行code作为主要显示了预期的结果。

Running the code as main shows the expected result

[@org.popper.fw.annotations.Page(name=Login)]

运行code作为的servlet在Tomcat中没有找到注释

Running the code as servlet in tomcat doesn't find the annotations

[]

谁能告诉我,为什么?

Can anybody tell me why?

推荐答案

这是与往常一样!方面的类加载器层次结构

It's the same as always: Regard the classloader-hierarchy!

new URLClassLoader(new URL[] {new File("c:/pos/example.jar").toURI().toURL()}, PopperServlet.class.getClassloader());

做的伎俩。但令人惊讶的是注释都没有发现,而不是的ClassNotFoundException NoClassDefError ,这就是我所期待的时候都没有发现注解加载类时...

did the trick. But it's surprising that annotations aren't found instead a ClassNotFoundException or NoClassDefError, thats what I expected when annotations are not found when loading a class...

这篇关于不能从在Tomcat的自定义类加载器加载的类注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆