JAVA常用自定义的注解使用

谁借莪1个温暖的怀抱¢ 2022-07-19 02:47 237阅读 0赞

@Deprecated 一般表示该方法可能在以后的版本不用了

@Retention 注释类型的注释要保留多久,可配置RetentionPolicy类型的数据,只能配置一种

  1. package java.lang.annotation;
  2. public enum RetentionPolicy {
  3. /** * Annotations are to be discarded by the compiler. * 编译器要丢弃的注释 */
  4. SOURCE,
  5. /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior. * 编译器将把注释记录在类文件中,但在运行时 VM 不需要保留注释 */
  6. CLASS,
  7. /** * Annotations are to be recorded in the class file by the compiler and * retained by the VM at run time, so they may be read reflectively. * * @see java.lang.reflect.AnnotatedElement * 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。 */
  8. RUNTIME
  9. }

@Target 限定注解,指示注释类型所适用的程序元素的种类。可配置ElementType类型的数据,可同时配置多个

  1. package java.lang.annotation;
  2. public enum ElementType {
  3. /** Class, interface (including annotation type), or enum declaration 类、接口(包括注释类型)或枚举声明*/
  4. TYPE,
  5. /** Field declaration (includes enum constants) 字段 */
  6. FIELD,
  7. /** Method declaration 方法 */
  8. METHOD,
  9. /** Formal parameter declaration 参数 */
  10. PARAMETER,
  11. /** Constructor declaration 构造方法 */
  12. CONSTRUCTOR,
  13. /** Local variable declaration 局部变量 */
  14. LOCAL_VARIABLE,
  15. /** Annotation type declaration 注释类型 */
  16. ANNOTATION_TYPE,
  17. /** Package declaration 包 */
  18. PACKAGE,
  19. /** * Type parameter declaration * * @since 1.8 * 类型变量的声明语句 */
  20. TYPE_PARAMETER,
  21. /** * Use of a type * * @since 1.8 * 使用类型的任何语句(例如声明语句、泛型和强制转换语句中的类型) */
  22. TYPE_USE
  23. }

@Documented 可以在制作JavaDoc文件的同时将注解信息加入到API文件中

@ModelAttribute 注释的方法会在此controller每个方法执行前被执行,可以对controller执行数据前添加一些数据

  1. @MethodAnnotation(desc = "在获取requerst之前先设置所有公共参数的值")
  2. @ModelAttribute
  3. public void setReqAndRes(HttpServletRequest request,HttpServletResponse response){
  4. RequestResponseContext.setRequest(request);
  5. RequestResponseContext.setResponse(response);
  6. }
  7. public HttpServletRequest getRequest() {
  8. return RequestResponseContext.getRequest();
  9. }
  10. public HttpServletResponse getResponse() {
  11. return RequestResponseContext.getResponse();
  12. }

文章主要参考

关于在spring 容器初始化 bean 和销毁前所做的操作:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法

发表评论

表情:
评论列表 (有 0 条评论,237人围观)

还没有评论,来说两句吧...

相关阅读