【发布时间】:2011-07-21 00:40:43
【问题描述】:
我有一个相当标准的 Spring webapp,并且我有许多自定义注释,我想用它们来表示应用于给定 web 服务方法的要求和约束。例如,我可以将@RequiresLogin 注释应用于任何需要有效用户会话的方法,@RequiresParameters(paramNames = {"name", "email"}) 应用于需要设置“名称”和“电子邮件”的方法,等等。
为了支持这一点,我实现了一个临时实用程序,用于在运行时验证方法的注释约束,它基本上遵循以下模式:
Map<Class<? extends Annotation>, Annotation> annotations = mergeConstraintsFromClassAndMethod(serviceClass, serviceMethod);
if (annotations.containsKey(AnnotationType1.class)) {
AnnotationType1 annotation = (AnnotationType1)annotations.get(AnnotationType1.class);
//do validation appropriate to 'AnnotationType1'
}
if (annotations.containsKey(AnnotationType2.class)) {
AnnotationType2 annotation = (AnnotationType2)annotations.get(AnnotationType2.class);
//do validation appropriate to 'AnnotationType2'
}
//...
这很好用,但是因为我添加了额外的注释而变得有点笨拙。我想用更易于维护的东西来替换它。理想情况下,我希望能够做到:
List<ValidatableAnnotation> annotations = mergeConstraintsFromClassAndMethod(serviceClass, serviceMethod);
for (ValidatableAnnotation annotation : annotations) {
annotation.validate(request);
}
但我很确定这是不可能的,因为注释本身不能包含可执行代码,而且编译器不会让我扩展 java.lang.annotation.Annotation(不是我知道如何允许可执行代码包含在一个注释,即使编译器让我尝试)。
但是,注解可以包含一个嵌套的内部类,并且该内部类可以做任何普通 Java 类可以做的事情。因此,我在此基础上提出的建议是:
public interface AnnotationProcessor {
public boolean processRequest(Annotation theAnnotation, HttpServletRequest request);
}
然后可以像这样实现注解:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RequiresLogin {
public static class Processor implements AnnotationProcessor {
@Override
public boolean processRequest(Annotation theAnnotation, HttpServletRequest request) {
if (! (theAnnotation instanceof RequiresLogin)) {
//someone made an invalid call, just return true
return true;
}
return request.getSession().getAttribute(Constants.SESSION_USER_KEY) != null;
}
}
}
这使验证逻辑保持良好并与正在验证的注释紧密耦合。然后我所有的临时验证代码都可以替换为:
List<Annotation> annotations = mergeConstraintsFromClassAndMethod(serviceClass, serviceMethod);
for (Annotation annotation : annotations) {
processAnnotation(annotation, request);
}
private static boolean processAnnotation(Annotation annotation, HttpServletRequest request) {
AnnotationProcessor processor = null;
for (Class<?> processorClass : annotation.annotationType().getDeclaredClasses()) {
if (AnnotationProcessor.class.isAssignableFrom(processorClass)) {
try {
processor = (AnnotationProcessor)processorClass.newInstance();
break;
}
catch (Exception ignored) {
//couldn't create it, but maybe there is another inner
//class that also implements the required interface that
//we can construct, so keep going
}
}
}
if (processor != null) {
return processor.processRequest(annotation, request);
}
//couldn't get a a processor and thus can't process the
//annotation, perhaps this annotation does not support
//validation, return true
return true;
}
这样就不再需要在每次添加新注释类型时修改临时代码了。我只是将验证器实现为注释的一部分,就完成了。
这看起来是一种合理的使用模式吗?如果没有,那么什么会更好?
【问题讨论】:
-
+1 教我你可以在注释中放置一个内部类,这很有趣。我同意 hoipolloi 的观点,即 AOP 是一个很好的解决方案,它还可以让您不必在每个带注释的方法的顶部调用验证方法。
-
@OpenSauce - 在每个带注释的方法开始时都不会调用验证方法。这有点违背了这样做的目的。验证作为顶级 Spring 控制器 (
DispatcherServlet) 的一部分执行,该控制器充当带注释的服务 API 的网关。在允许请求继续到服务 API 之前,其中有一个调用来运行请求前验证,另一个调用是在呈现服务 API 返回的ModelAndView之前运行请求后验证,仅此而已。
标签: java design-patterns annotations