從零開始實現一個簡易的Java MVC框架(八)--製作Starter

spring-boot的Starter

一個項目總是要有一個啓動的地方,當項目部署在tomcat中的時候,經常就會用tomcat的startup.sh(startup.bat)的啓動腳本來啓動web項目

而在spring-boot的web項目中基本會有類似於這樣子的啓動代碼:

@SpringBootApplication
public class SpringBootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

這個方法實際上會調用spring-boot的SpringApplication類的一個run方法:

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        // 1.加載環境變量、參數等
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
        ConfigurableEnvironment environment = prepareEnvironment(listeners,
                                                                 applicationArguments);
        configureIgnoreBeanInfo(environment);
        Banner printedBanner = printBanner(environment);
        // 2.加載Bean(IOC、AOP)等
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(
            SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
        prepareContext(context, environment, listeners, applicationArguments,
                       printedBanner);
        //會調用一個AbstractApplicationContext@refresh()方法,主要就是在這裏加載Bean,方法的最後還會啓動服務器
        refreshContext(context);
        afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass)
                .logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

這段代碼還是比較長的,不過實際上主要就做了兩個事情:1.加載環境變量、參數等 2.加載Bean(IOC、AOP)等。3.如果獲得的ApplicationContextServletWebServerApplicationContext,那麼在refresh()之後會啓動服務器,默認的就是tomcat服務器。

我覺得spring-boot啓動器算是spring-boot中相對來說代碼清晰易懂的,同時也非常容易瞭解到整個spring-boot的流程結構,建議大家能夠去看一下。

實現Starter

瞭解到spring-boot的啓動器的作用和原理之後,我們可以開始實現doodle的啓動器了。

根據剛纔提到的,啓動器要做以下幾件事

  1. 加載一些參數變量
  2. 加載Bean(IOC、AOP)等工作
  3. 啓動服務器

Configuration保存變量

在com.zbw包下創建類Configuration用於保存一些全局變量,目前這個類只保存了現在實現的功能所需的變量。

package com.zbw;
import ...

/**
 * 服務器相關配置
 */
@Builder
@Getter
public class Configuration {

    /**
     * 啓動類
     */
    private Class<?> bootClass;

    /**
     * 資源目錄
     */
    @Builder.Default
    private String resourcePath = "src/main/resources/";

    /**
     * jsp目錄
     */
    @Builder.Default
    private String viewPath = "/templates/";

    /**
     * 靜態文件目錄
     */
    @Builder.Default
    private String assetPath = "/static/";

    /**
     * 端口號
     */
    @Builder.Default
    private int serverPort = 9090;

    /**
     * tomcat docBase目錄
     */
    @Builder.Default
    private String docBase = "";

    /**
     * tomcat contextPath目錄
     */
    @Builder.Default
    private String contextPath = "";
}

實現內嵌Tomcat服務器

在上一章文章從零開始實現一個簡易的Java MVC框架(七)--實現MVC已經在pom.xml文件中引入了tomcat-embed依賴,所以這裏就不用引用了。

先在com.zbw.mvc下創建一個包server,然後再server包下創建一個接口Server

package com.zbw.mvc.server;

/**
 * 服務器 interface
 */
public interface Server {
    /**
     * 啓動服務器
     */
    void startServer() throws Exception;

    /**
     * 停止服務器
     */
    void stopServer() throws Exception;
}

因爲服務器有很多種,雖然現在只用tomcat,但是爲了方便擴展和修改,就先創建一個通用的server接口,每個服務器都要實現這個接口。

接下來就創建TomcatServer類,這個類實現Server

package com.zbw.mvc.server;
import ...

/**
 * Tomcat 服務器
 */
@Slf4j
public class TomcatServer implements Server {

    private Tomcat tomcat;

    public TomcatServer() {
        new TomcatServer(Doodle.getConfiguration());
    }

