Java CDI interceptor not working web application with WELD

徘徊边缘 提交于 2019-12-13 19:09:43

问题


I have created a web application which uses Weld-2.2.0 CDI implementation and runs on Tomcat-7. 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 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.

<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.

Can any one help me out here ?

Am i missing or doing something wrong here ?


回答1:


I don't think that interceptors are working outside a Java EE container. I guess standalone Weld can only do DI but not the other features. See also this post: Is it possible to use javax.interceptor in a Java SE environment?



来源:https://stackoverflow.com/questions/27269482/java-cdi-interceptor-not-working-web-application-with-weld

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!