2014-01-20 58 views
3

我有一個簡單的EJB,當我通過我的獨立客戶端運行的每一件事情運轉良好, 但一旦我添加了一個攔截器,在EJB的@PostConstruct停止工作:爲什麼@PostConstruct不起作用?

包攔截器;

public class LogInterceptor { 

@PostConstruct 
public void init(InvocationContext context) { 
    System.out.println(new Date().toString() +" Entered interceptor"); 
} 

@PreDestroy 
public void aboutToBeRemoved(InvocationContext context) { 
    System.out.println(new Date().toString() + " Leaving interceptor"); 
} 

@AroundInvoke 
public Object gettingToBusiness(InvocationContext context) { 

    String methodName = context.getMethod().getName(); 
    System.out.println("The method name is: " +methodName); 

    if (methodName.equals("add")) { 

    } 
    try { 
     return context.proceed(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 


} 

我的測試客戶端:

public class Test { 

public static void main(String[] args){ 
    InitialContext ctx=getInitialContext(); 
    ListRemote stub=null; 
    try { 
     stub = (ListRemote)ctx.lookup("ejb:/Lab12/ListEJB!interceptor.ListRemote?stateful"); 
    } catch (NamingException e) { 
     System.out.println("Lookup Failed"); 
     e.printStackTrace(); 
    } 
    System.out.println("Adding data"); 
    stub.add("Hello"); 
    stub.add("-"); 
    stub.add("World"); 
    stub.add("!!!"); 
    System.out.println("Getting data :"); 
    for(Object o:stub.getAll()){ 
     System.out.print(o+" "); 
    } 
    System.out.println("Removing '-' element"); 
    stub.remove(1); 
    System.out.println("Getting data again:"); 
    for(Object o:stub.getAll()){ 
     System.out.print(o+" "); 
    } 
    stub.destroy(); 
} 

public static InitialContext getInitialContext(){ 
    Properties properties = new Properties(); 
    properties.put(Context.URL_PKG_PREFIXES , "org.jboss.ejb.client.naming"); 
      try { 
     return new InitialContext(properties); 
    } catch (NamingException e) { 
     System.out.println("Cannot generate InitialContext"); 
     e.printStackTrace(); 
    } 
    return null; 
} 
} 

我EJB:

@Stateful(name="ListEJB") 
@Interceptors({LogInterceptor.class}) 
public class ListBean implements ListRemote{ 
private ArrayList<Serializable> list; 

@PostConstruct 
public void init(){ 
    System.out.println("In it init ---------------------------------------------------"); 
    list=new ArrayList<Serializable>(); 
} 
public void add(Serializable object){ 
    System.out.println("Currently in the list is: " +list); 

    System.out.println("The object is: " +object); 
    list.add(object); 
    System.out.println("Added"); 
} 
public Serializable get(int index){ 
    return list.get(index); 
} 
public void remove(int index){ 
    list.remove(index); 
} 
public Object[] getAll(){ 
    return list.toArray(); 
} 
@Remove 
public void destroy(){ 
    list=null; 
} 

} 

爲什麼它禁用我的EJB @PostConstruct?

+0

你是什麼意思,它不工作?它沒有被調用? – Eugene

+0

是的,它不會被調用 – javaLovah

+0

誰來處理@PostConstruct?你沒有一個容器 – Eugene

回答

3

在此代碼:

@PostConstruct 
public void init(InvocationContext context) { 
    System.out.println(new Date().toString() +" Entered interceptor"); 
} 

您攔截原來EJB的PostConstruct,而不是讓它通過。如果要將它傳遞給目標EJB,則需要調用繼續:

@PostConstruct 
public void init(InvocationContext context) { 
    System.out.println(new Date().toString() +" Entered interceptor"); 
    context.proceed(); 
} 

同樣適用於您的PreDestroy。基本上它適用於所有截取的方法。見EJB3 specification,12.5節(頁308):

攔截方法必須始終調用InvocationContext.proceed()或無 隨後攔截的方法或豆業務方法或生命週期 回調方法將被調用。

+0

工作正常,謝謝! – javaLovah