1 Star 0 Fork 27

linyi / simpleframework

forked from Noah2021 / simpleframework 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
GPL-3.0

课程概览

本项目来自慕课网:Spring源码轻松学 一课覆盖Spring核心知识点

课程在自研框架和 Spring 框架的穿插讲解中让大家逐渐熟悉 Spring 框架的脉络。通过从 0 搭建一个较为完备的 Web 框架来提升框架设计能力,辅以通俗易懂的 Spring 核心模块源码的讲解,带你了解 Spring 框架的设计思路。

课程分为下面两条主线,一条是自研 Spring 框架,源码放在src/main/java/org/simpleframework这个包下面(其中 java 包下面的 com 是一个 Web 项目的 Demo,主要用于测试自研框架的功能,而 demo 是实现自研源码过程中涉及的泛型、涉及模式、反射、注解的讲解 demo ),另外一条主线是跟着老师剖析 Spring 框架源码,这个源码需要你自己去 Spring 官网 clone 到本地进行编译(英语不好的小伙伴可以找找有中文注释的源码版本),本仓库没有。

环境准备

模块梳理

Spring设计的初衷

用于构造Java应用程序的轻量级框架; 1、可以采用Spring来构造任何程序,而不局限于Web程序;

2、轻量级:最少的侵入,与应用程序低耦合,接入成本低;

3、最直观的感受:基于 POJO,构建出稳健而强大的应用;

Spring的野心

为各大技术领域提供支持; 微服务、移动开发、社交API集成、安全管理、云计算等等;

Spring框架图

image-20210305093401986 image-20210305093428458

Spring基础核心模块预览

  1. spring-core

    包含框架基本的核心工具类,其他组件都要使用到这个包里的类;定义并提供资源的访问方式;

  2. spring-beans Spring主要面向Bean编程(BOP);

    Bean的定义、解析、创建;

    BeanFactory;

  3. spring-context 为Spring提供运行时环境,保存对象的状态;

    扩展了BeanFactory;

    ApplicationContext;

  4. spring-aop 最小化的动态代理实现;

    JDK动态代理;

    Cglib;

    只能使用运行时织入,仅支持方法级编织,仅支持方法执行切入点;

为了完整而讲的非核心模块

在Java语言中,从织入切面的方式来看,存在三种织入方式:

image-20210305093547624

Spring源码的下载和编译

  1. 从 github 上拉下 5.2.0.RELEASE 的 Spring 源码,然后在 build.gradle 文件中的 buildscript 加入 repositories 代码块,以后 gradle 本身的依赖就走这个阿里云镜像去下载
buildscript {
    repositories {
    	maven { url'https://maven.aliyun.com/repository/public/' }
    	maven { url'https://maven.aliyun.com/repository/jcenter/' }
    }
	dependencies {
		classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
		classpath 'io.spring.asciidoctor:spring-asciidoctor-extensions:0.1.3.RELEASE'
	}
}
  1. 项目的依赖就走这个地址去下载
repositories {
    maven { url'https://maven.aliyun.com/repository/public/' }
    maven { url'https://maven.aliyun.com/repository/jcenter/' }
    mavenCentral()
    maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
  1. 根据 github 上文档要求在 git 命令行执行下面指令
./gradlew :spring-oxm:compileTestJava

结果如下:

image-20210304174753976

image-20210304174817112

  1. 将源码项目导入 idea 然后等待 相关 jar 包下载
  2. 由于 spring-aspects 编译和其余包编译方法不同,故需要将 spring-aspects 从项目移除

Demo

  1. 新建一个 gradle 模块,然后引入 spring 其他模块作为依赖 由于 spring-context 模块自身也引入了 core、aop、beans 等模块,所以在这里不必引入它们了
dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
  1. 实现 WelcomeService 及其实现类
/*WelcomeService.java*/
public interface WelcomeService {
	String sayHello(String name);
}
/*WelcomeServiceImpl.java*/
public class WelcomeServiceImpl implements WelcomeService {

	@Override
	public String sayHello(String name) {
		System.out.println("欢迎您:"+ name);
		return "success";
	}
}
  1. 通过 xml 文件将 WelcomeService 及其实现类注入 Ioc 容器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   				       https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="welcomeService" class="com.noah2021.service.impl.WelcomeServiceImpl"/>
</beans>
  1. 实现测试类,调用容器中的 bean 检测是否注入成功,如果成功,同时也佐证了 Spring 源码编译成功
public class Entrance {
	public static void main(String[] args) {
		System.out.println("Hello world!");
		String xmlPath = "E:\\Git\\Gitee-Repository\\spring-framework-5.2.0.RELEASE\\springdemo\\src\\main\\resources\\spring\\spring-config.xml";
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		WelcomeService welcomeService = applicationContext.getBean("welcomeService", WelcomeService.class);
		welcomeService.sayHello("Noah2021");
	}
}

搭建自研框架雏形

  1. 依靠骨架新建一个 web 项目
  2. 在 pom 文件内导入 jsp 和 servlet 的依赖
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
  1. 编写 jsp 文件
<%@ page pageEncoding="UTF-8" isELIgnored="false" %>
<html>
    <head>
        <title>Hello</title>
    </head>
    <body>
        <h1>Hello</h1>
        <h2>厉害了,${name}</h2>
    </body>
</html>
  1. 编写对应的 web 后端代码
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = "我的简易框架";
        req.setAttribute("name", name);
        req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
    }
}
  1. 配置 tomcat 并启动
  2. 理解 jsp 运行原理
image-20210306215318036

业务系统架子的搭建

  1. 创建 o2odb 数据库
  2. 门面/外观模式(Facade):提供了一个统一的接口,用来访问子系统中的一群接口,从而让子系统更容易使用。下面举个门面模式的例子,其中 slf4j-log4j12 (Simple Logging Facade for Java)就是依靠门面模式解决了各个版本 jar 包依赖冲突问题。
public class SubSystem {
    public void turnOnTV() {
        System.out.println("turnOnTV()");
    }

    public void setCD(String cd) {
        System.out.println("setCD( " + cd + " )");
    }

    public void startWatching(){
        System.out.println("startWatching()");
    }
}
public class Facade {
    private SubSystem subSystem = new SubSystem();

    public void watchMovie() {
        subSystem.turnOnTV();
        subSystem.setCD("a movie");
        subSystem.startWatching();
    }
}
public class Client {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.watchMovie();
    }
}
  1. 导入 slf4j-log4j12 和 lombok 的依赖,引入 log4j.properties 配置文件并测试(包括注解和工厂创建两者方式)

  2. 实现 basicobject 相关类(HeadLine 和 ShopCategory)

  3. 掌握泛型类、泛型接口、泛型方法的使用

    • 泛型类、泛型方法
/*GenericClassExample.java*/
@Data
public class GenericClassExample<T> {
    private T member;

    public GenericClassExample(T member) {
        this.member = member;
    }

    public T headSomething(T target) {
        return target;
    }

    public String sayHello(String name) {
        return "hello, " + name;
    }

    public static <T> void printArray(T[] inputArray) {
        for (T element : inputArray) {
            System.out.printf("%s", element);
            System.out.print(" ");
        }
        System.out.println();
    }
}
/*GeneraicDemo.java*/
public class GeneraicDemo {
    public static void handMember(GenericClassExample<? super Integer> genericClassExample){
        Integer integer = 123 + (Integer) genericClassExample.getMember();
        System.out.println("result is " + integer);
    }

    public static void main(String[] args) {
        GenericClassExample<Number> integerExample = new GenericClassExample<Number>(123);
        GenericClassExample<String> stringExample = new GenericClassExample<>("abc");
        System.out.println(integerExample.getClass());
        System.out.println(stringExample.getClass());
        handMember(integerExample);
        Integer[] integers = {1, 2, 3, 4, 5, 6};
        Double[] doubles = {1.1, 1.2, 1.3, 1.4, 1.5};
        Character[] characters = {'A', 'B', 'C'};
        stringExample.printArray(integers);
        stringExample.printArray(doubles);
        stringExample.printArray(characters);
    }
    /*  class demo.generic.GenericClassExample   //说明泛型只存在与编译期,详情请移步“泛型擦除”
        class demo.generic.GenericClassExample   //目的是避免过多的创建类而造成的运行时的过度消耗
        result is 246
        1 2 3 4 5 6
        1.1 1.2 1.3 1.4 1.5
        A B C
    * */
}
  • 泛型接口
/*GenericFactory.java*/
public interface GenericFactory<T,N> {
    T nextObject();
    N nextNumber();
}
/*GenericFactory.java*/
public class RobotFactory implements GenericFactory<String, Integer>{
    private String[] stringRobot = new String[]{"Hello","Hi"};
    private Integer[] integerRobot = new Integer[]{111,000};
    @Override
    public String nextObject() {
        Random random = new Random();
        return stringRobot[random.nextInt(2)];//[0,2)
    }

    @Override
    public Integer nextNumber() {
        Random random = new Random();
        return integerRobot[random.nextInt(2)];
    }

    public static void main(String[] args) {
        GenericFactory<String, Integer> factory = new RobotFactory();
        System.out.println(factory.nextObject());
        System.out.println(factory.nextNumber());
    }
}
  1. 实现 datatransferobject 相关类(MainPageInfoDTO 和 Result)
  2. Service 层代码架子的搭建
    • solo 相关类
    • combine 相关类:包含多个 solo 相关类,统一对 controller 提供服务
  3. Controller 层代码架子的搭建

自研框架IOC实现前奏

工厂模式

设计模式懂得都懂,不懂在这比划半天还是似懂非懂,可以结合~~本人博客~~和源码加深理解。

简单工厂

工厂方法

抽象工厂

反射

  1. Class类的特点
    • Class 类也是类的一种,class 则是关键字
    • Class 类只有一个私有的构造函数,只有 JVM 能够创建 Class 类的实例
    • JVM 中只有唯一一个和类相对应的 Class 对象来描述其类型信息
  2. 获取Class对象的方式
