我有一个相当标准的 Spring web 应用程序,并且我有许多自定义注释,我想用它们来表示应用于给定 web 服务方法的要求和约束。例如,我可以将 @RequiresLogin
注释应用于任何需要有效用户 session 的方法,以及 @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;
}
这不再留下每次我添加新注释类型时都需要修改的临时代码。我只是将 validator 实现为注释的一部分,我就完成了。
这看起来是一种合理的使用模式吗?如果不是,那么什么可能更好?
最佳答案
您可能想要研究 AOP。您可以建议公开某些注释并相应地执行前/后处理的方法。
关于java - 这是注释处理的好模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770156/