从零到一实现SpringBoot

目标:

  实现SpringBoot的基本功能。主要包括:通过SpringBootApplicationChen注解实现web视图解析器,Spring容器等基本功能配置。

实现:

思路:

  自定义SpringApplicationChen,完成第三方框架整合的功能。大致步骤如下:

  • 首先判断启动类是否有SpringBootApplicationChen注解,如果没有,则不启动容器,直接返回。否则获取当前类包名。
  • 然后利用反射机制修改 ComponentScan 属性的默认值,使其定位到启动类下。
  • 接着创建Tomcat容器,配置基本参数。
  • 然后加载静态资源并设置class文件路径。
  • 最后启动服务器并异步等待请求执行。

一、前期准备

  实现自定义注解,并用自定义注解模拟正常的业务逻辑,实现将用户发送给服务器的数据回写给用户的功能。

1、加入依赖

  本项目SpringBoot是对springSpringMVC的整合,所以需要引入springSpringMVCjar包,还需要JSP视图解析器。源码如下:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.njust.myspringboot</groupId>
    <artifactId>myspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>

        <!--Java语言操作tomcat -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.16</version>
        </dependency>

        <!-- tomcat对jsp支持 -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>8.5.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.4.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.4.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、定义UserService 类

  定义UserService类。简单的返回一段字符串。供controller测试使用。源码如下:

UserService .java

package com.njust.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

    public String index() {
        return "springboot 2.0  我正在加载UserService";
    }

}

3、Controller层

3.1、定义IndexController类

  定义IndexController类。调用service层,实现向客户端写入一段文字。源码如下:

IndexController .java

package com.njust.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.njust.service.UserService;

@RestController
public class IndexController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/index", produces = "text/html;charset=UTF-8")
    public String index() {
        return userService.index();
    }

}

3.2、定义UserController 类

  定义UserController类。转发用户请求到指定的JSP。源码如下:

UserController .java

package com.njust.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 跳转页面<br>
 *

 */
@Controller
public class UserController {

    @RequestMapping("/pageIndex")
    public String pageIndex() {
        return "pageIndex";
    }

}

4、定义启动注解类

  定义SpringBootApplicationChen类。SpringBootApplicationChen用于标识该类是自定义SpringBoot类。定义该注解作用范围在接口或类上 。同时保证 注解会在class字节码文件中存在,在运行时可以通过反射获取到。源码如下:

SpringBootApplicationChen .java

package com.njust.annotation;

import java.lang.annotation.*;

/**
 * @author Chen
 * @version 1.0
 * @date 2020/4/3 22:51
 * @description:
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SpringBootApplicationChen {
}

5、定义spring配置类

  定义RootConfigChen类。主要实现将被注解类定义为配置类,同时根据定义的扫描路径,把符合扫描规则的类装配到spring容器中。源码如下:

RootConfigChen .java

package com.njust.springboot;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 根配置<br>
 *

 */
@SuppressWarnings("all")
//定义为配置类
@Configuration
//根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@ComponentScan(value = "deaultValue")
public class RootConfigChen {

}


6、定义web配置类

  定义WebConfigChen类。配置SpringMVC的视图解析器。将被注解类定义为配置类,同时根据定义的扫描路径,把符合扫描规则的类装配到spring容器中。源码如下:

WebConfigChen .java

package com.njust.springboot;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * springmvc 配置信息
 *
 * @EnableWebMvc 开启springmvc 功能<br>

 */
@SuppressWarnings("all")
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "deaultValue" })
public class WebConfigChen extends WebMvcConfigurerAdapter {

    // springboot 整合jsp 最好是war
    // 需要配置视图转换器
    // 创建SpringMVC视图解析器
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        // 可以在JSP页面中通过${}访问beans
        viewResolver.setExposeContextBeansAsAttributes(true);
        return viewResolver;
    }

}

7、设置自动配置

  定义AutoConfigChen类。主要引入SpringSpringMVC的配置信息,扩展AbstractAnnotationConfigDispatcherServletInitializer使其自动配置。源码如下:

AutoConfigChen .java

package com.njust.springboot;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * 加载springmvc--dispatcherServlert<br>
 * 扩展AbstractAnnotationConfigDispatcherServletInitializer的任意类都会自动配置
 */