public static void main(String[] args) throws ClassNotFoundException {
    //第一种方式获取class对象
    ReflectTarget reflectTarget = new ReflectTarget();
    Class reflectTargetClass1 = reflectTarget.getClass();
    System.out.println("1==>"+reflectTargetClass1.getName());
    //第二种方式获取class对象
    Class reflectTargetClass2 = ReflectTarget.class;
    System.out.println("2==>"+reflectTargetClass2.getName());
    //第三种方式获取class对象
    Class reflectTargetClass3 = Class.forName("demo.reflect.ReflectTarget");
    System.out.println("3==>"+reflectTargetClass3.getName());
}
/*在运行期,一个类只有一个与之对应的Class对象产生
    1==>demo.reflect.ReflectTarget
    2==>demo.reflect.ReflectTarget
    3==>demo.reflect.ReflectTarget
* */
  1. 获取构造方法并调用:通过Class对象可以获取某个类中的构造方法

    • 批量的方法

    ​ public Constructor[] getConstructors():所有"公有的"构造方法 ​ public Constructor[] getDeclaredConstructors():获取所有的构造方法(包括私有、受保护、默认、公有)

    • 获取单个的方法,并调用

    ​ public Constructor getConstructor(Class... parameterTypes):获取单个的"公有的"构造方法:

    ​ public Constructor getDeclaredConstructor(Class... parameterTypes):获取"某个构造方法"可以是私有的,或受保护、默认、 公有;

    • 调用构造方法(如果构造方法是 private 修饰需要先暴力反射):Constructor-->newInstance(Object... initargs)
  2. 获取成员变量并调用

    • 批量的

      Field[] getFields():获取所有的"公有字段"

      Field[] getDeclaredFields():获取所有字段,包括:私有、受保护、默认、公有;

    • 获取单个的

      public Field getField(String fieldName):获取某个"公有的"字段;

      public Field getDeclaredField(String fieldName):获取某个字段(可以是私有的)

    • 设置字段的值(如果成员变量是 private 修饰需要先暴力反射)

      Field --> public void set(Object obj,Object value):

    • 参数说明

      obj:要设置的字段所在的对象(非Class对象,是由构造方法生成的)

      value:要为字段设置的值

  3. 获取成员方法并调用

    • 批量的

      public Method[] getMethods():获取所有"公有方法"(包含了父类的方法也包含Object类)

      public Method[] getDeclaredMethods():获取所有的成员方法,包括私有的(不包括继承的)

    • 获取单个的

    ​ public Method getMethod(String name,Class<?>... parameterTypes):

    ​ public Method getDeclaredMethod(String name,Class<?>... parameterTypes)

    • 参数:

      name : 方法名

      Class ... : 形参的Class类型对象

    • 调用方法(如果成员方法是 private 修饰需要先暴力反射)

      Method --> public Object invoke(Object obj,Object... args):

    • 参数说明

      obj : 要调用方法的对象(非Class对象,是由构造方法生成的)

      args:调用方式时所传递的实参

注解

  1. 注解的功能

    • 作为特定的标记,用于告诉编译器一些信息
    • 编译时动态处理,如动态生成代码
    • 运行时动态处理,作为额外信息的媒体,如获取注解信息
  2. 注解的分类

    • 标准注解:@Override、@Deprecated、@SupressWarnings
    • 元注解(修饰注解的注解,通常用在注解的定义之上):@Retention、@Target、@Inherited、@Documented @Target:定义注解的作用目标 @Retention:定义注解的生命周期,用于决定被该元注解修饰的注解是否显示在编译的文件中 @Inherited:是否允许子类继承该注解 @Documented:注解是否应当被包含在 JavaDoc 文档中
    • 自定义注解(使用元注解实现)
  3. 自定义注解

    • 自定义注解格式
public @interface 注解名{
    修饰符 返回值 属性名() 默认值;
    修饰符 返回值 属性名() 默认值;
    ...
}
  • 注解属性支持的类型

    • 所有的基本数据类型

    • Enum类型

      • String类型
      • Annotation类型
      • Class 类型
      • 以上所有类型的数组
  1. 注解实现
/*CourseInfoAnnotation.java*/
@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;
}
/*PersonInfoAnnotation.java*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonInfoAnnotation {
    //名字
    public String name();
    //年龄
    public int age() default 20;
    //性别
    public String gender() default "男";
    //开发语言
    public String[] language();
}
/*Noah2021Course.java*/
@CourseInfoAnnotation(courseName = "数学",courseTag = "高中",courseProfile = "又难又多")
public class Noah2021Course {
    @PersonInfoAnnotation(name = "Noah2021",language = {"Java","c++","python"})
    private String author;
    @CourseInfoAnnotation(courseName = "化学",courseTag = "理综",courseProfile = "非常难")
    public void getCourseInfo(){
    }
}
/*AnnotationParser.java*/
public class AnnotationParser {
    //解析类的注解
    public static void parseTypeAnnotation() throws ClassNotFoundException {
        Class clazz = Class.forName("demo.annotation.Noah2021Course");
        //这里获取的是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()+"\n");
        }
    }

    //解析成员变量上的标签
    public static void parseFieldAnnotation() throws ClassNotFoundException {
        Class clazz = Class.forName("demo.annotation.Noah2021Course");
        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());
                for (String language : personInfoAnnotation.language()) {
                    System.out.println("开发语言:" + language);
                }

            }
        }
    }

    //解析方法注解
    public static void parseMethodAnnotation() throws ClassNotFoundException {
        Class clazz = Class.forName("demo.annotation.Noah2021Course");
        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("\n" + "课程名:" + courseInfoAnnotation.courseName() + "\n" +
                        "课程标签:" + courseInfoAnnotation.courseTag() + "\n" +
                        "课程简介:" + courseInfoAnnotation.courseProfile() + "\n" +
                        "课程序号:" + courseInfoAnnotation.courseIndex() + "\n");
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        parseTypeAnnotation();
        parseFieldAnnotation();
        parseMethodAnnotation();
    }
}
  1. 注解的工作原理

    • 通过键值对的形式为注解属性赋值
    • 编译器检查注解的使用范围,将注解信息写入元素属性表
    • 运行时 JVM 将 RUNTIME 的所有注解属性取出并最终存入 map 里(单个Class文件内所有的RUNTIME的注解而非整个项目的RUNTIME注解)
    • 创建AnnotationInvacationHandler实例并传入前面的map
    • JVM使用JDK动态代理为注解生成动态代理类,并初始化处理器
    • 调用invoke方法,通过传入方法名返回注解对应的属性值

上述学习对自研框架的意义

  1. 控制反转 IoC (Inversion of Controller)

    • 依托一个类似工厂的 IoC 容器
    • 将对象的创建、依赖关系的管理以及生命周期交由IoC容器管理
    • 降低系统在实现上的复杂性和耦合度,易于扩展,满足开闭原则(软件中的对象(类、模块、方法等),对于扩展是开放的,对于修改是封闭的)
  2. IoC容器的优势

    • 避免在各处使用new来创建类 ,并且可以做到统一维护
    • 创建实例的时候不需要 了解其中的细节
    • 反射+工厂模式的合体,满足开闭原则
  3. 依赖注入

    • 构造方法实现注入
    • setter实现注入
    • 接口实现注入
    • 注解实现注入
  4. 依靠倒置原则、IoC、DI、IoC容器的关系

image-20210306222705892
  1. 控制反转的例子

    一个行李箱是由轮子-->地盘-->箱体-->行李箱构成,但是当轮子发生改变大小时,上层的结构都需要改变,这无疑是不可接受的

image-20210306222854369

​ 依靠控制反转就可以很好地解决这个问题

导图总结

image-20210306223552050

自研框架IoC容器的实现

框架具备的基本功能

  • 解析配置(XML、注解等)
  • 定义和注册对象
  • 注入对象
  • 提供通用的工具类

IoC容器的实现

创建注解-->提取标记对象-->实现容器-->依赖注入

创建注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Service {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Resipotory {
}

提取标记对象

  • 指定范围,获取范围内的所有类
  • 遍历所有类,获取被注解标记的类并加载进容器中
  • 测试
/**
 * 〈通过类加载器可以获得多种资源,我们就是要获取 file 类型的class集合〉
 *
 * @author Noah2021
 * @create 2021/3/7
 * @return
 */
public class ClassUtil {
    public static final String FILE_PROTOCOL = "file";
    /**
     * 获取包下类集合
     *
     * @param packageName 包名
     * @return 类集合
     */
    public static Set<Class<?>> extractPackageClass(String packageName){
        //1.获取到类的加载器。
        ClassLoader classLoader = getClassLoader();
        //2.通过类加载器获取到加载的资源
        URL url = classLoader.getResource(packageName.replace(".", "/"));
        if (url == null){
            //log.warn("unable to retrieve anything from package: " + packageName);
            System.out.println("【WARN】unable to retrieve anything from package: " + packageName);
            return  null;
        }
        //3.依据不同的资源类型,采用不同的方式获取资源的集合
        Set<Class<?>> classSet = null;
        //过滤出文件类型的资源
        if (url.getProtocol().equalsIgnoreCase(FILE_PROTOCOL)){
            classSet = new HashSet<Class<?>>();
            File packageDirectory = new File(url.getPath());
            extractClassFile(classSet, packageDirectory, packageName);
        }
        //TODO 此处可以加入针对其他类型资源的处理

        return classSet;
    }
    /**
     * 递归获取目标package里面的所有class文件(包括子package里的class文件)
     *
     * @param emptyClassSet 装载目标类的集合
     * @param fileSource    目录
     * @param packageName   包名
     * @return 类集合
     */
    private static void extractClassFile(Set<Class<?>> emptyClassSet, File fileSource, String packageName) {
        if(!fileSource.isDirectory()){
            return;
        }
        //如果是一个目录,则调用其listFiles方法获取文件夹下的文件或文件夹
        File[] files = fileSource.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                if(file.isDirectory()){
                    return true;
                } else{
                    //获取文件的绝对值路径
                    String absoluteFilePath = file.getAbsolutePath();
                    if(absoluteFilePath.endsWith(".class")){
                        //若是class文件,则直接加载
                        addToClassSet(absoluteFilePath);
                    }
                }
                return false;
            }
            //根据class文件的绝对值路径,获取并生成class对象,并放入classSet中
            private void addToClassSet(String absoluteFilePath) {
                //1.从class文件的绝对值路径里提取出包含了package的类名
                //如/Users/baidu/imooc/springframework/sampleframework/target/classes/com/imooc/entity/dto/MainPageInfoDTO.class
                //需要弄成com.imooc.entity.dto.MainPageInfoDTO
                absoluteFilePath = absoluteFilePath.replace(File.separator, ".");
                String className = absoluteFilePath.substring(absoluteFilePath.indexOf(packageName));
                //去掉.class后缀
                className = className.substring(0, className.lastIndexOf("."));
                //2.通过反射机制获取对应的Class对象并加入到classSet里
                Class targetClass = loadClass(className);
                emptyClassSet.add(targetClass);
            }
        });
        if(files != null){
            for(File f : files){
                //递归调用
                extractClassFile(emptyClassSet, f, packageName);
            }
        }
    }
    /**
     * 获取Class对象
     *
     * @param className class全名=package + 类名
     * @return Class
     */
    public static Class<?> loadClass(String className){
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            //log.error("load class error:", e);
            System.out.println("load class error:"+ e);
            throw new RuntimeException(e);
        }
    }
    /**
     * 获取classLoader
     *
     * @return 当前ClassLoader
     */
    public static  ClassLoader getClassLoader(){
        return Thread.currentThread().getContextClassLoader();
    }
    //测试
    public class ClassUtilTest {
        @DisplayName("提取目标类方法:extractPackageClassTest")
        @Test
        public void extractPackageClassTest(){
            Set<Class<?>> classSet = ClassUtil.extractPackageClass("com.noah2021.entity");
            for (Class clazz:classSet) {
                System.out.println(clazz);
            }
            Assertions.assertEquals(4, classSet.size());
        }
    }
}
/*控制台打印结果*/
class com.noah2021.entity.dto.MainPageInfoDTO
class com.noah2021.entity.bo.HeadLine
class com.noah2021.entity.bo.ShopCategory
class com.noah2021.entity.dto.Result

