zoukankan      html  css  js  c++  java
  • 【Java EE 学习 49 下】【Spring学习第一天】【MVC】【注解回顾】

    一、MVC

      1.使用Spring有一个非常大的好处,那就是能够实现完全面向接口编程,传统的使用Dao、Service并不能实现完全的面向接口编程。

      2.示例:https://github.com/kdyzm/day45_spring_mvc

    二、注解示例

      1.定义注解方法示例:

    package com.kdyzm.spring.annotation;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
    public @interface MyAnnotation {
        String value() default "默认值";
    }

      2.使用注解方法示例

    package com.kdyzm.spring.annotation;
    
    @MyAnnotation
    public class Person {
        @MyAnnotation("name属性")
        public String name;
    
        @Override
        @MyAnnotation("toString方法!!!")
        public String toString() {
            return "Person [name=" + name + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
    }

      3.解析注解方法示例

    package com.kdyzm.spring.annotation;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    
    
    public class Test {
        public static void main(String[] args) {
            Class<Person> clazz=Person.class;
            if(clazz.isAnnotationPresent(MyAnnotation.class)){
                MyAnnotation annotation=(MyAnnotation) clazz.getAnnotation(MyAnnotation.class);
                System.out.println(annotation.value());
            }
            Field [] fields=clazz.getFields();
            for(Field field:fields){
                System.out.println(field.getName());
                if(field.isAnnotationPresent(MyAnnotation.class)){
                    MyAnnotation annotation=field.getAnnotation(MyAnnotation.class);
                    System.out.println(annotation.value());
                }
            }
            
            Method[]methods=clazz.getMethods();
            for(Method method:methods){
                System.out.println(method.getName());
                if(method.isAnnotationPresent(MyAnnotation.class)){
                    MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
                    System.out.println(annotation.value());
                }
            }
        }
    }

      4.解析结果:

    默认值
    name
    name属性
    toString
    toString方法!!!
    getName
    setName
    wait
    wait
    wait
    equals
    hashCode
    getClass
    notify
    notifyAll
  • 相关阅读:
    「杂文」随想录
    「小说」妖精舞于废墟之上
    昨日之盛,明日之俗 ~ SDOI2021 退役记
    P6292 区间本质不同子串个数
    「杂文」生之重
    「闭门造车」二叉分块树
    「杂文」雨色的魔法(一)
    「笔记」斜率优化 DP
    「笔记」后缀数组
    NOIP 2020 AFO 记
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/4843978.html
Copyright © 2011-2022 走看看