简体   繁体   English

Java CDI拦截器无法与WELD一起使用Web应用程序

[英]Java CDI interceptor not working web application with WELD

I have created a web application which uses Weld-2.2.0 CDI implementation and runs on Tomcat-7. 我创建了一个使用Weld-2.2.0 CDI实现并在Tomcat-7上运行的Web应用程序。 I have created an interceptor to log method calls. 我创建了一个拦截器来记录方法调用。 But when i run the application, it bypasses interceptor and calls method directly. 但是,当我运行该应用程序时,它会绕过拦截器并直接调用方法。

My interceptor constructs are as below: 我的拦截器构造如下:

Interceptor Binding: 拦截器绑定:

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface LogMe{

}

Interceptor class: 拦截器类别:

@LogMe
@Interceptor
public class LogInterceptorImpl
{
    private static final Logger log = LogManager.getLogger(LogInterceptorImpl.class);      

    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception
    {
        log.debug("LogInterceptor::intercept");
        return ctx.proceed();
    }    
}

Interceptor Target: 拦截目标:

@LogMe
public class DefaultAppController extends AbstractBaseController
{
    private static final long serialVersionUID = 1L;

    public DefaultAppController()
    {

    }

    @Override @LogMe
    public void processRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        resp.getWriter().write("Hello, Guest !");
    }

}

And finally i have created beans.xml file as below: 最后,我创建了beans.xml文件,如下所示:

<beans 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/beans_1_0.xsd">
    <interceptors>
        <class>x.web.interceptors.LogInterceptorImpl</class>
    </interceptors>
</beans>

I have also created weld resource reference in context.xml file and web.xml file (as described by weld documentation. 我还在context.xml文件和web.xml文件中创建了焊接资源参考(如焊接文档所述)。

<Resource name="BeanManager" 
        auth="Container"
        type="javax.enterprise.inject.spi.BeanManager"
        factory="org.jboss.weld.resources.ManagerObjectFactory" />

And

<resource-env-ref>
        <resource-env-ref-name>BeanManager</resource-env-ref-name>
        <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
    </resource-env-ref>

I tried lots of optins including using weld-tomcat-support jar for integration but none of them seems to be working. 我尝试了很多选择,包括使用weld-tomcat-support jar进行集成,但是似乎都没有用。

Can any one help me out here ? 有人可以帮我从这里出去吗 ?

Am i missing or doing something wrong here ? 我在这里想念还是做错了什么?

I don't think that interceptors are working outside a Java EE container. 我不认为拦截器在Java EE容器之外工作。 I guess standalone Weld can only do DI but not the other features. 我想独立的Weld只能做DI,而不能做其他功能。 See also this post: Is it possible to use javax.interceptor in a Java SE environment? 另请参见此文章: 是否可以在Java SE环境中使用javax.interceptor?

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

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