剑指Spring框架-基础知识(反射+注解+泛型)

本文介绍了Spring框架的基础知识,包括泛型的使用,如泛型类、接口和方法;Controller在Spring MVC中的作用,简化请求派发;探讨设计模式中的简单工厂、工厂和抽象工厂模式;深入讲解反射机制,如Class对象、构造函数、成员变量和方法的操作;最后,详细阐述自定义注解的工作原理及其在编译和运行时的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

泛型

  • 泛型会在编译为class文件后隐藏。
泛型类

能否在泛型中使用具备继承关系的类

  1. 使用通配符<?>,但让类型检查失去意义
  2. 给泛型加入上边界 ?extends E
  3. 给泛型加入下边界 ?super E
public static void handleMember(GenericClassExample<? super Integer> integerGenericClassExample) {
        Integer result = 111 + (Integer) integerGenericClassExample.getMember();
        System.out.println("result is " + result);
    }
泛型接口

常用于数据类型的生产工厂接口中

public interface GenericIFactory<T,N> {
    T nextObject();
    N nextNumber();
}
泛型方法

<T>才是泛型方法

public static <T> void printArray(T[] inputArray){
        for (T element : inputArray){
            System.out.printf("%s", element);
            System.out.printf(" ");
        }
        System.out.println();
    }

Controller

减少Servlet的数量参照spring mvc,仅通过dispatcherServlet进行请求派发

//@WebServlet("/*")
// 两者不相同,/ 优先级最低,不会覆盖其它的url pattern
@WebServlet("/")
public class DispatcherServlet extends HttpServlet {
    @Override
    public void init(){

    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("request path is : " + req.getServletPath());
        System.out.println("request method is : " + req.getMethod());
    }
}

设计模式

简单工厂模式
public static Mouse  createMouse(int type){
        switch (type) {
            case 0: return new DellMouse();
            case 1: return new HpMouse();
            case 2: return new LenovoMouse();
            default: return new DellMouse();
        }
    }

    public static void main(String[] args) {
        Mouse mouse = MouseFactory.createMouse(1);
        mouse.sayHi();
    }
工厂模式
public class HpMouseFactory implements MouseFactory {
    @Override
    public Mouse createMouse() {
        return new HpMouse();
    }
}
MouseFactory mf = new HpMouseFactory();
        Mouse mouse = mf.createMouse();
        mouse.sayHi();
  • 遵循开闭原则
  • 对客户端隐藏对象创建细节
  • 遵循单一职责
抽象工厂模式

提供一个创建一系列相关或相互依赖对象的接口

  • 抽象工厂侧重统一产品族
  • 工厂方法侧重同一产品等级

ComputerFactory cf = new HpComputerFactory();
        Mouse mouse = cf.createMouse();
        Keyboard keyboard = cf.createKeyboard();
        mouse.sayHi();
        keyboard.sayHello();
 public class HpComputerFactory implements ComputerFactory {
    @Override
    public Mouse createMouse() {
        return new HpMouse();
    }
    @Override
    public Keyboard createKeyboard() {
        return new HpKeyboard();
    }
}

Spring结合了工厂模式和反射机制实现IOC容器

反射

Class类的特点

  • Class类也是类的一种,class是关键字
  • Class类只有一个私有的构造函数,只有JVM能创建Class类的实例
  • JVM中只有唯一一个和类相应的Class对象描述其类型信息
    获取Class对象
  1. Object getClass()
  2. 任何数据类型(包括基本)都有一个“静态”的class属性
  3. 通过Class类的静态方法 forName(String className)-常用
        //第一种方式获取Class对象
        ReflectTarget reflectTarget = new ReflectTarget();
        Class reflectTargetClass1 = reflectTarget.getClass();
        System.out.println("1st : " + reflectTargetClass1.getName());
        //第二种方式获取Class对象
        Class reflectTargetClass2 = ReflectTarget.class;
        System.out.println("2nd: " + reflectTargetClass2.getName());
        //判断第一种方式获取的class对象和第二种方式获取的是否是同一个
        System.out.println(reflectTargetClass1 == reflectTargetClass2);
        //第三种方式来获取Class对象
        Class reflectTargetClass3 = Class.forName("demo.reflect.ReflectTarget");
        System.out.println("3rd: " + reflectTargetClass3.getName());
        System.out.println(reflectTargetClass2 == reflectTargetClass3);

反射操作构造函数