实现容器

  • 初始化
/*BeanContainer.java*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BeanContainer {
	private enum ContainerHolder {
        HOLDER;
        private BeanContainer instance;

        ContainerHolder() {
            instance = new BeanContainer();
        }
    }
    //获取bean容器实例
    public static BeanContainer getInstance() {
        return ContainerHolder.HOLDER.instance;
    }
}
  • 保存Class对象及其实例的载体
/*BeanContainer.java*/
//用于存放所有被配资标记的目标对象的Map
private final Map<Class<?>, Object> beanMap = new ConcurrentHashMap();
  • 保存Class对象及其实例的载体

    1. 注解配置的管理与获取

    2. 获取指定范围内的Class对象(上面已完成)

    3. 依据配置提取Class对象,连同实例一并存入容器

    4. 测试

/*BeanContainer.java*/
//加载bean的注解列表
private static final List<Class<? extends Annotation>> BEAN_ANNOTATION
            = Arrays.asList(Component.class, Controller.class, Service.class, Repository.class, Aspect.class);   

//是否加载过bean
private boolean loaded = false;
public boolean isLoaded() {
    return loaded;
}
//bean实例数量
public int size() {
    return beanMap.size();
}
//扫描加载所有的bean
public synchronized void loadBeans(String packageName) {
    //判断bean容器是否被加载过
    if(isLoaded()){
        System.out.println("【WARN】BeanContainer has been loaded");
        return;
    }
    Set<Class<?>> classSet = ClassUtil.extractPackageClass(packageName);
    //新建一个验证工具类ValidationUtil用于字符串、数组等的判空校验
    if(ValidationUtil.isEmpty(classSet)){
        System.out.println("【WARN】extract nothing from packageName" + packageName);
        return;
    }
    for (Class clazz:classSet) {
        for (Class<? extends Annotation> annotation: BEAN_ANNOTATION) {
            //如果类上面标记了定义的注解
            if(clazz.isAnnotationPresent(annotation))
                //将目标类本身作为键,目标类的实例作为值,放入到beanMap中,定义一个newInstance方法用于通过反射初始化对象
                beanMap.put(clazz, ClassUtil.newInstance(clazz,true));
        }
    }
    loaded = true;
}
//在com.noah2021包里面加上一些自定义注解,然后进行测试
public class BeanContainerTest {
    private static BeanContainer beanContainer;
    @BeforeAll
    static void init(){
        beanContainer = BeanContainer.getInstance();
    }
    @DisplayName("加载目标类及其实例到BeanContainer:loadBeansTest")
    @Test
    public void loadBeansTest(){
        Assertions.assertEquals(false, beanContainer.isLoaded());
        beanContainer.loadBeans("com.noah2021");
        Assertions.assertEquals(6, beanContainer.size());
        Assertions.assertEquals(true, beanContainer.isLoaded());
    }
}
  • 容器的操作方式,涉及到容器的增删改查

    1. 增加、删除操作
    2. 通过Class获取对应实例
    3. 获取所有的Class和实例
    4. 通过注解来获取被注解标注的Class
    5. 通过超类获取对应的子类Class
    6. 获取容器载体保存Class的数量
    7. 在com.noah2021包里添加自定义注解,并进行单元测试
    /*BeanContainer.java*/ 
    /**
     * 添加一个class对象及其Bean实例
     * @param clazz Class对象
     * @param bean  Bean实例
     * @return 原有的Bean实例, 没有则返回null
     */
    public Object addBean(Class<?> clazz, Object bean) {
        return beanMap.put(clazz, bean);
    }
    /*移除一个IOC容器管理的对象*/
    public Object removeBean(Class<?> clazz) {
        return beanMap.remove(clazz);
    }
    /*根据Class对象获取Bean实例*/
    public Object getBean(Class<?> clazz) {
        return beanMap.get(clazz);
    }
    /*获取容器管理的所有Class对象集合*/
    public Set<Class<?>> getClasses(){
        return beanMap.keySet();
    }
    /*获取所有Bean集合*/
    public Set<Object> getBeans(){
        return new HashSet<>(beanMap.values());
    }
    /*根据注解筛选出Bean的Class集合*/
    public Set<Class<?>> getClassesByAnnotation(Class<? extends Annotation> annotation){
        //1.获取beanMap的所有class对象
        Set<Class<?>> keySet = getClasses();
        if(ValidationUtil.isEmpty(keySet)){
            //log.warn("nothing in beanMap");
            System.out.println("【WARN】nothing in beanMap");
            return null;
        }
        //2.通过注解筛选被注解标记的class对象,并添加到classSet里
        Set<Class<?>> classSet = new HashSet<>();
        for(Class<?> clazz : keySet){
            //类是否有相关的注解标记
            if(clazz.isAnnotationPresent(annotation)){
                classSet.add(clazz);
            }
        }
        return classSet.size() > 0? classSet: null;
    }
    /*通过接口或者父类获取实现类或者子类的Class集合,不包括其本身*/
    public Set<Class<?>> getClassesBySuper(Class<?> interfaceOrClass){
        //1.获取beanMap的所有class对象
        Set<Class<?>> keySet = getClasses();
        if(ValidationUtil.isEmpty(keySet)){
            //log.warn("nothing in beanMap");
            System.out.println("【WARN】nothing in beanMap");
            return null;
        }
        //2.判断keySet里的元素是否是传入的接口或者类的子类,如果是,就将其添加到classSet里
        Set<Class<?>> classSet = new HashSet<>();
        for(Class<?> clazz : keySet){
            //判断keySet里的元素是否是传入的接口或者类的子类(非实现类对象或子类对象)
            //父类.class.isAssignableFrom(子类.class)
            if(interfaceOrClass.isAssignableFrom(clazz) && !clazz.equals(interfaceOrClass)){
                classSet.add(clazz);
            }
        }
        return classSet.size() > 0? classSet: null;
    }
    
    /*BeanContainerTest.java*/
    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    public class BeanContainerTest {
        private static BeanContainer beanContainer;
        @BeforeAll
        static void init(){
            beanContainer = BeanContainer.getInstance();
        }
        @DisplayName("加载目标类及其实例到BeanContainer:loadBeansTest")
        @Order(1)
        @Test
        public void loadBeansTest(){
            Assertions.assertEquals(false, beanContainer.isLoaded());
            beanContainer.loadBeans("com.noah2021");
            Assertions.assertEquals(6, beanContainer.size());
            Assertions.assertEquals(true, beanContainer.isLoaded());
        }
        @DisplayName("根据类获取其实例:getBeanTest")
        @Order(2)
        @Test
        public void getBeanTest(){
            MainPageController controller = (MainPageController)beanContainer.getBean(MainPageController.class);
            Assertions.assertEquals(true, controller instanceof MainPageController);
            DispatcherServlet dispatcherServlet = (DispatcherServlet) beanContainer.getBean(DispatcherServlet.class);
            Assertions.assertEquals(null, dispatcherServlet);
        }
        @DisplayName("根据注解获取对应的实例:getClassesByAnnotationTest")
        @Order(3)
        @Test
        public void getClassesByAnnotationTest(){
            Assertions.assertEquals(true, beanContainer.isLoaded());
            Assertions.assertEquals(3, beanContainer.getClassesByAnnotation(Controller.class).size());
        }
        @DisplayName("根据接口获取实现类:getClassesBySuperTest")
        @Order(4)
        @Test
        public void getClassesBySuperTest(){
            Assertions.assertEquals(true, beanContainer.isLoaded());
            Assertions.assertEquals(true, beanContainer.getClassesBySuper(HeadLineService.class).contains(HeadLineServiceImpl.class));
        }
    }

依赖注入

存在问题 实例里面某些必须的成员变量还没有被创建出来,比如:虽然被@Controller注解的MainPageController实例获取到了,但它下面的headLineShopCategoryCombineService为null 实现思路

  • 定义相关的注解标签

  • 实现创建被注解标记的成员变量的实例,并将其注入到成员变量里

    • 遍历 Bean 容器中所有的 Class 对象
    • 遍历 Class 对象的所有成员变量
    • 找出被 Autowired 标记的成员变量
    • 获取这些成员变量的类型
    • 获取这些成员变量的类型在容器里对应的实例
    • 通过反射将对应的成员变量实例注入到成员变量所在类的实例里
