Annotations on Interfaces?

前端 未结 10 2172
一生所求
一生所求 2020-12-08 21:52

I can\'t figure out a use case for being able to annotate interfaces in Java.

Maybe someone could give me an example?

10条回答
  •  猫巷女王i
    2020-12-08 22:26

    A use case that I am working with is javax/hibernate bean validation, we are using interfaces to help us on avoiding to define validations on every specific class.

    public interface IUser {
        @NotNull Long getUserId();
        ...
    }
    
    public class WebUser implements IUser {
        private Long userId;
    
        @Override
        public Long getUserId(){
            return userId;
        }
        ...
    }
    

提交回复
热议问题