我有一個簡單的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?
你是什麼意思,它不工作?它沒有被調用? – Eugene
是的,它不會被調用 – javaLovah
誰來處理@PostConstruct?你沒有一個容器 – Eugene