public class AutoConfigChen extends AbstractAnnotationConfigDispatcherServletInitializer {

    // 加载根配置信息 spring核心
    protected Class<?>[] getRootConfigClasses() {

        System.out.println("--------getRootConfigClasses-----------");

        return new Class[] { RootConfigChen.class };
    }

    // springmvc 加载 配置信息
    protected Class<?>[] getServletConfigClasses() {

        System.out.println("--------getServletConfigClasses-----------");

        return new Class[] { WebConfigChen.class };
    }

    // springmvc 拦截url映射 拦截所有请求
    protected String[] getServletMappings() {

        System.out.println("--------getServletMappings-----------");

        return new String[] { "/" };

    }

}

8、定义核心启动类

  定义SpringApplicationChen类。主要流程如下:首先判断启动类是否有SpringBootApplicationChen注解,如果没有,则不启动容器,直接返回。否则获取当前类包名。然后利用反射机制修改 ComponentScan 属性的默认值,使其定位到启动类下。接着创建Tomcat容器,配置基本参数。然后加载静态资源并设置class文件路径。最后启动服务器并异步等待请求执行。源码如下:

SpringApplicationChen .java

package com.njust.springboot;

import com.njust.annotation.SpringBootApplicationChen;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.springframework.context.annotation.ComponentScan;

import javax.servlet.ServletException;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * @author Chen
 * @version 1.0
 * @date 2020/4/3 22:49
 * @description:
 */