Class clazz = Class.forName("demo.reflect.ReflectTarget");
        //1.获取所有的公有构造方法
        System.out.println("**********************所有公有构造方法*********************************");
        Constructor[] conArray = clazz.getConstructors();
        for(Constructor c : conArray){
            System.out.println(c);
        }
        //2.获取所有构造方法
        System.out.println("************所有的构造方法(包括:私有、受保护、默认、公有)***************");
        conArray = clazz.getDeclaredConstructors();
        for(Constructor c : conArray){
            System.out.println(c);
        }
        //3.获取单个带参数的公有方法
        System.out.println("*****************获取公有、有两个参数的构造方法*******************************");
        Constructor con = clazz.getConstructor(String.class, int.class);
        System.out.println("con = " + con);
        //4.获取单个私有的构造方法
        System.out.println("******************获取私有构造方法*******************************");
        con = clazz.getDeclaredConstructor(int.class);
        System.out.println("private con = " + con);
        System.out.println("******************调用私有构造方法创建实例*******************************");
        //暴力访问(忽略掉访问修饰符)
        con.setAccessible(true);
        ReflectTarget reflectTarget = (ReflectTarget) con.newInstance(1);

反射操作成员变量

//获取Class对象
        Class reflectTargetClass = Class.forName("demo.reflect.ReflectTarget");
        //1.获取所有公有的字段
        System.out.println("************获取所有公有的字段********************");
        Field[] fieldArray = reflectTargetClass.getFields();
        for (Field f : fieldArray){
            System.out.println(f);
        }
        //2.获取所有的字段
        System.out.println("************获取所有的字段(包括私有、受保护、默认的)********************");
        fieldArray = reflectTargetClass.getDeclaredFields();
        for (Field f : fieldArray){
            System.out.println(f);
        }
        //3.获取单个特定公有的field
        System.out.println("*************获取公有字段并调用***********************************");
        Field f = reflectTargetClass.getField("name");
        System.out.println("公有的field name : " + f);
        ReflectTarget reflectTarget = (ReflectTarget)reflectTargetClass.getConstructor().newInstance();
        //4.给获取到的field赋值
        f.set(reflectTarget, "待反射一号");
        //5.验证对应的值name
        System.out.println("验证name : " + reflectTarget.name);
        //6.获取单个私有的Field
        System.out.println("**************获取私有字段targetInfo并调用********************************");
        f = reflectTargetClass.getDeclaredField("targetInfo");
        System.out.println(f);
        f.setAccessible(true);
        f.set(reflectTarget, "13810592345");
        System.out.println("验证信息" + reflectTarget);

反射操作成员方法

//1、获取Class对象
        Class reflectTargetClass = Class.forName("demo.reflect.ReflectTarget");
        //2、获取所有公有方法
        System.out.println("***************获取所有的public方法,包括父类和Object*******************");
        Method[] methodArray = reflectTargetClass.getMethods();
        for(Method m : methodArray){
            System.out.println(m);
        }
        //3、获取该类的所有方法
        System.out.println("***************获取所有的方法,包括私有的*******************");
        methodArray = reflectTargetClass.getDeclaredMethods();
        for(Method m : methodArray){
            System.out.println(m);
        }
        //4、获取单个公有方法
        System.out.println("***************获取公有的show1()方法*******************");
        Method m = reflectTargetClass.getMethod("show1", String.class);
        System.out.println(m);
        //5、调用show1并执行
        ReflectTarget reflectTarget = (ReflectTarget)reflectTargetClass.getConstructor().newInstance();
        m.invoke(reflectTarget, "待反射方法一号");
        //6、获取一个私有的成员方法
        System.out.println("***************获取私有的show4()方法******************");
        m = reflectTargetClass.getDeclaredMethod("show4", int.class);
        System.out.println(m);
        m.setAccessible(true);
        String result = String.valueOf(m.invoke(reflectTarget, 20));
        System.out.println("返回值 : " + result);

自定义注解

  • 作为特定的标记,用于告诉编译器信息
  • 编译时动态处理(如lombok动态生成代码)
  • 运行时动态处理,作为额外信息的载体,如获取注解信息

注解分类

  1. 标准注解:Override、Deprecated
  2. 元注解:@Retention(注解的生命周期)、@Target(注解的作用目标)、@Documented(注解是否应在JavaDoc)、@Inherited(是否允许子类继承该注解)
    • 用于修饰注解的注解,用在注解的定义上。
  3. 自定义注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CourseInfoAnnotation {
    //课程名称
    public String courseName();
    //课程标签
    public  String courseTag();
    //课程简介
    public String courseProfile();
    //课程序号
    public int courseIndex() default 303;
}

