EJB3 - Interceptors
// @Interceptor放此就是class等級的interceptor,
// 則此class呼叫到的每個method都會呼叫該interceptor的method.
@Interceptors(ClassLevelAroundInvoke.class)
@Stateful
public class TestStateful implements ITestStateful {
...
// @Interceptors可指定一或多個interceptor(用逗號隔開interceptor class)
// 指定呼叫此method時的interceptor為TestInterceptor
// 也可@Interceptor(TestInterceptor.class)
@Interceptors({TestInterceptor.class, TestInteceptor2.class})
public String addString(String src) {
status.setName(status.getName() + src);
return status.getName();
}
...
}
// addString被呼叫時TestInterceptor.aroundInvokd會被呼叫
public class TestInterceptor {
@AroundInvoke
public Object aroundInvoke(InvocationContext invocationContext)
throws Exception {
System.out.println("TestStatelessInterceptor.aroundInvoke is called from:"
+ invocationContext.getMethod());
return invocationContext.proceed();
}
}
// addString被呼叫時TestInterceptor.aroundInvokd會被呼叫
public class TestInteceptor2 {
@AroundInvoke
public Object aroundInvoke(InvocationContext invocationContext)
throws Exception {
System.out.println("TestStatelessInteceptor2.aroundInvoke is called from:"
+ invocationContext.getMethod());
return invocationContext.proceed();
}
}
// TestStateful的每個method被呼叫時都會呼叫此interceptor
public class ClassLevelAroundInvoke {
@AroundInvoke
public Object aroundInvoke(InvocationContext invocationContext)
throws Exception {
System.out.println("ClassLevelAroundInvoke.aroundInvoke is called from:"
+ invocationContext.getTarget() + " method:"
+ invocationContext.getMethod());
return invocationContext.proceed();
}
}
< !-- 先註冊interceptor -- >
< interceptors>
< interceptor>
< interceptor-class>interceptors.DefaultInterceptor< /interceptor-class>
< /interceptor>
< /interceptors>
< !-- 再指定該interceptor為DefaultInterceptor -- >
< assembly-descriptor>
< interceptor-binding>
< ejb-name>*< /ejb-name>
< interceptor-class>interceptors.DefaultInterceptor< /interceptor-class>
< /interceptor-binding>
< /assembly-descriptor>
public class ClassLevelAroundInvoke {
@AroundInvoke
public Object aroundInvoke(InvocationContext invocationContext)
throws Exception {
System.out.println("ClassLevelAroundInvoke.aroundInvoke is called from:"
+ invocationContext.getTarget() + " method:"
+ invocationContext.getMethod());
return invocationContext.proceed();
}
}
< interceptors>
< interceptor>
< interceptor-class>interceptors.TestInterceptor< /interceptor-class>
< /interceptor>
< interceptor>
< interceptor-class>interceptors.TestInterceptor2< /interceptor-class>
< /interceptor>
< /interceptors>
< assembly-descriptor>
< interceptor-binding>
< ejb-name>TestInterceptorOrder< /ejb-name>
< interceptor-order>
< interceptor-class>interceptors.TestInterceptor2< /interceptor-class>
< interceptor-class>interceptors.TestInterceptor< /interceptor-class>
< /interceptor-order>
< /interceptor-binding>
< /assembly-descriptor>
若指定interceptor-order後再指定method level的interceptor, 則以method-level的interceptor-order為主, 原來的interceptor-order會失效(實作發現的, 文件上沒明說)
< interceptor-binding>
< ejb-name>TestInterceptorOrder< /ejb-name>
< interceptor-order>
< interceptor-class>interceptors.DefaultInterceptor< /interceptor-class>
< interceptor-class>interceptors.XMLAssignedInterceptor< /interceptor-class>
< interceptor-class>interceptors.DefaultInterceptor< /interceptor-class>
< /interceptor-order>
< method>
< method-name>showResult< /method-name>
< /method>
< /interceptor-binding>
< interceptor-binding>
< ejb-name>TestInterceptorOrder< /ejb-name>
< interceptor-order>
< interceptor-class>interceptors.TestInterceptor2< /interceptor-class>
< interceptor-class>interceptors.TestInterceptor< /interceptor-class>
< /interceptor-order>
< /interceptor-binding>
@ExcludeDefaultInterceptors //在這指定使此class所有method都不會呼叫DefaultInterceptor
@Interceptors(TestInterceptor.class)
@Stateless
public class TestDI implements ITestDI {
@ExcludeDefaultInterceptors // 在者指定使此method不會呼叫DefaultInterceptor
@ExcludeClassInterceptors // 指定此method不會呼叫Class interceptor
public String showDIResult() {
StringBuffer sb = new StringBuffer();
sb.append("how to inject mystringvalue from env-entry?:"
+ mystringvalueKK);
sb.append("TimerService:" + timerService);
sb.append("dataSourceFromField:" + dataSourceFromField.toString());
sb.append("mailSessionFromSetter:" + mailSessionFromSetter.toString());
sb.append("mailSessionFromInitialContext:"
+ mailSessionFromInitialContext.toString());
sb.append("mailSessionFromUtils:" + mailSessionFromUtils.toString());
return "DI Result:" + sb.toString();
}
}
public class TestInterceptor {
@PostConstruct
public void postConstructInterceptor(InvocationContext invocationContext) {
try {
System.out.println("TestInterceptor post construct:"
+ invocationContext.getMethod());
invocationContext.proceed();
} catch (Exception ex) {
Logger.getLogger(TestInterceptor.class.getName())
.log(Level.SEVERE, null, ex);
}
}
@AroundInvoke
public Object aroundInvoke(InvocationContext invocationContext)
throws Exception {
System.out.println("TestStatelessInterceptor.aroundInvoke is called from:"
+ invocationContext.getMethod());
return invocationContext.proceed();
}
@PreDestroy
public void preDestroyInterceptor(InvocationContext invocationContext) {
try {
System.out.println("TestInterceptor preDestroy:"
+ invocationContext.getMethod());
invocationContext.proceed();
} catch (Exception ex) {
Logger.getLogger(TestInterceptor.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}
Posted at 04:44下午 四月 19, 2008 by shooeugenesea in EJB3 | 迴響[0]