我正在尝试在spring中实现自定义注释,如下所示:-测试注释如下
@Documented
@Target( { ElementType.METHOD, ElementType.FIELD })
@Constraint(validatedBy=CheckUser.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
String user();
}为了验证,我编写了CheckUser类,如下所示:
private String xyz;
@Override
public void initialize(Test user) {
xyz=user.toString();
}
@Override
public boolean isValid(Principal principal, ConstraintValidatorContext context) {
if(principal.getName().equalsIgnoreCase(xyz))
return true;
else
return false;
}但它不起作用。有人能告诉我这里出了什么问题吗??
在安全性和我编写的应用程序上下文中
<Secured:global-method-security secured-annotations="enabled" pre-post-annotations="enabled" jsr250-annotations="enabled"/>发布于 2014-06-05 01:23:31
您需要包含消息、组和有效负载,这样才能很好地符合JSR-303规范:http://beanvalidation.org/1.0/spec/#constraintsdefinitionimplementation-constraintdefinition-properties
@Documented
@Target( { ElementType.METHOD, ElementType.FIELD })
@Constraint(validatedBy=CheckUser.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
String user();
String message() default "test failed"
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}https://stackoverflow.com/questions/24029225
复制相似问题