cocosum 2019-11-04 14:06 采纳率: 33.3%
浏览 548
已采纳

Java AOP 为什么能切入controller层,不能切入实体类层。实体类层用了lombok与persistence

自己定义了一个注解:@AttributeJudge
想用在实体类的属性上面。

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited // 允许继承
public @interface AttributeJudge

实体类:

@Data
@Entity
@Table(name = "XXXX")
public class UserInfo  implements Serializable {

    /**
     *  用户Id
     */
    @Id
    private String userId;
    /**
     *  用户名称
     */
    @AttributeJudge
    private String userName;
}

在控制层(controller都能切入),但是在实体类属性上面无法切入。

@Aspect
@Component
public class AttributeJudgeAsprct {

    // 配置织入点
    @Pointcut("@annotation(com.cocosum.blog.core.AttributeJudge.AttributeJudge)")
    public void attributePointCut() {

    }

    @Around("attributePointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println(pjp);
        // 获取注解的 方法参数列表
        Object[] args = pjp.getArgs();
        System.out.println(args);
        // 放行
        return pjp.proceed();
    }
}       

求解,大佬们!!!!!
其实我想的是判断数据为空,如下:

if (StringUtils.isBlank(userName)) {
    return ResultUtils.returnError("用户名不能为空");
}
 if (StringUtils.isBlank(userPassword) || userPassword.length() < 6) {
    return ResultUtils.returnError("密码长度不能小于6位");
}

每次都写这种,很无奈,我想直接加一个注解,在实体类的属性上:
比如我的注解:
@AttributeJudge(isNull = YES, title = "用户名不能为空")
private String userName;
然后我去捕获。后台统一返回一个json。
有没有好的建议.......

  • 写回答

3条回答 默认 最新

  • tkzc_shark 2019-11-04 14:14
    关注

    注解使用需要扫描实例化该注解

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?