/*Autowired.java*/
//Autowired目前只支持成员变量的注入
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
    String value() default "";
}
/*DependencyInjector.java*/
public class DependencyInjector {
    //Bean容器
    private BeanContainer beanContainer;
    public DependencyInjector(){
        beanContainer = BeanContainer.getInstance();
    }
    /**
     * 执行Ioc
     */
    public void doIoc(){
        if(ValidationUtil.isEmpty(beanContainer.getClasses())){
            //log.warn("empty classset in BeanContainer");
            System.out.println("【WARN】empty classSet in BeanContainer");
            return;
        }
        //1.遍历Bean容器中所有的Class对象
        for(Class<?> clazz : beanContainer.getClasses()){
            //2.遍历Class对象的所有成员变量
            Field[] fields = clazz.getDeclaredFields();
            if (ValidationUtil.isEmpty(fields)){
                continue;
            }
            for(Field field : fields){
                //3.找出被Autowired标记的成员变量
                if(field.isAnnotationPresent(Autowired.class)){
                    Autowired autowired = field.getAnnotation(Autowired.class);
                    String autowiredValue = autowired.value();
                    //4.获取这些成员变量的类型
                    Class<?> fieldClass = field.getType();
                    //5.获取这些成员变量的类型在容器里对应的实例
                    Object fieldValue = getFieldInstance(fieldClass, autowiredValue);
                    if(fieldValue == null){
                        throw new RuntimeException("unable to inject relevant type,target fieldClass is:" + fieldClass.getName() + " autowiredValue is : " + autowiredValue);
                    } else {
                        //6.通过反射将对应的成员变量实例注入到成员变量所在类的实例里
                        Object targetBean =  beanContainer.getBean(clazz);
                        ClassUtil.setField(field, targetBean, fieldValue, true);
                    }
                }
            }
        }
    }
    /**
     * 根据Class在beanContainer里获取其实例或者实现类
     */
    private Object getFieldInstance(Class<?> fieldClass, String autowiredValue) {
        Object fieldValue = beanContainer.getBean(fieldClass);
        if (fieldValue != null){
            return fieldValue;
        } else { //说明这个成员变量是一个接口,需要得到它的实现类实例注入到bean容器
            Class<?> implementedClass = getImplementedClass(fieldClass, autowiredValue);
            if(implementedClass != null){
                return beanContainer.getBean(implementedClass);
            } else {
                return null;
            }
        }
    }
    /**
     * 获取接口的实现类
     */
    private Class<?> getImplementedClass(Class<?> fieldClass, String autowiredValue) {
        Set<Class<?>> classSet =  beanContainer.getClassesBySuper(fieldClass);
        if(!ValidationUtil.isEmpty(classSet)){
            if(ValidationUtil.isEmpty(autowiredValue)){
                if(classSet.size() == 1){
                    return classSet.iterator().next();
                } else {
                    //如果多于两个实现类且用户未指定其中一个实现类,则抛出异常
                    throw new RuntimeException("multiple implemented classes for " + fieldClass.getName() + " please set @Autowired's value to pick one");
                }
            } else {
                for(Class<?> clazz : classSet){
                    if(autowiredValue.equals(clazz.getSimpleName())){
                        return clazz;
                    }
                }
            }
        }
        return null;
    }
}
  • 依赖注入的使用(添加注解)并测试

    /HeadLineShopCategoryCombineServiceImpl2.java/ @Service public class HeadLineShopCategoryCombineServiceImpl2 implements HeadLineShopCategoryCombineService { @Override public Result getMainPageInfo() { return null; } } /DependencyInjectorTest.java/ public class DependencyInjectorTest { @DisplayName("依赖注入doIoc") @Test public void doIocTest(){ BeanContainer beanContainer = BeanContainer.getInstance(); beanContainer.loadBeans("com.noah2021"); Assertions.assertEquals(true, beanContainer.isLoaded()); MainPageController mainPageController = (MainPageController)beanContainer.getBean(MainPageController.class); Assertions.assertEquals(true, mainPageController instanceof MainPageController); Assertions.assertEquals(null, mainPageController.getHeadLineShopCategoryCombineService()); new DependencyInjector().doIoc(); Assertions.assertNotEquals(null, mainPageController.getHeadLineShopCategoryCombineService()); Assertions.assertEquals(true, mainPageController.getHeadLineShopCategoryCombineService() instanceof HeadLineShopCategoryCombineServiceImpl1); Assertions.assertEquals(false, mainPageController.getHeadLineShopCategoryCombineService() instanceof HeadLineShopCategoryCombineServiceImpl2); } }

导图总结

image-20210307183721519

自研框架AOP的实现

自上而下从左到右

容器是 OOP 的高级工具,以低耦合低侵入的方式打通从上到下的开发通道,但不擅长从左到右的系统需求会增加维护成本且破坏低耦合 系统需求:程序员才去关心的需求,比如:添加日志信息,系统权限校验 关注点分离原则:不同的问题交给不同的部分去解决,每部分专注解决自己的问题 在Spring源码里创建一个 AopDemo 需要往 springdemo 的 build.gradle 的 dependencies 模块添加 aspects 的相关依赖compile(project(":spring-aspects"))

AOP的子民们

  • 切面 Aspect :把通知应用到切入点的过程,将横切关注点逻辑进行模块化封装的实体对象
  • 通知 Advice :好比是 Class 里面的方法,还定义了织入逻辑的时机
  • 连接点 Joinpoint :类里面可以被增强的方法,允许使用 Advice 的地方
  • 切入点 pointcut :实际被增强的方法
  • SpringAOP 默认只支持方法级别的 Joinpoint

Aspect的执行顺序

单个 Aspect 的执行顺序

image-20210310185418129

多个 Aspect 的执行顺序

image-20210310185454340

AOP操作(前提)

(1)Spring 框架中一般都是基于AspectJ实现AOP操作

AspectJ,本身是单独的框架,不属于Spring组成部分,独立于AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作。

(2)基于AspectJ实现AOP操作

  • 基于xml配置文件实现
  • 基于注解方式实现

(3)项目里引入相关依赖

image-20201014154039308

(4)切入点表达式

  • 切入点表达式作用:知道对哪个类的哪个方法进行增强

  • 语法结构

  • execution([权限修饰符(一般省略)] [返回类型] [类全路径] [方法名称] ([参数列表]))

    • 举例1:对com.zhh.dao.BookDao类里的add进行增强

      execution(* com.zhh.dao.BookDao.add(..))

    • 举例2:对com.zhh.dao.BookDao类里的所有方法进行增强

      execution(* com.zhh.dao.BookDao.*(..))

    • 举例3:对com.zhh.dao包里的所有类,类里的所有方法进行增强

      execution(* com.zhh.dao..(..))

代理模式

Demo

/*ToCPayment.java*/
public interface ToCPayment {
    void pay();
}
/*ToCPaymentImpl*/
public class ToCPaymentImpl implements ToCPayment {
    @Override
    public void pay() {
        System.out.println("以用户名义进行支付");
    }
}
/*AlipayToC*/
public class AlipayToC implements ToCPayment {
    private ToCPayment toCPayment;
    public AlipayToC(ToCPayment toCPayment) {
        this.toCPayment = toCPayment;
    }
    @Override
    public void pay() {
        Before();
        toCPayment.pay();
        After();
    }
    private void After() {
        System.out.println("付钱给慕课网");
    }
    private void Before() {
        System.out.println("从招行取款");
    }
}
/*ProxyDemo*/
public class ProxyDemo {
    public static void main(String[] args) {
        AlipayToC alipayToC = new AlipayToC(new ToCPaymentImpl());
        alipayToC.pay();
    }
}
/*控制台输出*/
从招行取款
以用户名义进行支付
付钱给慕课网

原理很简单,这里只说它的缺点: 对应不同的目标对象,针对不一样的目标对象类型我们都要实现一个代理对象,代理对象的横切逻辑都是一样的,可是还是得再创建一遍代理类。 针对同样的连接点,不同的接口还要创建出不同的代理类,这样维护成本会呈指数级增长

  1. 朔源ClassLoader
    • 通过带有包名的类来获取对应 Class 文件的二进制字节流
    • 根据读取的字节流,将代表的静态存储结构转换成动态数据结构
    • 生成一个代表该类的 Class 对象,作为方法区该类的数据访问入口
  2. 改进的切入点:根据一定的规则区改动或者生成新的字节流,将切面逻辑织入其中
    • 行之有效的方法就是动态代理机制
    • 根据接口或者目标类,计算出代理类的字节码并加载到 JVM 中
  3. JDK 动态代理
    • 动态代理并没有实际的 class 文件,而是在程序运行时生成类的字节码文件并加载到 JVM 中
    • 要求【被代理的类】必须实现接口
    • 并不要求【代理对象】去实现接口,所以可以复用代理对象的逻辑
    • Demo实现
    /*AlipayInvocationHandler.java*/
    public class AlipayInvocationHandler implements InvocationHandler {
        private Object target;
    
        public AlipayInvocationHandler(Object target) {
            this.target = target;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Before();
            Object result = method.invoke(target, args);
            After();
            return result;
        }
        private void After() {
            System.out.println("付钱给慕课网");
        }
        private void Before() {
            System.out.println("从招行取款");
        }
    }
    /*JdkDynamicProxyUtil.java*/
    public class JdkDynamicProxyUtil {
        public static <T>T newProxyInstance(Object target, InvocationHandler h){
            ClassLoader classLoader = target.getClass().getClassLoader();
            Class<T>[] interfaces = (Class<T>[]) target.getClass().getInterfaces();
            return (T)Proxy.newProxyInstance(classLoader,interfaces,h);
        }
    }
    /*ProxyDemo.java*/
    //只需要写一个实现接口的被代理类,省略了之前AlipayToC类的具体实现,提高代码复用
    public class ProxyDemo {
        public static void main(String[] args) {
            /*ToCPayment toCProxy = new AlipayToC(new ToCPaymentImpl());
            toCProxy.pay();*/
            ToCPaymentImpl toCPayment = new ToCPaymentImpl();
            AlipayInvocationHandler h = new AlipayInvocationHandler(toCPayment);
            ToCPayment toCProxy = JdkDynamicProxyUtil.newProxyInstance(toCPayment, h);
            toCProxy.pay();
        }
    }
    //控制台显示与静态代理一致
  4. Cglib(Code Generation Library) 动态代理
    • 不要求被代理类实现接口
    • 内部主要封装了ASM Java字节码操控框架
    • 动态生成子类以覆盖非 final 的方法,绑定钩子回调自定义拦截器
    • Demo实现
    /*CommonPayment.java*/
    public class CommonPayment {
        public void pay() {
            System.out.println("个人名义或者公司名义都可以走这个支付通道");
        }
    }
    /*AliPayMethodInterceptor.java*/
    public class AliPayMethodInterceptor implements MethodInterceptor {
    
        @Override
        public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            Before();
            Object result = methodProxy.invokeSuper(o, args);
            After();
            return result;
        }
        private void After() {
            System.out.println("付钱给慕课网");
        }
        private void Before() {
            System.out.println("从招行取款");
        }
    }
    /*CglibDanamicProxyUtil.java*/
    public class CglibDanamicProxyUtil {
        public static <T>T createProxy(T target, MethodInterceptor methodInterceptor){
            return (T)Enhancer.create(target.getClass(),methodInterceptor);
        }
    }
    /*ProxyDemo.java*/
    public class ProxyDemo {
        public static void main(String[] args) {
            //被代理类没实现接口,打印结果无误但会报警
            CommonPayment commonPayment = new CommonPayment();
            AliPayMethodInterceptor interceptor = new AliPayMethodInterceptor();
            CommonPayment commonProxy = CglibDanamicProxyUtil.createProxy(commonPayment, interceptor);
            commonProxy.pay();
            //被代理类实现接口,打印结果无误但会报警
            ToCPaymentImpl toCPayment = new ToCPaymentImpl();
            AliPayMethodInterceptor interceptor = new AliPayMethodInterceptor();
            ToCPaymentImpl toCProxy = CglibDanamicProxyUtil.createProxy(toCPayment, interceptor);
            toCProxy.pay();
        }
    }
  5. JDK动态代理和Cglib
    • JDK动态代理
      • 实现机制:基于反射机制实现,要求业务类必须实现接口
      • JDK原生,在 JVM 中运行较为可靠
      • 平滑支持 JDK 版本的升级
    • Cglib
      • 实现机制:基于 ASM 机制实现,生成业务类的子类作为代理类
      • 被代理类无需实现接口,能实现代理类的无侵入
    • 默认策略:Bean 实现了接口则用 JDK,否则使用 Cglib

