使用 EJB 拦截器确保参数有效性



我正在开发从URL接收参数的应用程序,因此我仅限于字符串类型。在每个 bean 方法开始时,我都会检查参数格式,如果我使用拦截器根据每个参数设置的注释来检查参数格式,我想我可以减少我的代码。我对使用注释和拦截器的经验很少,只能在Google上找到非常简单的示例。是否有任何库或框架可以处理此功能?还是例子?

提前谢谢。

拦截器可用于在调用 Bean 方法之前执行自定义代码。您可以在 Bean 类本身或单独的类中定义拦截器方法。

@AroundInvoke
public Object defaultInterceptor(InvocationContext ctx) throws Exception
{
   Object[] parameters = ctx.getParameters();
   /* parameters will have values that will be 
      passed to the method of the target class */
   return ctx.proceed();
}

在 ejb-jar 中创建拦截器的条目.xml通过指定完全限定的路径。这将适用于所有豆子的每种方法。

<interceptor-binding>
      <ejb-name>*</ejb-name>
      <interceptor-class>com.interceptor.DefaultInterceptor</interceptor-class>
   </interceptor-binding>

你有机会使用特殊的框架,比如Hibernate验证器吗?这将减少实施工作量...

最新更新