    public TomcatServer(Configuration configuration) {
        try {
            this.tomcat = new Tomcat();
            tomcat.setBaseDir(configuration.getDocBase());
            tomcat.setPort(configuration.getServerPort());

            File root = getRootFolder();
            File webContentFolder = new File(root.getAbsolutePath(), configuration.getResourcePath());
            if (!webContentFolder.exists()) {
                webContentFolder = Files.createTempDirectory("default-doc-base").toFile();
            }

            log.info("Tomcat:configuring app with basedir: [{}]", webContentFolder.getAbsolutePath());
            StandardContext ctx = (StandardContext) tomcat.addWebapp(configuration.getContextPath(), webContentFolder.getAbsolutePath());
            ctx.setParentClassLoader(this.getClass().getClassLoader());

            WebResourceRoot resources = new StandardRoot(ctx);
            ctx.setResources(resources);
            // 添加jspServlet,defaultServlet和自己實現的dispatcherServlet
            tomcat.addServlet("", "jspServlet", new JspServlet()).setLoadOnStartup(3);
            tomcat.addServlet("", "defaultServlet", new DefaultServlet()).setLoadOnStartup(1);
            tomcat.addServlet("", "dispatcherServlet", new DispatcherServlet()).setLoadOnStartup(0);
            ctx.addServletMappingDecoded("/templates/" + "*", "jspServlet");
            ctx.addServletMappingDecoded("/static/" + "*", "defaultServlet");
            ctx.addServletMappingDecoded("/*", "dispatcherServlet");
            ctx.addServletMappingDecoded("/*", "dispatcherServlet");
        } catch (Exception e) {
            log.error("初始化Tomcat失敗", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void startServer() throws Exception {
        tomcat.start();
        String address = tomcat.getServer().getAddress();
        int port = tomcat.getConnector().getPort();
        log.info("local address: http://{}:{}", address, port);
        tomcat.getServer().await();
    }

    @Override
    public void stopServer() throws Exception {
        tomcat.stop();
    }

    private File getRootFolder() {
        try {
            File root;
            String runningJarPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath().replaceAll("\\\\", "/");
            int lastIndexOf = runningJarPath.lastIndexOf("/target/");
            if (lastIndexOf < 0) {
                root = new File("");
            } else {
                root = new File(runningJarPath.substring(0, lastIndexOf));
            }
            log.info("Tomcat:application resolved root folder: [{}]", root.getAbsolutePath());
            return root;
        } catch (URISyntaxException ex) {
            throw new RuntimeException(ex);
        }
    }
}

這個類主要就是配置tomcat,和配置普通的外部tomcat有點類似只是這裏是用代碼的方式。注意的是在getRootFolder()方法中獲取的是當前項目目錄下的target文件夾,即idea默認的編譯文件保存的位置,如果修改了編譯文件保存位置,這裏也要修改。

特別值得一提的是這部分代碼:

// 添加jspServlet,defaultServlet和自己實現的dispatcherServlet
tomcat.addServlet("", "jspServlet", new JspServlet()).setLoadOnStartup(3);
tomcat.addServlet("", "defaultServlet", new DefaultServlet()).setLoadOnStartup(1);
tomcat.addServlet("", "dispatcherServlet", new DispatcherServlet()).setLoadOnStartup(0);
ctx.addServletMappingDecoded("/templates/" + "*", "jspServlet");
ctx.addServletMappingDecoded("/static/" + "*", "defaultServlet");
ctx.addServletMappingDecoded("/*", "dispatcherServlet");
ctx.addServletMappingDecoded("/*", "dispatcherServlet");

這部分代碼就相當於原來的web.xml配置的文件,而且defaultServletjspServlet這兩個servlet是tomcat內置的servlet,前者用於處理靜態資源如css、js文件等,後者用於處理jsp。如果有安裝tomcat可以去tomcat目錄下的conf文件夾裏有個web.xml文件,裏面有幾行就是配置defaultServletjspServlet

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

而dispatcherServlet就是從零開始實現一個簡易的Java MVC框架(七)--實現MVC這一節中實現的分發器。這三個servlet都設置了LoadOnStartup,當這個值大於等於0時就會隨tomcat啓動也實例化。

實現啓動器類

在com.zbw包下創建一個類作爲啓動器類,就是類似於SpringApplication這樣的。這裏起名叫做Doodle,因爲這個框架就叫doodle嘛。

package com.zbw;
import ...

/**
 * Doodle Starter
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public final class Doodle {

    /**
     * 全局配置
     */
    @Getter
    private static Configuration configuration = Configuration.builder().build();

    /**
     * 默認服務器
     */
    @Getter
    private static Server server;

    /**
     * 啓動
     */
    public static void run(Class<?> bootClass) {
        run(Configuration.builder().bootClass(bootClass).build());
    }

    /**
     * 啓動
     */
    public static void run(Class<?> bootClass, int port) {
        run(Configuration.builder().bootClass(bootClass).serverPort(port).build());
    }

    /**
     * 啓動
     */
    public static void run(Configuration configuration) {
        new Doodle().start(configuration);
    }

    /**
     * 初始化
     */
    private void start(Configuration configuration) {
        try {
            Doodle.configuration = configuration;
            String basePackage = configuration.getBootClass().getPackage().getName();
            BeanContainer.getInstance().loadBeans(basePackage);
           //注意Aop必須在Ioc之前執行
            new Aop().doAop();
            new Ioc().doIoc();

            server = new TomcatServer(configuration);
            server.startServer();
        } catch (Exception e) {
            log.error("Doodle 啓動失敗", e);
        }
    }
}

這個類中有三個啓動方法都會調用Doodle@start()方法,在這個方法裏做了三件事:

  1. 讀取configuration中的配置
  2. BeanContainer掃描包並加載Bean
  3. 執行Aop
  4. 執行Ioc
  5. 啓動Tomcat服務器

這裏的執行是有順序要求的,特別是Aop必須要在Ioc之前執行,不然注入到類中的屬性都是沒被代理的。

修改硬編碼

在之前寫mvc的時候有一處有個硬編碼,現在有了啓動器和全局配置,可以把之前的硬編碼修改了

對在com.zbw.mvc包下的ResultRender類裏的resultResolver()方法,當判斷爲跳轉到jsp文件的時候跳轉路徑那一行代碼修改:

try {
    Doodle.getConfiguration().getResourcePath();
    // req.getRequestDispatcher("/templates/" + path).forward(req, resp);
    req.getRequestDispatcher(Doodle.getConfiguration().getResourcePath() + path).forward(req, resp);
} catch (Exception e) {
    log.error("轉發請求失敗", e);
    // TODO: 異常統一處理,400等...
}

啓動和測試項目

現在doodle框架已經完成其功能了,我們可以簡單的創建一個Controller來感受一下這個框架。

在com包下創建sample包,然後在com.sample包下創建啓動類APP

package com.sample;

import com.zbw.Doodle;
public class App {
    public static void main(String[] args) {
        Doodle.run(App.class);
    }
}

然後再創建一個ControllerDoodleController:

package com.sample;
import com.zbw.core.annotation.Controller;
import com.zbw.mvc.annotation.RequestMapping;
import com.zbw.mvc.annotation.ResponseBody;

@Controller
@RequestMapping
public class DoodleController {
    @RequestMapping
    @ResponseBody
    public String hello() {
        return "hello doodle";
    }
}

接着再運行App的main方法,就能啓動服務了。


源碼地址:doodle

原文地址:從零開始實現一個簡易的Java MVC框架(八)--製作Starter

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章