我创建了一个使用Weld-2.2.0 CDI实现并在Tomcat-7上运行的Web应用程序。我创建了一个拦截器来记录方法调用。但是当我运行该应用程序时,它会绕过拦截器并直接调用方法。
我的拦截器构造如下:
拦截器绑定:
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD,TYPE})
public @interface LogMe {
}
拦截器类:
@LogMe
@Interceptor
公共类LogInterceptorImpl
{
私有静态最终Logger日志= LogManager.getLogger(LogInterceptorImpl.class);
@AroundInvoke
公共对象拦截(InvocationContext ctx)引发异常
{
log.debug( LogInterceptor :: intercept);
return ctx.proceed();
}
}
拦截器目标:
@LogMe
公共类DefaultAppController扩展了AbstractBaseController
{
private static final long serialVersionUID = 1L;
public DefaultAppController()
{
}
@Override @LogMe
public void processRequest(HttpServletRequest req,HttpServletResponse resp)引发IOException,ServletException
{
resp.getWriter()。write( Hello,Guest!);
}
}
最后我创建了bean。 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>
我还已经在context.xml文件和web.xml文件中创建了焊接资源引用(如焊接文档。
<资源名称= BeanManager
auth =容器
type = javax.enterprise.inject.spi.BeanManager
factory = org.jboss.weld.resources.ManagerObjectFactory />
和
< resource-env-ref>
< resource -env-ref-name> BeanManager< /资源-env-ref-name>
<资源-env-ref-type> javax.enterprise.inject.spi.BeanManager< / resource-env-ref-type> ;
< / resource-env-ref>
我尝试了很多使用optins的方法
有人可以在这里帮助我吗?
我在这里丢失还是做错了什么?
解决方案我认为拦截器不在Java EE容器之外工作。我想独立的Weld只能做DI,而不能做其他功能。另请参阅此帖子:
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 ?
解决方案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?
这篇关于Java CDI拦截器无法与WELD一起使用Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!