开发者

Interceptor method not being invoked

I have written the following test code for testing InterceptorBinding in EJB as explained here:

Bean Class:

@Stateless
@LocalBean
@Interceptors ({LoggingInterceptor.class})
public class TestInterceptor {

 @PostConstruct
 public int func1()
 {
     System.out.println("here1");
     return 0;
 }

 public void func2()
 {
     System.out.println("here2");
 }

 public static void main(String[] args)
 {
    TestInterceptor t = new TestInterceptor();
    t.func1();
    t.func2();
 }
}

Interceptor Binding:

@InterceptorBinding
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
}

Interceptor: @Interceptor @Log public class LoggingInterceptor {

 private java.util.logging.Logger logger =
        java.util.logging.Logger.getLogger("theLogger");

 @AroundInvoke
 public Object intercept(InvocationContext context) throws Exception {
     logger.log(Level.INFO, "here ", context.getMethod().getName());
     return context.proceed();
 }

}

JSP file:

开发者_Python百科
<%@page import="test.TestInterceptor"%>
<%@page import="javax.ejb.EJB"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">

<html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
 </head>
 <body>
     <h1>Hello World!</h1>
     <%
  TestInterceptor object = new TestInterceptor();
  %>
  <%=object.func1()%>
 </body>
</html>

However the intercept method is not being invoked. Can you please help me identify the problem?


You are directly instantiating TestInterceptor, which means the EJB container does not have an opportunity to apply interceptors. This will not work, even for @LocalBean. You need to look up or inject a reference to the EJB instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