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

查看:99
本文介绍了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 Expression Language或其他方式访问它吗?例如,这不起作用,但这是我要拍摄的

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天全站免登陆