实现自研框架的AOP 1.0

使用 Cglib 来实现:不需要业务类实现接口,相对灵活

  • 解决标记的问题,定义横切逻辑的骨架
  • 定义Aspect横切逻辑以及被代理方法的执行顺序
  • 将横切逻辑织入到被代理的对象以生成动态代理对象
  • 这是1.0版本,所以和你之前用的Spring AOP不一致很正常,接着做下去就是了
  1. 定义与横切逻辑相关的注解
/*Aspect.java*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Aspect {
    /**
     * 需要被织入横切逻辑的注解标签
     */
    Class<? extends Annotation> value();
}
/*Order.java*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
    int value();
}
  1. 定义供外部使用的横切逻辑骨架
/*DefaultAspect.java*/
public abstract class DefaultAspect {
    /** 事前拦截
     * @param target 被代理的目标类
     * @param method 被代理的目标方向
     * @param args 被代理的目标方法对应的参数列表
     * @throws Throwable
     */
    public void before(Class<?> target, Method method, Object[] args) throws Throwable{
    }
    /**
     * 事后拦截
     * @param target 被代理的目标类
     * @param method 被代理的目标方法
     * @param args 被代理的目标方法对应的参数列表
     * @param returnValue 被代理的目标方法执行后的返回值
     * @throws Throwable
     */
    public Object afterReturning(Class<?> target, Method method, Object[] args, Object returnValue) throws Throwable{
        return returnValue;
    }
    /**
     *
     * @param target 被代理的目标类
     * @param method 被代理的目标方法
     * @param args 被代理的目标方法对应的参数列表
     * @param e 被代理的目标方法抛出的异常
     * @throws Throwable
     */
    public void afterThrowing(Class<?> target, Method method, Object[] args,  Throwable e) throws Throwable{
    }
}

  1. 创建 MethodInterceptor 的实现类 AspectListExecutor
  2. 定义必要的成员变量:被代理的类以及Aspect列表
  3. 按照 Order 对 Aspect 进行排序
  4. 实现 Aspect 横切逻辑以及被代理对象方法的定序执行
/*AspectInfo.java*/
@AllArgsConstructor
@Data
public class AspectInfo {
    private int orderIndex;
    private DefaultAspect aspectObject;
}
/*MethodInterceptor.java*/
public class AspectListExecutor implements MethodInterceptor {
    //被代理的类
    private Class<?> targetClass;
    //排好序的Aspect列表
    @Getter
    private List<AspectInfo> sortedAspectInfoList;

    public AspectListExecutor(Class<?> targetClass, List<AspectInfo> aspectInfoList){
        this.targetClass = targetClass;
        this.sortedAspectInfoList = sortAspectInfoList(aspectInfoList);
    }
    /**
     * 按照order的值进行升序排序,确保order值小的aspect先被织入
     *
     * @param aspectInfoList
     * @return
     */
    private List<AspectInfo> sortAspectInfoList(List<AspectInfo> aspectInfoList) {
        Collections.sort(aspectInfoList, new Comparator<AspectInfo>() {
            @Override
            public int compare(AspectInfo o1, AspectInfo o2) {
                //按照值的大小进行升序排序
                return o1.getOrderIndex() - o2.getOrderIndex();
            }
        });
        return aspectInfoList;
    }

    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object returnValue = null;
        if(ValidationUtil.isEmpty(sortedAspectInfoList)){
            returnValue = methodProxy.invokeSuper(proxy, args);
            return returnValue;
        }
        //1.按照order的顺序升序执行完所有Aspect的before方法
        invokeBeforeAdvices(method, args);
        try{
            //2.执行被代理类的方法
            returnValue = methodProxy.invokeSuper(proxy, args);
            //3.如果被代理方法正常返回,则按照order的顺序降序执行完所有Aspect的afterReturning方法
            returnValue = invokeAfterReturningAdvices(method, args, returnValue);
        } catch (Exception e){
            //4.如果被代理方法抛出异常,则按照order的顺序降序执行完所有Aspect的afterThrowing方法
            invokeAfterThrowingAdvides(method, args, e);
        }
        return returnValue;
    }

    //4.如果被代理方法抛出异常,则按照order的顺序降序执行完所有Aspect的afterThrowing方法
    private void invokeAfterThrowingAdvides(Method method, Object[] args, Exception e) throws Throwable {
        for (int i =  sortedAspectInfoList.size() - 1; i >=0 ; i--){
            sortedAspectInfoList.get(i).getAspectObject().afterThrowing(targetClass, method, args, e);
        }
    }

    //3.如果被代理方法正常返回,则按照order的顺序降序执行完所有Aspect的afterReturning方法
    private Object invokeAfterReturningAdvices(Method method, Object[] args, Object returnValue) throws Throwable {
        Object result = null;
        for (int i =  sortedAspectInfoList.size() - 1; i >=0 ; i--){
            result = sortedAspectInfoList.get(i).getAspectObject().afterReturning(targetClass, method, args, returnValue);
        }
        return result;
    }

    //1.按照order的顺序升序执行完所有Aspect的before方法
    private void invokeBeforeAdvices(Method method, Object[] args) throws Throwable {
        for(AspectInfo aspectInfo : sortedAspectInfoList){
            aspectInfo.getAspectObject().before(targetClass, method, args);
        }
    }
}
/*AspectListExecutorTest.java*/
public class AspectListExecutorTest {
    @DisplayName("Aspect排序")
    @Test
    public void sortTest(){
        ArrayList<AspectInfo> aspectInfoList = new ArrayList<>();
        aspectInfoList.add(new AspectInfo(4, new Mock1()));
        aspectInfoList.add(new AspectInfo(5, new Mock4()));
        aspectInfoList.add(new AspectInfo(2, new Mock3()));
        aspectInfoList.add(new AspectInfo(3, new Mock5()));
        aspectInfoList.add(new AspectInfo(1, new Mock2()));
        List<AspectInfo> sortAspectInfoList = new AspectListExecutor(AspectListExecutorTest.class, aspectInfoList).getSortedAspectInfoList();
        for (AspectInfo info: sortAspectInfoList) {
            System.out.println(info);
        }
    }
}
  1. 实现 AspectListExecutor.java 中 interceptor 的具体逻辑
  2. 生成动态代理对象的实现类
/*ProxyCreateor.java*/
public class ProxyCreateor {
    public static Object createProxy(Class<?> targetClass, MethodInterceptor methodInterceptor){
        return Enhancer.create(targetClass,methodInterceptor);
    }
}

  1. 织入 AOP 需要在 BeanContainner 类的 BEAN_ANNOTATION 中加上 Aspect.class
/*AspectWeaver.java*/
public class AspectWeaver {
    private BeanContainer beanContainer;
    public AspectWeaver() {
        this.beanContainer = BeanContainer.getInstance();
    }

    /**
     * 1.获取所有的切面类
     * 2.将切面类按照不同的织入目标进行切分
     * 3.按照不同的织入目标分别去按序织入Aspect的逻辑
     */
    public void doAop(){
        //获取所有的切面类
        Set<Class<?>> aspectSet = beanContainer.getClassesByAnnotation(Aspect.class);
        //将切面类按照不同的织入目标进行切分
        //Key为Aspect注解的value属性,value为同属一个属性的集合
        Map<Class<? extends Annotation>, List<AspectInfo>> categoriedMap = new HashMap<>();
        if(ValidationUtil.isEmpty(aspectSet))
            return;
        for (Class<?> aspectClass: aspectSet) {
            if(verifyAspect(aspectClass)){
                //当前注解类维持map的状态
                categoriedAspect(categoriedMap, aspectClass);
            }else{
                throw new RuntimeException("Aspect 类中未加 @Aspect 或 @Order,亦或者没有继承DefaultAspect.class," +
                        "此外,@Aspect的值等于@Aspect本身");
            }
            //按照不同的织入目标分别去按序织入Aspect的逻辑
            if(ValidationUtil.isEmpty(categoriedMap))
                return;
            for (Class<? extends Annotation> category:categoriedMap.keySet()) {
                weaveByCategory(category, categoriedMap.get(category));
            }
        }
    }

    /**
     * @param category
     * @param aspectInfoList
     * 1.获取被代理类的集合
     * 2.遍历被代理类,分别为每个被代理类生成动态代理实例
     * 3.将动态代理对象实例添加到容器里,取代未被代理前的类实例
     */
    private void weaveByCategory(Class<? extends Annotation> category, List<AspectInfo> aspectInfoList) {
        Set<Class<?>> classSet = beanContainer.getClassesByAnnotation(category);
        if(ValidationUtil.isEmpty(classSet))
            return;
        //遍历被代理类,分别为每个被代理类生成动态代理实例
        for (Class<?> targetClass: classSet) {
            //创建动态代理对象
            AspectListExecutor aspectListExecutor = new AspectListExecutor(targetClass, aspectInfoList);
            Object proxyBean = ProxyCreateor.createProxy(targetClass, aspectListExecutor);
            //将动态代理对象实例添加到容器里,取代未被代理前的类实例
            beanContainer.addBean(targetClass, proxyBean);
        }
    }

