简易版的SpringBoot是如何实现的!!!


prtyaa
prtyaa 2024-01-05 22:08:51 53373 赞同 0 反对 0
分类: 资源 标签: 运维
目前RocketMQ总算可以告一段落了,在学习的过程中是否遇到什么难点,前面的知识点是否还依稀记得,后面有时间还是需要好好复习一遍,不过总的来说还是得会使用,学习源码是为了能够让你更加了解该技术,在碰到问题的同时能够更加快速的发现和解决问题。

 模拟实现简易版SpringBoot

 

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

模块创建

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

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

依赖导入

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


    
        org.springframework
        spring-context
        5.3.18
    
    
        org.springframework
        spring-web
        5.3.18
    
    
        org.springframework
        spring-webmvc
        5.3.18
    


    
        javax.servlet
        javax.servlet-api
        4.0.1
    


    
        org.apache.tomcat.embed
        tomcat-embed-core
        9.0.60
    

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


    
        com.simulate.example
        springboot
        1.0-SNAPSHOT
    

代码编写

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

SpringBoot模块,效仿真正的SpringBoot项目在项目启动类里面存在一个注解,传入配置类,然后调用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去进行相关操作,将DispatcherServlet加入到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) {
        e.printStackTrace();
    }


}

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



如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
prtyaaL2
粉丝 1 资源 1949 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  123
统信桌面专业版【全盘安装UOS系统】介绍  118
银河麒麟桌面操作系统安装佳能打印机驱动方法  109
银河麒麟桌面操作系统 V10-SP1用户密码修改  103
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!