简体   繁体   English

Java EE 5 / Servlet 2.5将EJB注入ServletFilter

[英]Java EE 5 / Servlet 2.5 inject EJB into ServletFilter

I want to inject session beans into my ServletFilter, which seems not to work. 我想将会话bean注入我的ServletFilter,这似乎不起作用。 Can you please tell me how to achieve this? 你能告诉我如何实现这个目标吗?

public class MyExample implements Filter {
    @EJB
    private MyBean someEjb;

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                     FilterChain filterChain) throws IOException, ServletException{
        someEjb.toString();
    }
}

Results in NullPointerException because myEjb is null. 导致NullPointerException,因为myEjb为null。 The platform used is JBoss 5.1 MyBean can be accessed correctly from other EJBs or from Servlets. 使用的平台是JBoss 5.1可以从其他EJB或Servlet正确访问MyBean。

Thank you. 谢谢。


Problem Solved (though I do not know why): 问题解决了(虽然我不知道为什么):

The application consists of three artifacts: - A jar containing the EJBs - A war containing servlets - An ear containing both the above 该应用程序由三个工件组成: - 包含EJB的jar - 包含servlet的war - 包含上述两者的ear

If i package the Filter in the jar, the problem occurs. 如果我将过滤器打包在jar中,则会出现问题。 If i package it along with the servlets in the war, the problem does not occur. 如果我将它与战争中的servlet一起打包,则问题不会发生。

So, immediate problem solved but not understood. 所以,眼前的问题已经解决,但却未被理

Maybe someone can help me understand that? 也许有人可以帮我理解?

If both the servlet and EJB are not in a single ear file, one must use @EJB(mappedName="name") while injecting EJB. 如果servletEJB都不在单个ear文件中,那么在注入EJB时必须使用@EJB(mappedName="name") Check this post for more details. 查看此帖子了解更多详情。

Related links: Injection from outside modules 相关链接: 从外部模块注入

Of course, Filter is a kinda Servlet , therefore known as "Servlet Filter". 当然, Filter是一种有点Servlet ,因此被称为“Servlet过滤器”。 And both Servlet and Filter are web component, hence belongs to web archive, .war , not Java archive, .jar . ServletFilter都是Web组件,因此属于web archive, .war ,而不是Java archive, .jar Filter in jar will not be scanned to inject that kinda annotations and will be dealt as any other regular Java class. jar过滤器不会被扫描以注入那种注释,并将作为任何其他常规Java类处理。

We faced a similar problem, we had some @WebFilter and @WebListener components packaged inside a Jar that weren´t readed (the annotations weren´t processed). 我们遇到了类似的问题,我们将一些@WebFilter@WebListener组件打包在一个没有被加入的Jar中(注释未被处理)。

The objetive was to tell the container to scan other jars looking for other annotated components. 目标是告诉容器扫描其他罐子寻找其他注释组件。

We achive the goal adding the attribute metadata-complete to web.xml declaration. 我们实现了将属性metadata-complete添加到web.xml声明的目标。 Example: 例:

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false"> ...NORMAL_CONTENT_HERE... </web-app> 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM