springboot学习之——SpringApplication.run方法

springboot学习之——SpringApplication.run方法

目录

springboot 版本3.1.5

第一步

   /
     * Static helper that can be used to run a {@link SpringApplication} from the
     * specified source using default settings.
     * @param primarySource the primary source to load
     * @param args the application arguments (usually passed from a Java main method)
     * @return the running {@link ApplicationContext}
     */
    public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        return run(new Class<?>[] { primarySource }, args);
    }

注释说的啥屁话,这玩意返回一个可配置的应用上下文ConfigurableApplicationContext。看这种名字的类就知道不简单。

run()方法其实创建了一个新的SpringApplication。这个新的SpringApplication会被添加一些属性。

到这里就没了,实例化SpringApplication之后,返回的是更加具体的ConfigurableApplicationContext,也就是说,后面的工作交给了ConfigurableApplicationContext,所以要研究它了。

   /
     * Create a new {@link SpringApplication} instance. The application context will load
     * beans from the specified primary sources (see {@link SpringApplication class-level}
     * documentation for details). The instance can be customized before calling
     * {@link #run(String...)}.
     * @param resourceLoader the resource loader to use
     * @param primarySources the primary bean sources
     * @see #run(Class, String[])
     * @see #setSources(Set)
     /
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.resourceLoader = resourceLoader;//null
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));//这个资源类就是自己
        this.webApplicationType = WebApplicationType.deduceFromClasspath();//推断使用什么类型的web应用。我们经常用WEBMVC_INDICATOR_CLASS
        this.bootstrapRegistryInitializers = new ArrayList<>(
                getSpringFactoriesInstances(BootstrapRegistryInitializer.class));//启动注册实例化
        setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//监听器
        this.mainApplicationClass = deduceMainApplicationClass();//主应用类(就是自己项目的启动类)
    }

第二步

ConfigurableApplicationContext
/
 * SPI interface to be implemented by most if not all application contexts.
 * Provides facilities to configure an application context in addition
 * to the application context client methods in the
 * {@link org.springframework.context.ApplicationContext} interface.
 
 * <p>Configuration and lifecycle methods are encapsulated here to avoid
 * making them obvious to ApplicationContext client code. The present
 * methods should only be used by startup and shutdown code.
 
 * @author Juergen Hoeller
 * @author Chris Beams
 * @author Sam Brannen
 * @since 03.11.2003
 */
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
}

SPI接口(串行外设接口,芯片和外设通信用的),实现了大多数应用上下文的SPI接口。注释翻译了结果还是配置应用上下文。

前面的run()创建了它,程序运行实际是用它来执行的。然而它还是接口,还是要看谁实现了它。

实现它的类也很多。找到一个我们熟悉的看看。AnnotationConfigServletWebApplicationContext

   /
     * Create a new {@link AnnotationConfigServletWebApplicationContext}, deriving bean
     * definitions from the given annotated classes and automatically refreshing the
     * context.
     * @param annotatedClasses one or more annotated classes, e.g. {@code @Configuration}
     * classes
     /
    public AnnotationConfigServletWebApplicationContext(Class<?>... annotatedClasses //注册被注解的类) {
        this();
        register(annotatedClasses);
        refresh();
    }
springboot学习之——SpringApplication.run方法

在Boot项目中使用import javax.servlet.http.HttpServletRequest的方法有多种。其中一种方法是在需要使用HttpServletRequest的类中直接import javax.servlet.http.HttpServletRequest即可。另一种方法是在SpringBoot项目的入口类或者配置类中通过使用@ServletComponentScan注解扫描Servlet中的注解,然后在需要使用HttpServletRequest的类中import javax.servlet.http.HttpServletRequest。具体代码示例如下: 在入口类中使用@ServletComponentScan注解扫描Servlet中的注解: ```java package com.songzihao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan(basePackages = "com.songzihao.springboot.servlet") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在配置类中注册Servlet: ```java package com.wx.boot; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.obtk.servlets.MyServlet01; @Configuration public class ServletConfig { @Bean public ServletRegistrationBean MyServlet1(){ return new ServletRegistrationBean(new MyServlet01(),"/servlets/*"); } } ``` 在需要使用HttpServletRequest的类中直接import javax.servlet.http.HttpServletRequest: ```java import javax.servlet.http.HttpServletRequest; public class MyClass { // 使用HttpServletRequest对象 public void myMethod(HttpServletRequest request) { // ... } } ``` 这样就可以在SpringBoot项目中使用import javax.servlet.http.HttpServletRequest了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot——SpringBoot中使用Servlet的两种方式](https://blog.csdn.net/weixin_43823808/article/details/116992973)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [spring boot最新教程(八):在spring boot中使用servlet的两种方式](https://blog.csdn.net/wx5040257/article/details/79575619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值