    //将切面类按照不同的织入目标进行切分,当前注解类aspectClass维持map的状态
    private void categoriedAspect(Map<Class<? extends Annotation>, List<AspectInfo>> categoriedMap, Class<?> aspectClass) {
        Order orderTag = aspectClass.getAnnotation(Order.class);
        Aspect aspectTag = aspectClass.getAnnotation(Aspect.class);
        DefaultAspect aspect = (DefaultAspect) beanContainer.getBean(aspectClass);
        AspectInfo aspectInfo = new AspectInfo(orderTag.value(), aspect);
        if(!categoriedMap.containsKey(aspectTag.value())){
            //如果织入的joinpoint第一次出现,则以该joinpoint为key,以新建的List<AspectInfo>为value
            List<AspectInfo> aspectInfoList = new ArrayList<>();
            aspectInfoList.add(aspectInfo);
            categoriedMap.put(aspectTag.value(), aspectInfoList);
        }else{
            //如果织入的joinpoint不是第一次出现,则往joinpoint对应的value里添加新的Aspect逻辑
            List<AspectInfo> aspectInfoList = categoriedMap.get(aspectTag.value());
            aspectInfoList.add(aspectInfo);
        }
    }

    //框架中一定要遵守给Aspect类添加@Aspect和@Order标签的规范,同时,必须继承自DefaultAspect.class
    //此外,@Aspect的属性值不能是它本身
    private boolean verifyAspect(Class<?> aspectClass) {
        return aspectClass.isAnnotationPresent(Aspect.class) &&
                aspectClass.isAnnotationPresent(Order.class) &&
                DefaultAspect.class.isAssignableFrom(aspectClass) &&
                aspectClass.getAnnotation(Aspect.class).value() != Aspect.class;
    }
}
  1. 测试 本次是给所有的注解为 Controller.class 的类进行织入,打印 HeadLineOperationController 被注入的 bean 调用的 addHeadLine 方法,所以需要显示一句话表示已执行
/*ControllerTimeCalculatorAspect.java*/
@Aspect(value = Controller.class)
@Order(0)
public class ControllerTimeCalculatorAspect extends DefaultAspect {
    private long timeStampCache;
    @Override
    public void before(Class<?> target, Method method, Object[] args) throws Throwable {
        System.out.println("【INFO】开始计时,执行的类是"+target.getName()+",执行的方法是:"+method.getName()+",参数是:"+args);
        timeStampCache = System.currentTimeMillis();
    }

    @Override
    public Object afterReturning(Class<?> target, Method method, Object[] args, Object returnValue) throws Throwable {
        long endTime = System.currentTimeMillis();
        long costTime = endTime - timeStampCache;
        System.out.println("【INFO】结束计时,执行的类是"+target.getName()+",执行的方法是:"+method.getName()+",参数是:"+args+
                ",返回值是"+returnValue+",时间是:"+costTime);
        return returnValue;

    }
}
/*AspectWeaverTest.java*/
public class AspectWeaverTest {
    @DisplayName("织入通用逻辑测试:doAop")
    @Test
    public void doAopTest(){
        BeanContainer beanContainer = BeanContainer.getInstance();
        beanContainer.loadBeans("com.noah2021");
        new AspectWeaver().doAop();
        new DependencyInjector().doIoc();
        HeadLineOperationController headLineOperationController =
                (HeadLineOperationController) beanContainer.getBean(HeadLineOperationController.class);
        headLineOperationController.addHeadLine(null, null);
    }
}
  1. 待改进的地方
    • Aspect 只支持对某个标签标记的类进行横切逻辑的织入
    • 需要披上 AspectJ 的外衣

实现自研框架的AOP 2.0

AspectJ 框架织入时机:静态织入和 LTW

编译时织入(静态织入):利用ajc编译器而不是Javac编译器,将源文件编译成class文件,并将切面逻辑织入到class文件 编译后织入(静态织入):先利用Javac将源文件编译成class文件,再利用ajc将切面逻辑织入到class文件 类加载期织入(动态织入,Load Time Weaving):利用java agent,在类加载的时候织入切面逻辑

  1. 引入 aspectjrt 和 aspectjweaver 的依赖,由于 aspectjweaver 包含 aspectjrt,所以引入 aspectjweaver 即可
  2. 核心目标
    • 让 pointcut 更加灵活
    • 引入 AspectJ的切面表达式和相关的定位解析机制
  3. 将注解类 Aspect 的属性 value 修改成 pointcut;
  4. 解析 Aspect 表达式并且定位被织入的目标
/*PointcutLocator.java*/
public class PointcutLocator {
    /**
     * Pointcut解析器,直接给它赋值上AspectJ的所有表达式,以便支持对众多表达式的解析
     */
    private PointcutParser pointcutParser = PointcutParser.getPointcutParserSupportingSpecifiedPrimitivesAndUsingContextClassloaderForResolution(
        PointcutParser.getAllSupportedPointcutPrimitives()
    );
    /**
     * 表达式解析器
     */
    private PointcutExpression pointcutExpression;
    public PointcutLocator(String expression) {
        this.pointcutExpression = pointcutParser.parsePointcutExpression(expression);
    }
    /**
     * 判断传入的Class对象是否是Aspect的目标代理类,即匹配Pointcut表达式(初筛)
     *
     * @param targetClass 目标类
     * @return 是否匹配
     */
    public boolean roughMatches(Class<?> targetClass){
        //couldMatchJoinPointsInType比较坑,只能校验within
        //不能校验 (execution(精确到某个类除外), call, get, set),面对无法校验的表达式,直接返回true
        return pointcutExpression.couldMatchJoinPointsInType(targetClass);
    }
    /**
     * 判断传入的Method对象是否是Aspect的目标代理方法,即匹配Pointcut表达式(精筛)
     * @param method
     * @return
     */
    public boolean accurateMatches(Method method){
        ShadowMatch shadowMatch = pointcutExpression.matchesMethodExecution(method);
        if(shadowMatch.alwaysMatches()){
            return true;
        } else{
            return false;
        }
    }
}
  1. 修改 AspectWeaver 中 AOP 的相关实现方式
/*AspectWeaver.java*/
public class AspectWeaver {
    private BeanContainer beanContainer;
    public AspectWeaver() {
        this.beanContainer = BeanContainer.getInstance();
    }

    public void doAop() {
        //1.获取所有的切面类
        Set<Class<?>> aspectSet = beanContainer.getClassesByAnnotation(Aspect.class);
        if(ValidationUtil.isEmpty(aspectSet)){return;}
        //2.拼装AspectInfoList,把所有的注解类组合成AspectInfo然后存入List集合
        List<AspectInfo> aspectInfoList = packAspectInfoList(aspectSet);
        //3.遍历容器里的类
        Set<Class<?>> classSet = beanContainer.getClasses();
        for (Class<?> targetClass: classSet) {
            //排除AspectClass自身
            if(targetClass.isAnnotationPresent(Aspect.class)){
                continue;
            }
            //4.粗筛符合条件的Aspect
            List<AspectInfo> roughMatchedAspectList  = collectRoughMatchedAspectListForSpecificClass(aspectInfoList, targetClass);
            //5.尝试进行Aspect的织入
            wrapIfNecessary(roughMatchedAspectList,targetClass);
        }
    }

    private void wrapIfNecessary(List<AspectInfo> roughMatchedAspectList, Class<?> targetClass) {
        if(ValidationUtil.isEmpty(roughMatchedAspectList)){return;}
        //创建动态代理对象
        AspectListExecutor aspectListExecutor = new AspectListExecutor(targetClass, roughMatchedAspectList);
        Object proxyBean = ProxyCreator.createProxy(targetClass, aspectListExecutor);
        beanContainer.addBean(targetClass, proxyBean);
    }

    private List<AspectInfo> collectRoughMatchedAspectListForSpecificClass(List<AspectInfo> aspectInfoList, Class<?> targetClass) {
        List<AspectInfo> roughMatchedAspectList = new ArrayList<>();
        for(AspectInfo aspectInfo : aspectInfoList){
            //粗筛
            if(aspectInfo.getPointcutLocator().roughMatches(targetClass)){
                roughMatchedAspectList.add(aspectInfo);
            }
        }
        return roughMatchedAspectList;
    }
    //把所有的注解类组合成AspectInfo然后存入List集合
    private List<AspectInfo> packAspectInfoList(Set<Class<?>> aspectSet) {
        List<AspectInfo> aspectInfoList = new ArrayList<>();
        for(Class<?> aspectClass : aspectSet){
            if (verifyAspect(aspectClass)){
                Order orderTag = aspectClass.getAnnotation(Order.class);
                Aspect aspectTag = aspectClass.getAnnotation(Aspect.class);
                //将当前注解类的class对象转换成DefaultAspect对象
                DefaultAspect defaultAspect = (DefaultAspect) beanContainer.getBean(aspectClass);
                //初始化表达式定位器
                PointcutLocator pointcutLocator = new PointcutLocator(aspectTag.pointcut());
                AspectInfo aspectInfo = new AspectInfo(orderTag.value(), defaultAspect, pointcutLocator);
                aspectInfoList.add(aspectInfo);
            } else {
                //不遵守规范则直接抛出异常
                throw new RuntimeException("@Aspect and @Order must be added to the Aspect class, and Aspect class must extend from DefaultAspect");
            }
        }
        return aspectInfoList;
    }

    //框架中一定要遵守给Aspect类添加@Aspect和@Order标签的规范,同时,必须继承自DefaultAspect.class
    //此外,@Aspect的属性值不能是它本身
    private boolean verifyAspect(Class<?> aspectClass) {
        return aspectClass.isAnnotationPresent(Aspect.class) &&
                aspectClass.isAnnotationPresent(Order.class) &&
                DefaultAspect.class.isAssignableFrom(aspectClass);
    }
}
  1. 修改相关测试类中 Aspect 的属性值并测试
/*ControllerTimeCalculatorAspect.java*/
@Aspect(pointcut = "within(com.noah2021.controller.superadmin.*)")
@Aspect(pointcut = "execution(* com.noah2021.controller.superadmin..*.*(..))")

导图总结

image-20210312190231318

image-20210312190214036

image-20210312190249626

自研框架MVC的实现

进行HTTP连接重构

DispatcherServlet

  • 解析请求路径和请求方法
  • 依赖容器,建议并维护 Controller 方法与请求的映射
  • 用合适的 Controller 方法去处理特定的请求
image-20210312220542829
  1. 创建 RequestProcessor 的接口然后令 RequestProcessor 矩阵的相关类实现它
/**
 * 请求执行器
 */
public interface RequestProcessor {
    boolean process(RequestProcessorChain requestProcessorChain) throws Exception;
}
  1. 创建 ResultRender 的接口然后令 Render 矩阵的相关类实现它
/**
 * 渲染处理请求
 */
public interface ResultRender {
    //执行渲染
    public void render(RequestProcessorChain requestProcessorChain) throws Exception;
}
  1. 实现责任链(RequestProcessorChain)类的相关方法
/**
 * 1.以责任链的模式执行注册的请求处理器
 * 2.委派给特定的Render实例对处理后的结果进行渲染
 */
@Data
public class RequestProcessorChain {
    //请求处理器迭代器
    private Iterator<RequestProcessor> requestProcessorIterator;
    //请求request
    private HttpServletRequest request;
    //响应response
    private HttpServletResponse response;
    //http请求方法
    private String requestMethod;
    //http请求路径
    private String requestPath;
    //http响应状态码
    private int responseCode;
    //请求结果渲染器
    private ResultRender resultRender;

    public RequestProcessorChain(Iterator<RequestProcessor> iterator, HttpServletRequest req, HttpServletResponse resp) {
        this.requestProcessorIterator = iterator;
        this.request = req;
        this.response = resp;
        this.requestMethod = req.getMethod();
        this.requestPath = req.getPathInfo();
        this.responseCode = HttpServletResponse.SC_OK;
    }

    /**
     * 以责任链的模式执行请求链
     */
    public void doRequestProcessChain() {
        //1.通过迭代器遍历注册的请求处理器实现类列表
        try {
            while (requestProcessorIterator.hasNext()) {
                //2.直到某个请求处理器执行后返回为false为止
                if (requestProcessorIterator.next().process(this))
                    break;
            }
        } catch (Exception e) {
            //3.期间如果出现异常,则交由内部异常渲染器处理
            this.resultRender = new InternalErrorResultRender(e.getMessage());
            System.out.println("【ERROR】doRequestProcessorChain error:" + e);
        }
    }

    /**
     * 执行处理器
     */
    public void doRender() {
        //1.如果请求处理器实现类均未选择合适的渲染器,则使用默认的
        if(this.resultRender == null)
            this.resultRender = new DefaultResultRender();
        //2.调用渲染器的render方法对结果进行渲染
        try {
            this.resultRender.render(this);
        } catch (Exception e) {
            System.out.println("【ERROR】doRender error:"+e);
            throw new RuntimeException(e);
        }
    }
}
  1. 在 DispatcherServlet 里对进来的请求进行处理
/*DispatcherServlet.java*/
List<RequestProcessor> PROCESSOR = new ArrayList<RequestProcessor>();

@Override
public void init() throws ServletException {
    //1.初始化容器
    BeanContainer beanContainer = BeanContainer.getInstance();
    beanContainer.loadBeans("com.noah2021");
    new AspectWeaver().doAop();
    new DependencyInjector().doIoc();
    //2.初始化请求处理器责任链
    PROCESSOR.add(new PreRequestProcessor());
    PROCESSOR.add(new StaticResourceRequestProcessor());
    PROCESSOR.add(new JspRequestProcessor());
    PROCESSOR.add(new ControllerRequestProcessor());
}

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //1.创建责任链对象的实例
    RequestProcessorChain requestProcessorChain = new RequestProcessorChain(PROCESSOR.iterator(), req, resp);
    //2.通过责任链模式来依次调用请求处理器对请求进行处理
    requestProcessorChain.doRequestProcessChain();
    //3.对处理结果进行渲染
    requestProcessorChain.doRender();
}

@Override
public void destroy() {
    
}

RequestProcessor矩阵的实现

PreRequestProcessor

请求预处理,包括编码以及路径请求

/*PreRequestProcessor.java*/
public class PreRequestProcessor implements RequestProcessor {
    @Override
    public boolean process(RequestProcessorChain requestProcessorChain) throws Exception {
        // 1.设置请求编码,将其统一设置成UTF-8
        requestProcessorChain.getRequest().setCharacterEncoding("UTF-8");
        // 2.将请求路径末尾的/剔除,为后续匹配Controller请求路径做准备
        // (一般Controller的处理路径是/aaa/bbb,所以如果传入的路径结尾是/aaa/bbb/,
        // 就需要处理成/aaa/bbb)
        String requestPath = requestProcessorChain.getRequestPath();
        //http://localhost:8080/simpleframework requestPath="/"
        if(requestPath.length() > 1 && requestPath.endsWith("/")){
            requestProcessorChain.setRequestPath(requestPath.substring(0, requestPath.length() - 1));
        }
        System.out.println("【INFO】preprocess request "+requestProcessorChain.getRequestMethod()+requestProcessorChain.getRequestPath());
        return true;
    }
}

StaticResourceRequestProcessor

静态资源请求处理,包括但不限于图片、CSS、JS文件等

/*StaticResourceRequestProcessor.java*/
public class StaticResourceRequestProcessor implements RequestProcessor {
    private static final String DEFAULT_TOMCAT_SERVLET = "default";
    private static final String STATIC_RESOURCE_PREFIX = "/static/";
    //tomcat默认请求派发器RequestDispatcher的名称
    RequestDispatcher defaultDispatcher;
    public StaticResourceRequestProcessor(ServletContext servletContext) {
        this.defaultDispatcher = servletContext.getNamedDispatcher(DEFAULT_TOMCAT_SERVLET);
        if(defaultDispatcher == null)
            throw new RuntimeException("There is no default tomcat servlet");
        System.out.println("【INFO】The default servlet for static resource is "+DEFAULT_TOMCAT_SERVLET);
    }

    @Override
    public boolean process(RequestProcessorChain requestProcessorChain) throws Exception {

        //1.通过请求路径判断是否是请求的静态资源 webapp/static
        if (isStaticResource(requestProcessorChain.getRequestPath())) {
            //2.如果是静态资源,则将请求转发给default servlet处理
            defaultDispatcher.forward(requestProcessorChain.getRequest(), requestProcessorChain.getResponse());
            return false;
        }
        return true;
    }
        //通过请求路径前缀(目录)是否为静态资源 /static/
    public boolean isStaticResource(String path){
        return path.startsWith(STATIC_RESOURCE_PREFIX);
    }
}

JspRequestProcessor

类似 StaticResourceRequestProcessor,对 jsp 资源进行处理

public class JspRequestProcessor implements RequestProcessor {
    //jsp请求的RequestDispatcher的名称
    private static final String JSP_SERVLET = "jsp";
    //Jsp请求资源路径前缀
    private static final String  JSP_RESOURCE_PREFIX = "/templates/";
    //jsp的RequestDispatcher,处理jsp资源
    private RequestDispatcher jspServlet;
    public JspRequestProcessor(ServletContext servletContext) {
        jspServlet = servletContext.getNamedDispatcher(JSP_SERVLET);
        if (null == jspServlet) {
            throw new RuntimeException("there is no jsp servlet");
        }
    }
    @Override
    public boolean process(RequestProcessorChain requestProcessorChain) throws Exception {
        if (isJspResource(requestProcessorChain.getRequestPath())) {
            jspServlet.forward(requestProcessorChain.getRequest(), requestProcessorChain.getResponse());
            return false;
        }
        return true;
    }
    //是否请求的是jsp资源
    private boolean isJspResource(String url) {
        return url.startsWith(JSP_RESOURCE_PREFIX);
    }
}

ControllerRequestProcessor