@CourseInfoAnnotation(courseName = "剑指java面试", courseTag = "面试",
        courseProfile = "不仅讲解Java相关的核心知识,还涉及网络、数据库、缓存框架等核心知识,"
                + "帮助大家构建海陆空一体化的面试护城河。"
                + "全面提升大家的内功。"
)
public class ImoocCourse {
    @PersonInfoAnnotation(name = "翔仔", language = {"Java","C++","Go","Python","PHP","JS"})
    private String author;
    @CourseInfoAnnotation(courseName = "校园商铺",
            courseTag = "实战",
            courseProfile = "手把手教会从前端到后端开发多店铺商铺管理系统,"
                    + "可以用在毕设创业中,学习完会对SSM以及Springboot有一个"
                    + "全面的了解",
            courseIndex = 144)
    public void getCourseInfo() {

    }
}
 //解析类的注解
    public static  void  parseTypeAnnotation() throws ClassNotFoundException {
        Class clazz = Class.forName("demo.annotation.ImoocCourse");
        //这里获取的是class对象的注解,而不是其里面的方法和成员变量的注解
        Annotation[] annotations = clazz.getAnnotations();
        for(Annotation annotation : annotations){
            CourseInfoAnnotation courseInfoAnnotation =  (CourseInfoAnnotation) annotation;
            System.out.println("课程名:" + courseInfoAnnotation.courseName() + "\n" +
                    "课程标签:" + courseInfoAnnotation.courseTag() + "\n" +
                    "课程简介:" + courseInfoAnnotation.courseProfile() + "\n" +
                    "课程序号:" + courseInfoAnnotation.courseIndex() );
        }
    }
    //解析成员变量上的标签
    public static void parseFieldAnnotation() throws ClassNotFoundException {
        Class clazz = Class.forName("demo.annotation.ImoocCourse");
        Field[] fields = clazz.getDeclaredFields();
        for(Field f : fields){
            //判断成员变量中是否有指定注解类型的注解
            boolean hasAnnotation = f.isAnnotationPresent(PersonInfoAnnotation.class);
            if(hasAnnotation){
                PersonInfoAnnotation personInfoAnnotation = f.getAnnotation(PersonInfoAnnotation.class);
                System.out.println("名字:" + personInfoAnnotation.name() + "\n" +
                        "年龄:" + personInfoAnnotation.age() + "\n" +
                        "性别:" + personInfoAnnotation.gender() + "\n");
                for(String language : personInfoAnnotation.language()){
                    System.out.println("开发语言:" + language);
                }

            }
        }
    }
    //解析方法注解
    public static void parseMethodAnnotation() throws ClassNotFoundException{
        Class clazz = Class.forName("demo.annotation.ImoocCourse");
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            /*
             * 判断方法中是否有指定注解类型的注解
             */
            boolean hasAnnotation = method.isAnnotationPresent(CourseInfoAnnotation.class);
            if(hasAnnotation){
                CourseInfoAnnotation courseInfoAnnotation = method.getAnnotation(CourseInfoAnnotation.class);
                System.out.println("课程名:" + courseInfoAnnotation.courseName() + "\n" +
                        "课程标签:" + courseInfoAnnotation.courseTag() + "\n" +
                        "课程简介:" + courseInfoAnnotation.courseProfile() + "\n"+
                        "课程序号:" + courseInfoAnnotation .courseIndex() + "\n");
            }
        }
    }
这里为什么能在注解中获取属性值呢?
- java中万物皆对象,JVM会为注解生成代理对象。(修改启动参数,记录中间对象)-jdk动态代理
VM options添加: -Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true
或者 System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
-XX:+TraceClassLoading (跟踪类加载)

这时我们可以观察到

// 注解也是一个接口,$Proxy1时JDK动态生成的
public final class $Proxy1 extends Proxy implements PersonInfoAnnotation {
    private static Method m1;
    private static Method m4;
    private static Method m3;
    private static Method m2;
    private static Method m6;
    private static Method m5;
    private static Method m7;
    private static Method m0;

    public $Proxy1(InvocationHandler var1) throws  {
    	// InvocationHandler 中有个invoke(不是反射的)它是个接口
    	//	实现类为AnnotationInvocationHandler
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue();
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }
	// 返回值类型与属性定义一致
    public final String[] language() throws  {
        try {
            return (String[])super.h.invoke(this, m4, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String name() throws  {
        try {
            return (String)super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String gender() throws  {
        try {
            return (String)super.h.invoke(this, m6, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final int age() throws  {
        try {
            return ((Integer)super.h.invoke(this, m5, (Object[])null)).intValue();
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final Class annotationType() throws  {
        try {
            return (Class)super.h.invoke(this, m7, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final int hashCode() throws  {
        try {
            return ((Integer)super.h.invoke(this, m0, (Object[])null)).intValue();
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        try {
        	// PersonInfoAnnotation的属性名都在这啦~
            m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
            m4 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("language", new Class[0]);
            m3 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("name", new Class[0]);
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m6 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("gender", new Class[0]);
            m5 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("age", new Class[0]);
            m7 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("annotationType", new Class[0]);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

注解的工作原理

  1. 通过键值对的形式为注解属性赋值
  2. 编译器检查注解的使用范围,将注解信息写入到元素属性表
  3. 运行时JVM将RUNTIME的所有注解属性取出并最终存入map
  4. 创建AnnotationInvocationHandler实例并传入前面的map
  5. JVM使用JDK动态代理为注解生成代理类,初始化处理器
  6. 调用invoke,通过传入方法名返回注解对应属性值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值