开发者

简易版SpringBoot自定义模拟实现

开发者 https://www.devze.com 2024-01-02 10:20 出处:网络 作者: 梦一年
目录模块创建依赖导入代码编写实现run方法逻辑创建Spring容器创建Tomcat对象与DispatcherServlet并绑定启动总结SpringBoot作为目前最流行的框架之一,同时是每个程序员必须掌握的知识,其提供了丰富的功能模块和开箱
目录
  • 模块创建
  • 依赖导入
  • 代码编写
  • 实现run方法逻辑
  • 创建Spring容器
  • 创建Tomcat对象与DispatcherServlet并绑定启动
  • 总结

SpringBoot作为目前最流行的框架之一,同时是每个程序员必须掌握的知识,其提供了丰富的功能模块和开箱即用的特性,极大地提高了开发效率和降低了学习成本,使得开发人员能够更专注于业务逻辑的实现,而无需过多关注底层框架的配置和集成。编程客栈本文模拟实现简易版SpringBoot。

模块创建

创建一个Springboot源码模块,主要用来实现SpringBoot的核心编程逻辑,类似导入SpringBoot依赖。

创建一个应用模块Demo,用来实现业务逻辑测试我们自己编写好的Springboot代码。

依赖导入

由于SpringBoot是依赖于Spring的也依赖SpringMVC,所以我们也得依赖Spring和SpringMVC,导入Spring与SpringMVC的相关jar。

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.18</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.18</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.18</version>
    </dependency>


    <dependency>
        <groupId>Javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>


    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.60</version>
    </dependency>
</dependencies>

而Demo模块就可以类似平常一样,随便写需要什么导入什么,但是得依赖于我们自己写的SpringBoot模块。

<dependencies>
    <depend编程ency>
        <groupId>com.simulate.example</groupId>
        <artifactId>springboot</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

代码编写

Demo模块的代码直接就正常编写逻辑,定义一个Controller,Service一个接口请求方法执行“/test”。

SpringBoot模块,效仿真正的SpringBoot项目在项目启动类里面goihlNiegF存在一个注解,传入配置类,然后调用run方法即可。

/**
 * @author dream
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Configuration
@ComponentScan
public @interface DemoSpringBootApplication {}


public class MySpringApplication {
    public static void run(Class clazz){


    }
}

首先我们需要去定义一个核心的注解类和一个启动类DemoSpringApplication。

定义完这两个类此时我们就可以去编写Demo业务的启动类,之前是表示@SpringBootApplication,现在通过我们自定义的注解来实现。

@DemoSpringBootApplication
public class MyApplication { 
    public static void main(String[] args) {
        MySpringApplication.run(MyApplication.class);
    }
}

实现run方法逻辑

我想着当run方法结束后,我们就可以在浏览器里面访问我们之前定义好的test路径,那么run方法必定会去启动Tomcat服务才能够在浏览器里面访问,所在方法里面必须去启动一个Tomcat服务。

同时我们需要扫描得到Spring的相关类,同时还得利用Springmvc去进行相关操作,将Dispatchehttp://www.devze.comrServlet加入到Tomcat中。

在run方法里面需要实现逻辑:创建一个Spring容器,创建Tomcat对象,创建DispatcherServlet对象并且和前面创建出来的Spring容器进行绑定将DispatcherServlet添加到Tomcat中,最后启动Tomcat。

创建Spring容器

public static void run(Class clazz) {
  AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.register(clazz); 
    applicationContext.refresh(); 
}

创建AnnotationConfigWebApplicationContext容易传入class类就表示该clazz为配置类,Spring就会去扫描类上的相关注解,这时候就会扫描到我们自己写好的@DemoSpringBootApplication注解,然后该注解内存存在@ComponentScan注解等都会一并去扫描实现,ComponentScan就是去扫描路径找到bean对象,如果没有指定路径默认就是配置类所在包路径,就会将Demo的Controller类扫描到Spring中,并将访问地址扫描到其中。

创建Tomcat对象与DispatcherServlet并绑定启动

public static void startTomcat(WebApplicationContext applicationContext){
    Tomcat tomcat = new Tomcat();
    Server server = tomcat.getServer();
    Service service = server.findService("Tomcat");
    Connector connector = new Connector();
    connector.setPort(8081);
    Engine engine = new StandardEngine();
    engine.setDefaultHost("localhost");
    Host host = new StandardHost();
    host.setName("localhost");
    String contextPath = "";
    Context context = new StandardContext();
    context.setPath(contextPath);
    context.addLifecycleListener(new Tomcat.FixContextListener());
    host.addChild(context);
    engine.addChild(host);
    service.setContainer(engine);
    service.addConnector(connector);
    tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(applicationContext));
    context.addServletMappingDecoded("/*", "dispatcher");
    try {
        tomcat.start();
    } catch (LifecycleException e) {
        javascripte.printStackTrace();
    }
}

startTomcat方法就是启动Tomcat,需要传递一个容器,然后绑定8081端口,在浏览器中我们就可以通过“localhost:8081/test”来访问。

总结

开篇简单模拟一下SpringBoot的过程,后期逐步来分析一下SpringBoot中的相关源码。其中大量运用Spring的相关知识。

到此这篇关于简易版SpringBoot自定义模拟实现的文章就介绍到这了,更多相关模拟实现简易版SpringBoot内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消