public class SpringApplicationChen {
    public static void run(Class<?> primarySource, String... args) {
        SpringBootApplicationChen annotation = primarySource.getAnnotation(SpringBootApplicationChen.class);

        /**
         * 没有使用注解,直接返回,不启动应用
         */
        if (annotation == null) {
            System.out.println("您没有使用SpringBootApplicationChen注解,程序无法启动");
            return;
        }

        try {
            System.out.println("-------start------------");

            //获取当前类包名
            String packageName = primarySource.getPackage().getName();

            try {
                //利用反射机制修改 ComponentScan 属性的默认值
                dealAnnotation(packageName);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            start();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public static void dealAnnotation(String basePackage) throws NoSuchFieldException, IllegalAccessException {
        ComponentScan componentScan = RootConfigChen.class.getAnnotation(ComponentScan.class);
        if (componentScan != null) {
            //获取 annotation 这个代理实例所持有的 InvocationHandler
            InvocationHandler h = Proxy.getInvocationHandler(componentScan);
            // 获取 AnnotationInvocationHandler 的 memberValues 字段
            Field hField = h.getClass().getDeclaredField("memberValues");
            // 因为这个字段事 private final 修饰,所以要打开权限
            hField.setAccessible(true);
            // 获取 memberValues
            Map memberValues = (Map) hField.get(h);
            // 修改 value 属性值
            memberValues.put("value", new String[]{basePackage});

            System.out.println("componentScan.value(): " + componentScan.value());
        }

        componentScan = WebConfigChen.class.getAnnotation(ComponentScan.class);
        if (componentScan != null) {
            //获取 annotation 这个代理实例所持有的 InvocationHandler
            InvocationHandler h = Proxy.getInvocationHandler(componentScan);
            // 获取 AnnotationInvocationHandler 的 memberValues 字段
            Field hField = h.getClass().getDeclaredField("memberValues");
            // 因为这个字段事 private final 修饰,所以要打开权限
            hField.setAccessible(true);
            // 获取 memberValues
            Map memberValues = (Map) hField.get(h);
            // 修改 value 属性值
            memberValues.put("basePackages", new String[]{basePackage});

            System.out.println("componentScan.basePackages(): " + componentScan.basePackages());
        }

    }

    public static void start() throws ServletException, LifecycleException {

        // 创建Tomcat容器
        Tomcat tomcatServer = new Tomcat();
        // 端口号设置
        tomcatServer.setPort(9090);
        // 读取项目路径 加载静态资源 和WebConfig中配置合起来就是静态资源的真实路径 设置webapp目录
        StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main/chen").getAbsolutePath());
//		StandardContext ctx = new StandardContext();
        // 禁止重新载入
        ctx.setReloadable(false);
        // class文件读取地址
        File additionWebInfClasses = new File("target/classes");
        // 创建WebRoot,即 web项目的根路径
        WebResourceRoot resources = new StandardRoot(ctx);
        // tomcat内部读取Class执行
//		即将java字节码文件路径 和 tomcat的默认写法 /WEB-INF/classes 即web项目的默认路径对应起来
        resources.addPreResources(
                new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
        tomcatServer.start();
        // 异步等待请求执行 方法阻塞,否则main方法运行完毕就退出,tomcat也就关闭了
        tomcatServer.getServer().await();

    }
}

9、定义JSP视图文件

  定义pageIndex.jsp文件。在页面中展示一段文字。源码如下:

pageIndex.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP文件</title>
</head>
<body>

<h1>纯手写SpringBoot框架</h1>
pageIndex.jsp
</body>
</html>

10、定义业务启动类

  定义ChenApplication类,使用方法和SpringBoot类似,只不过使用自定义注解和类实现同样功能。源码如下:

ChenApplication .java

package com.njust;

import com.njust.annotation.SpringBootApplicationChen;
import com.njust.springboot.SpringApplicationChen;

/**
 * @author Chen
 * @version 1.0
 * @date 2020/4/4 7:04
 * @description:
 */
@SpringBootApplicationChen
public class ChenApplication {
    public static void main(String[] args) {
        // 使用Java内置Tomcat运行SpringMVC框架 原理:tomcat加载到
        // springmvc注解启动方式,就会创建springmvc容器
        SpringApplicationChen.run(ChenApplication.class, args);
    }
}

11、测试及结果分析

  首先不使用自定义注解,运行程序。源码如下:

ChenApplication .java

package com.njust;

import com.njust.annotation.SpringBootApplicationChen;
import com.njust.springboot.SpringApplicationChen;

/**
 * @author Chen
 * @version 1.0
 * @date 2020/4/4 7:04
 * @description:
 */
//@SpringBootApplicationChen
public class ChenApplication {
    public static void main(String[] args) {
        // 使用Java内置Tomcat运行SpringMVC框架 原理:tomcat加载到
        // springmvc注解启动方式,就会创建springmvc容器
        SpringApplicationChen.run(ChenApplication.class, args);
    }
}

控制台输出:

Console

您没有使用SpringBootApplicationChen注解,程序无法启动

Process finished with exit code 0

程序直接退出,不予启动容器。
  使用自定义注解,运行程序。源码如下:

ChenApplication .java

package com.njust;

import com.njust.annotation.SpringBootApplicationChen;
import com.njust.springboot.SpringApplicationChen;

/**
 * @author Chen
 * @version 1.0
 * @date 2020/4/4 7:04
 * @description:
 */
@SpringBootApplicationChen
public class ChenApplication {
    public static void main(String[] args) {
        // 使用Java内置Tomcat运行SpringMVC框架 原理:tomcat加载到
        // springmvc注解启动方式,就会创建springmvc容器
        SpringApplicationChen.run(ChenApplication.class, args);
    }
}

控制台输出:

Console

-------start------------
componentScan.value(): [Ljava.lang.String;@e9e54c2
componentScan.basePackages(): [Ljava.lang.String;@448139f0
四月 04, 2020 7:48:50 上午 org.apache.catalina.core.StandardContext setPath
警告: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
四月 04, 2020 7:48:50 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-9090"]
四月 04, 2020 7:48:51 上午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
四月 04, 2020 7:48:51 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Tomcat]
四月 04, 2020 7:48:51 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.5.16
四月 04, 2020 7:48:51 上午 org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
信息: No global web.xml found
四月 04, 2020 7:48:58 上午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 04, 2020 7:48:58 上午 org.apache.catalina.core.ApplicationContext log
信息: 1 Spring WebApplicationInitializers detected on classpath
--------getRootConfigClasses-----------
--------getServletConfigClasses-----------
--------getServletMappings-----------
四月 04, 2020 7:48:58 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
四月 04, 2020 7:48:58 上午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization started
四月 04, 2020 7:48:58 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing Root WebApplicationContext: startup date [Sat Apr 04 07:48:58 CST 2020]; root of context hierarchy
四月 04, 2020 7:48:58 上午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
信息: Registering annotated classes: [class com.njust.springboot.RootConfigChen]
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
信息: Mapped "{[/index],produces=[text/html;charset=UTF-8]}" onto public java.lang.String com.njust.controller.IndexController.index()
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
信息: Mapped "{[/pageIndex]}" onto public java.lang.String com.njust.controller.UserController.pageIndex()
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Sat Apr 04 07:48:58 CST 2020]; root of context hierarchy
四月 04, 2020 7:48:59 上午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization completed in 1337 ms
四月 04, 2020 7:48:59 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'dispatcher'
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'dispatcher': initialization started
四月 04, 2020 7:48:59 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sat Apr 04 07:48:59 CST 2020]; parent: Root WebApplicationContext
四月 04, 2020 7:48:59 上午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
信息: Registering annotated classes: [class com.njust.springboot.WebConfigChen]
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
信息: Mapped "{[/index],produces=[text/html;charset=UTF-8]}" onto public java.lang.String com.njust.controller.IndexController.index()
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
信息: Mapped "{[/pageIndex]}" onto public java.lang.String com.njust.controller.UserController.pageIndex()
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sat Apr 04 07:48:59 CST 2020]; parent: Root WebApplicationContext
四月 04, 2020 7:48:59 上午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'dispatcher': initialization completed in 202 ms
四月 04, 2020 7:48:59 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-9090"]

服务器已经正常启动,我们在浏览器中访问我们的服务网址。http://localhost:9090/pageIndex。效果如下:

在这里插入图片描述

可以发现我们的服务器可以正常提供服务。再测试 http://localhost:9090/index,效果如下:

在这里插入图片描述

同样可以正常访问。说明我们的服务器可以按照预期运行。

总结

流程图

Created with Raphaël 2.2.0 开始 启动类是否有 SpringBootApplicationChen 注解? 获取当前类包名 创建Tomcat容器,配置基本参数 利用反射机制修改ComponentScan 属性的默认值,使其定位到启动类下 加载静态资源并设置class文件路径 最后启动服务器并异步等待请求执行 结束 yes no

重点及易错点

1、工程模式

  使用idea开发项目的时候,一定要使用project模式。如果使用module模式,则无法在项目中获取静态资源和class文件路径。

2、使用反射机制修改注解值

public static void dealAnnotation(String basePackage) throws NoSuchFieldException, IllegalAccessException {
        ComponentScan componentScan = RootConfigChen.class.getAnnotation(ComponentScan.class);
        if (componentScan != null) {
            //获取 annotation 这个代理实例所持有的 InvocationHandler
            InvocationHandler h = Proxy.getInvocationHandler(componentScan);
            // 获取 AnnotationInvocationHandler 的 memberValues 字段
            Field hField = h.getClass().getDeclaredField("memberValues");
            // 因为这个字段事 private final 修饰,所以要打开权限
            hField.setAccessible(true);
            // 获取 memberValues
            Map memberValues = (Map) hField.get(h);
            // 修改 value 属性值
            memberValues.put("value", new String[]{basePackage});

            System.out.println("componentScan.value(): " + componentScan.value());
        }

        componentScan = WebConfigChen.class.getAnnotation(ComponentScan.class);
        if (componentScan != null) {
            //获取 annotation 这个代理实例所持有的 InvocationHandler
            InvocationHandler h = Proxy.getInvocationHandler(componentScan);
            // 获取 AnnotationInvocationHandler 的 memberValues 字段
            Field hField = h.getClass().getDeclaredField("memberValues");
            // 因为这个字段事 private final 修饰,所以要打开权限
            hField.setAccessible(true);
            // 获取 memberValues
            Map memberValues = (Map) hField.get(h);
            // 修改 value 属性值
            memberValues.put("basePackages", new String[]{basePackage});

            System.out.println("componentScan.basePackages(): " + componentScan.basePackages());
        }

    }

  注意一定要更改访问权限,否则会造成字段无法访问的现象。
  有问题欢迎各位读者批评指正。

点个赞再走呗!欢迎留言哦!

发布了22 篇原创文章 · 获赞 4 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/qq_32510597/article/details/105305425