2010-12-28 3 views
1

Я написал следующий тестовый код для тестирования InterceptorBinding в EJB, как описано here:метод перехватчик не вызывается

Bean Класс:

@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(); 
} 
} 

перехватчик Переплет:

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

Interceptor: @Interceptor @Log открытый класс 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 файл:

<%@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> 

Однако метод конст не вызывается. Не могли бы вы помочь мне определить проблему?

ответ

4

Вы непосредственно создаете TestInterceptor, что означает, что контейнер EJB не имеет возможности применять перехватчики. Это не сработает, даже для @LocalBean. Вам нужно искать или вводить ссылку на EJB.