ControllerRequestProcessor的功能

  • 针对特定请求 ,选择匹配的Controller方法进行处理
  • 解析请求里的参数及其对应的值,并赋值给Controller方法的参数
  • 选择合适的Render ,为后续请求处理结果的渲染做准备
  1. 定义 RequestMethod 枚举类和 MVC 相关注解
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
    String value() default "";
    RequestMethod method() default RequestMethod.GET;   //需要先定义RequestMethod枚举类
}

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestParam {
    String value() default "";
    boolean required() default true;
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ResponseBody {
}
  1. 定义相关类
/*ControllerMethod.java*/
/*待执行的Controller及其方法实例和参数的映射*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ControllerMethod {
    //Controller对应的Class对象
    private Class<?> controllerClass;
    //执行的Controller方法实例
    private Method invokeMethod;
    //方法参数名称以及对应的参数类型
    private Map<String, Class<?>> methodParameters;
}
/*RequestPathInfo.java*/
/*存储http请求路径和请求方法*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RequestPathInfo {
    //http请求方法
    private String httpMethod;
    //http请求路径
    private String httpPath;
}
  1. 实现 ConverterUtil 工具类
    • 将基本数据类型转换成空值类型
    • 将String类型转换成对应的基本数据类型
  2. 实现 Controller 请求处理器 ControllerRequestProcessor
/*ControllerRequestProcessor.java*/
public class ControllerRequestProcessor implements RequestProcessor {
    private BeanContainer beanContainer;
    //请求和controller方法的映射集合
    private Map<RequestPathInfo, ControllerMethod> pathControllerMethodMap = new ConcurrentHashMap<>();
    //依靠容器的能力,建立起请求路径、请求方法与Controller方法实例的映射


    public ControllerRequestProcessor() {
        this.beanContainer = BeanContainer.getInstance();
        Set<Class<?>> requestMappingSet = beanContainer.getClassesByAnnotation(RequestMapping.class);
        initPathControllerMethodMap(requestMappingSet);
    }

    //遍历每个被RequestMapping注解的类的被RequestMapping注解的方法,然后维持pathControllerMethodMap这个映射集合
    private void initPathControllerMethodMap(Set<Class<?>> requestMappingSet) {
        if (ValidationUtil.isEmpty(requestMappingSet))
            return;
        //1.遍历所有被@RequestMapping标记的类,获取类上面该注解的属性值作为一级路径
        for (Class<?> requestMappingClass : requestMappingSet) {
            RequestMapping mappingClass = requestMappingClass.getAnnotation(RequestMapping.class);
            String basePath = mappingClass.value();
            if (!basePath.startsWith("/"))
                basePath = "/" + basePath;
            //2.遍历类里所有被@RequestMapping标记的方法,获取方法上面该注解的属性值,作为二级路径
            Method[] methods = requestMappingClass.getMethods();
            if (ValidationUtil.isEmpty(methods))
                continue;
            for (Method method : methods) {
                if (method.isAnnotationPresent(RequestMapping.class)) {
                    RequestMapping mappingMethod = method.getAnnotation(RequestMapping.class);
                    String methodPath = mappingMethod.value();
                    if (!methodPath.startsWith("/"))
                        methodPath = "/" + methodPath;
                    String url = basePath + methodPath;
                    //3.解析方法里被@RequestParam标记的参数:methodName(@RequestParam("param") String param)
                    //获取该注解的属性值,作为参数名,
                    //获取被标记的参数的数据类型,建立参数名和参数类型的映射,也为将来把praram转换成String类型做好铺垫
                    Map<String, Class<?>> methodParam = new HashMap<>();
                    Parameter[] parameters = method.getParameters();
                    if (!ValidationUtil.isEmpty(parameters)) {
                        for (Parameter parameter : parameters) {
                            RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
                            //目前暂定为Controller方法里面所有的参数都需要@RequestParam注解
                            if (requestParam == null)
                                throw new RuntimeException();
                            methodParam.put(requestParam.value(), parameter.getType());
                        }
                    }
                    //4.将获取到的信息封装成RequestPathInfo实例和ControllerMethod实例,放置到映射表里
                    String httpMethod = String.valueOf(mappingMethod.method());
                    RequestPathInfo requestPathInfo = new RequestPathInfo(httpMethod, url);
                    if (this.pathControllerMethodMap.containsKey(requestPathInfo))
                        System.out.println("【WARN】duplicate url:" + requestPathInfo.getHttpPath() + " registration,current class " +
                                requestMappingClass.getName() + " method" + method.getName() + " will override the former one");
                    ControllerMethod controllerMethod = new ControllerMethod(requestMappingClass, method, methodParam);
                    pathControllerMethodMap.put(requestPathInfo, controllerMethod);
                }
            }
        }
    }

    @Override
    public boolean process(RequestProcessorChain requestProcessorChain) throws Exception {
        //1.解析HttpSevletRequest的请求方法,请求路径,获取对应的ControllerMethod实例
        String method = requestProcessorChain.getRequestMethod();
        String path = requestProcessorChain.getRequestPath();
        ControllerMethod controllerMethod = this.pathControllerMethodMap.get(new RequestPathInfo(method, path));
        if (controllerMethod == null) {
            requestProcessorChain.setResultRender(new ResourceNotFoundResultRender(method, path));
            return false;
        }
        //2.解析请求参数,并传递给获取到的ControllerMethod实例去执行
        Object result = invokeControllerMethod(controllerMethod, requestProcessorChain.getRequest());
        //3.根据处理的结果,选择对应的render进行渲染
        setResultRender(result, controllerMethod, requestProcessorChain);
        return true;
    }

    //据不同情况设置不同的渲染器
    private void setResultRender(Object result, ControllerMethod controllerMethod, RequestProcessorChain requestProcessorChain) {
        if (result == null)
            return;
        ResultRender resultRender;
        boolean isJson = controllerMethod.getInvokeMethod().isAnnotationPresent(ResponseBody.class);
        if (isJson)
            resultRender = new JsonResultRender(result);
        else
            resultRender = new ViewResultRender(result);
        requestProcessorChain.setResultRender(resultRender);
    }
    //!!!没写
    private Object invokeControllerMethod(ControllerMethod controllerMethod, HttpServletRequest request) {
        //1.从请求里获取GET或者POST的参数名及其对应的值
        Map<String, String> requestParamMap = new HashMap<>();
        //GET,POST方法的请求参数获取方式
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Map.Entry<String, String[]> parameter : parameterMap.entrySet()) {
            if (!ValidationUtil.isEmpty(parameter.getValue())) {
                //只支持一个参数对应一个值的形式
                requestParamMap.put(parameter.getKey(), parameter.getValue()[0]);
            }
        }
        //2.根据获取到的请求参数名及其对应的值,以及controllerMethod里面的参数和类型的映射关系,去实例化出方法对应的参数
        List<Object> methodParams = new ArrayList<>();
        Map<String, Class<?>> methodParamMap = controllerMethod.getMethodParameters();
        for (String paramName : methodParamMap.keySet()) {
            Class<?> type = methodParamMap.get(paramName);
            String requestValue = requestParamMap.get(paramName);
            Object value;
            //只支持String 以及基础类型char,int,short,byte,double,long,float,boolean,及它们的包装类型
            if (requestValue == null) {
                //将请求里的参数值转成适配于参数类型的空值
                value = ConverterUtil.primitiveNull(type);
            } else {
                value = ConverterUtil.convert(type, requestValue);
            }
            methodParams.add(value);
        }
        //3.执行Controller里面对应的方法并返回结果
        Object controller = beanContainer.getBean(controllerMethod.getControllerClass());
        Method invokeMethod = controllerMethod.getInvokeMethod();
        invokeMethod.setAccessible(true);
        Object result;
        try {
            if (methodParams.size() == 0) {
                result = invokeMethod.invoke(controller);
            } else {
                result = invokeMethod.invoke(controller, methodParams.toArray());
            }
        } catch (InvocationTargetException e) {
            //如果是调用异常的话,需要通过e.getTargetException(),去获取执行方法抛出的异常
            throw new RuntimeException(e.getTargetException());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}

ResultRender矩阵的实现

DefaultResultRender

默认渲染器

/*DefaultResultRender.java*/
public class DefaultResultRender implements ResultRender {
    @Override
    public void render(RequestProcessorChain requestProcessorChain) throws Exception {
        requestProcessorChain.getResponse().setStatus(requestProcessorChain.getResponseCode());
    }
}

InternalErrorResultRender

内部异常渲染器

/*InternalErrorResultRender.java*/
public class InternalErrorResultRender implements ResultRender{
    private String errorMsg;
    public InternalErrorResultRender(String  errorMsg){
        this.errorMsg = errorMsg;
    }
    @Override
    public void render(RequestProcessorChain requestProcessorChain) throws Exception {
        requestProcessorChain.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMsg);
    }
}

ResourceNotFoundResultRender

资源找不到时使用的渲染器

/*ResourceNotFoundResultRender.java*/
public class ResourceNotFoundResultRender implements ResultRender {
    private String httpMethod;
    private String httpPath;
    public ResourceNotFoundResultRender(String method, String path) {
        this.httpMethod = method;
        this.httpPath = path;
    }
    @Override
    public void render(RequestProcessorChain requestProcessorChain) throws Exception {
        requestProcessorChain.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND,
                "获取不到对应的请求资源:请求路径[" + httpPath + "]" + "请求方法[" + httpMethod + "]");
    }
}

JsonResultRender

Json渲染器

/*JsonResultRender.java*/
public class JsonResultRender implements ResultRender {
    private Object jsonData;
    public JsonResultRender(Object jsonData) {
        this.jsonData = jsonData;
    }

    @Override
    public void render(RequestProcessorChain requestProcessorChain) throws Exception {
        // 设置响应头
        requestProcessorChain.getResponse().setContentType("application/json");
        requestProcessorChain.getResponse().setCharacterEncoding("UTF-8");
        // 响应流写入经过gson格式化之后的处理结果
        try(PrintWriter writer = requestProcessorChain.getResponse().getWriter()){
            Gson gson = new Gson();
            writer.write(gson.toJson(jsonData));
            writer.flush();
        }
    }
}

ViewResultRender

页面渲染器

/*ModelAndView.java*/
public class ModelAndView {
    //页面所在的路径
    @Getter
    private String view;
    //页面的data数据
    @Getter
    private Map<String, Object> model = new HashMap<>();

    public ModelAndView setView(String view){
        this.view = view;
        return this;
    }
    //modelAndView.setView("addheadline.jsp").addViewData("aaa", "bbb");
    public ModelAndView addViewData(String attributeName,  Object attributeValue){
        model.put(attributeName, attributeValue);
        return this;
    }
}
/*ViewResultRender.java*/
public class ViewResultRender implements ResultRender {
    public static final String VIEW_PATH = "/templates/";
    private ModelAndView modelAndView;
    /**
     * 对传入的参数进行处理,并赋值给ModelAndView成员变量
     * @param mv
     */
    public ViewResultRender(Object mv) {
        if(mv instanceof ModelAndView){
            //1.如果入参类型是ModelAndView,则直接赋值给成员变量
            this.modelAndView = (ModelAndView)mv;
        } else if(mv instanceof  String){
            //2.如果入参类型是String,则为视图,需要包装后才赋值给成员变量
            this.modelAndView = new ModelAndView().setView((String)mv);
        } else {
            //3.针对其他情况,则直接抛出异常
            throw new RuntimeException("illegal request result type");
        }
    }
    /**
     * 将请求处理结果按照视图路径转发至对应视图进行展示
     * @param requestProcessorChain
     * @throws Exception
     */
    @Override
    public void render(RequestProcessorChain requestProcessorChain) throws Exception {
        HttpServletRequest request = requestProcessorChain.getRequest();
        HttpServletResponse response = requestProcessorChain.getResponse();
        String path = modelAndView.getView();
        Map<String, Object> model = modelAndView.getModel();
        for(Map.Entry<String, Object> entry : model.entrySet()){
            request.setAttribute(entry.getKey(), entry.getValue());
        }
        //JSP
        request.getRequestDispatcher(VIEW_PATH +path).forward(request, response);
    }
}

测试

  1. 修改 HeadLineServiceImpl 的 addHeadLine()
/*HeadLineServiceImpl.java*/
@Override
public Result<Boolean> addHeadLine(HeadLine headLine) {
    System.out.println("【INFO】addHeadLine被执行啦, lineName["+headLine.getLineName()+
            "],lineLink["+headLine.getLineLink()+"],lineImg["+headLine.getLineImg()+"], priority["+headLine.getPriority()+"]");
    Result<Boolean> result = new Result<Boolean>();
    result.setCode(200);
    result.setMsg("请求成功啦");
    result.setData(true);
    return result;
}
  1. 修改 HeadLineOperationController 的 addHeadLine()
/*HeadLineOperationController.java*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addHeadLine(@RequestParam("lineName") String lineName,
                                @RequestParam("lineLink")String lineLink,
                                @RequestParam("lineImg")String lineImg,
                                @RequestParam("priority")Integer priority){
    HeadLine headLine = new HeadLine();
    headLine.setLineName(lineName);
    headLine.setLineLink(lineLink);
    headLine.setLineImg(lineImg);
    headLine.setPriority(priority);
    Result<Boolean> result = headLineService.addHeadLine(headLine);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setView("addheadline.jsp").addViewData("result", result);
    return modelAndView;
}
  1. 通过访问http://localhost:8080/templates/addheadline.jsp,然后随便填满表单点击提交即可通过 post 方式访问http://localhost:8080/headline/add,则可以看到前端页面返回状态码和信息
GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. <one line to give the program's name and a brief idea of what it does.> Copyright (C) <year> <name of author> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: <program> Copyright (C) <year> <name of author> This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see <http://www.gnu.org/licenses/>. The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read <http://www.gnu.org/philosophy/why-not-lgpl.html>.

简介

剑指Java自研框架 决胜Spring源码 展开 收起
Java
GPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/linyilinyi/simpleframework.git
git@gitee.com:linyilinyi/simpleframework.git
linyilinyi
simpleframework
simpleframework
master

搜索帮助