繁体   English   中英

具有动态参数的自定义注释

[英]Custom Annotation with dynamic argument

这是我的问题:

我有一个注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DummyAnnotation {
String dummyParam1();
String dummyParam2();
}

我的服务是:

@Component
public class DummyService {

@DummyAnnotation(dummyParam1="#otp",dummyParam2="#transactionId")
public void dummy(String otp, String transactionId){

}
}

我的方面是尝试验证两个参数并在验证未成功的情况下抛出异常:

 @Around(value = "@annotation(annotation)")
public Object verifyOtp(final ProceedingJoinPoint jointPoint, final DummyAnnotation  annotation) throws Throwable {
            String transactionId = annotation.dummyParam1();
            String otp = annotation.dummyParam2();
            Boolean otpValid = otpService.verify(transactionId, otp);
            if (otpValid) {
                return jointPoint.proceed();
            }
            else {
                throw new AuthentificationForteException();
            }

}

由于Spring EL无法正常工作并且在我的方面中,我如何获得此工作,我拥有dummyAnnotation.dummyParam1()等于“ #otp”而不是我的参数String otp的值。

提前致谢 。

写一个与此相反的方面会更简单吗?

@DummyAnnotation
public void dummy(@Otp String otp, @TransactionId String transactionId){
  ...
}

它也不是特定于Spring的,因此更易于测试。

(如果您打算遍历参数以提取OTP或TX ID,例如#foo.txId,那么这当然不起作用,但是我认为否则会更简单。)

好的,我找到一个解决方案:

解决方案是实现特定的Spring Expression Parser。
我将这些声明添加到我的方面:

private ExpressionParser expressionParser = new SpelExpressionParser();
private ParserContext parserContext = new TemplateParserContext();

并且我更改了如何检索我的dummyValue:

@Around(value = "@annotation(annotation)")
public Object verifyOtp(final ProceedingJoinPoint jointPoint, final DummyAnnotation  annotation) throws Throwable {

String transactionId = getDummyValue(annotation.dummyParam1(),jointPoint.getArgs());
String otp = getDummyValue(annotation.dummyParam2(),jointPoint.getArgs());

        Boolean otpValid = otpService.verify(transactionId, otp);
        if (otpValid) {
            return jointPoint.proceed();
        }
        else {
            throw new AuthentificationForteException();
        }

}

getDummyValue是一种解析表达式的方法:

private String getDummyValue(String authExpression,  Object[] args){
    Expression expression = expressionParser.parseExpression(authExpression, parserContext);
    String value = expression.getValue(new RootObject(args),String.class);
    return value;
}


private static class TemplateparserContext implements ParserContext{

    @Override
    public boolean isTemplate() {
        return true;
    }

    @Override
    public String getExpressionPrefix() {
        return "#{";
    }

    @Override
    public String getExpressionSuffix() {
        return "}";
    }

}

 protected static class RootObject {

        private final Object[] args;

        private RootObject(Object[] args) {
            super();
            this.args = args;
        }

        public Object[] getArgs() {
            return args;
        }
    }

最后,我将服务和DummyAnnotation的使用更改为:

 @Component
 public class DummyService {

 @DummyAnnotation(dummyParam1="#{args[0]}",dummyParam2="#{args[1]}")
 public void dummy(String otp, String transactionId){

 }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM