spring注解可以访问方法参数吗? [英] Can spring annotation access method parameters?

查看:28
本文介绍了spring注解可以访问方法参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个 UrlValidator 方法注释,它在调用方法之前测试给定的 url 是否有效.

Consider a UrlValidator method annotation that tests if a given url is valid before calling a method.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UrlValdator{
    String value();
}

当路由是静态的并且提前知道时,这可以正常工作.例如:

This is working fine when routes are static and known ahead of time. For example:

@UrlValidator("http://some.known.url")
public void doSomething();

但这不是很灵活.例如,如果路由隐含在 doSomething() 方法签名中怎么办?我可以通过 Spring 表达式语言或其他方式以某种方式访问​​它吗?例如,这不起作用,但我正在拍摄

But this is not very flexible. For example, what if the route was implicit in the doSomething() method signature? Could I somehow access it form the Spring Expression Language, or some other means? For example, this doesn't work but is what I'm shooting for

@UrlValidator("#p1")
public void doSomething(String url)

@UrlValidator("#p1.url")
public void doSomething(Request request)

是否可以通过这种方式使注释动态化?

Is it possible to make annotations dynamic this way?

这是我找到的最接近的,但是线程很旧,接受的答案很麻烦/难以理解.是否有一个最小的工作示例/更新的方法来做到这一点?

This is the closest I've found, but the thread is old and the accepted answer is quire cumbersome/hard to follow. Is there a minimal working example/updated way to do this?

推荐答案

简短回答:是的.

长答案:ElementType 指定注解,可以是:ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE, and TYPE_PARAMETER.对这里的 PARAMETER 感兴趣.由于我们希望编译器运行我们的代码,RetentionPolicy.RUNTIME 适合保留类型.接下来我们必须添加 @Constraint 注释,根据文档:

Long answer: ElementType specifies the target of the annotation, which can be the following: ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE, and TYPE_PARAMETER. Were are interested in PARAMETER here. Since we want from the compiler the run our code, RetentionPolicy.RUNTIME is fine for the retention type. Next we have to add @Constraint annotation, which according to the documentation:

将注释标记为 Bean 验证约束.

Marks an annotation as being a Bean Validation constraint.

这意味着,Spring 将获取您的参数并在运行时对其进行验证.我们要做的最后一件事是实现验证本身,这意味着创建一个实现 ConstraintValidator 接口.

This means, Spring will pick up your parameter and validate it in runtime. The last thing we have to do is to implement the validation itself which implies creating a class which implements ConstraintValidator interface.

综合起来:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UrlValidatorImplementation.class)
public @interface UrlValidator{
    String message() default "Invalid url";
}

UrlValidatorImplementation 类的实现:

public class UrlValidatorImplementation implements ConstraintValidator<UrlValidator, String> {
    @Override
    public void initialize(UrlValidator annotation) {
        // initialization, probably not needed
    }

    @Override
    public boolean isValid(String url, ConstraintValidatorContext context) {
        // implementation of the url validation
    }
}

注解的使用:

public void doSomething(@UrlValidator url) { ... }

这篇关于spring注解可以访问方法参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