【校招VIP】Spring启动过程

10月17日 收藏 0 评论 0 java开发

【校招VIP】Spring启动过程

转载声明:文章来源https://blog.csdn.net/coralinelee/article/details/74931689

关于spring的用处

关于日常spring使用,最常用的就是两个IoC和AOP。IoC是用于创建对象以及管理对象,实现高内聚低耦合,AOP实现了日志日路、性能统计、异常处理等面向切面的功能。

使用spring的好处除了以上两点,还有JDBC的简单实现,比如hibernate和mybatis等;与第三方web框架集成简单,也有一套自己的web层框架Spring MVC。相当于就是一个轻量级的粘合剂。

关于spring的配置

spring的配置是写在xml中的,文件名称可以按情况来取,比如spring.xml或者spring-mybatis.xml等。

接下来介绍一些常用的配置项,如果一个文件中的内容比较长,可以引入其他文件,可以更好的配置不同模块。

<import resource="classpath*:datasource.xml"/>

在xml中配置bean的方式比较麻烦,现在一般采用注解的方式实现bean的自动载入,需要配置组件扫描和注解处理器

<context:component-scan base-package="com.xxx"/>

spring的启动过程

IoC容器的启动过程就是上下文建立的过程

整个spring的启动过程,就是上下文加载的过程。

对于非web程序,IoC容器的启动,可以在main中手动加载context,具体过程参见以下代码,关闭容器的时候,在JVM里注册一个关闭钩子,调用bean上对应的析构回调方法。

public final class Boot {

public static void main(final String[] args) throws Exception {
AbstractApplicationContext ctx
= new ClassPathXmlApplicationContext(new String []{"beans.xml"});

// add a shutdown hook for the above context...
ctx.registerShutdownHook();

// app runs here...
// get bean from container
ctx.getBean(“beanName”);
// main method exits, hook is called prior to the app shutting down...
}
}

对于web程序,跑在tomcat中,配置一般在web.xml中。这里需要先了解tomcat的加载顺序

1.读取context-param和listener,创建ServletContext上下文,项目中的所有部分共享这个上下文

2.将context-param转化为键值对,交给ServletContext,listener和filter初始化时会用到这些信息

3.将listener转换为类实例,创建监听

4.监听中有contextInitialized方法,可以获取context-param对应的值,进行一些操作。这里进行的操作会比之前所有的Servlet都要早,会在web项目完全启动之前被执行,比如加载日志配置信息,应用相关基本信息等。

@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext test = sce.getServletContext();
String context_param_value = test.getInitParameter("context-param的键");
LogConfig.setAppID(sce.getServletContext().getInitParameter(KEY_APP_ID));
RAppSetting.setAppID(sce.getServletContext().getInitParameter(KEY_APP_ID));
...//Remainder omitted
}

tomcat的加载顺序为context-param -> listener -> filter -> servlet,一般在listener中加载是spring。

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>

需要注意的是对于servlet和filter,这种有mapping配置的节点,需要先定义name,再定义mapping,不然会报错,并且tomcat是按照配置节点的出现顺序依次调用doFilter()方法。

<servlet>
<servlet-name>xxServlet</servlet-name>
<servlet-class>com.xxx.xxx</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xxServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

包含spring MVC的web项目启动过程

这里先明确几个概念

1.父上下文

父上下文是可以没有的,即可以只使用DispatcherServlet来加载spring的配置。

listener监听器加载配置后,创建的WebApplicationContext上下文,保存在ServletContext中,key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的值,取出对象可以用WebApplicationContextUtils.getWebApplicationContext(ServletContext)。这个就是spring的IoC容器,其对应的Bean定义的配置由web.xml中的context-param标签指定。

2.子上下文

Spring MVC用来拦截相关请求的时候,会用到DispatchServlet,这个servlet是一个标准的前端控制器,用于转发、匹配、处理每个servlet请求。

每个DispatchServlet对应一个子上下文,保存在ServletContext中,用以持有spring MVC相关的bean。key 是”org.springframework.web.servlet.FrameworkServlet.CONTEXT”+Servlet名称。每个request对象中包含这个子上下文对象,key是 DispatcherServlet.class.getName() + “.CONTEXT”。取出对象可以用RequestContextUtils.getWebApplicationContext(request);

3.两者之间的关系

子上下文可以访问父上下文中的bean,但是父上下文不可以访问子上下文中的bean。

接下来理解一下过程

启动listener即ContextLoaderListener,其中的contextInitialized方法会被调用,初始化父上下文,即WebApplicationContext。

启动servlet,其中spring MVC用到的DispatcherServlet会被初始化,这里会先获取根上下文,再调用initStrategies方法初始化自己的上下文,完成之后,这个子上下文也被放到ServletContext中,以便后续使用。

这样每个DispatcherServlet就持有自己的上下文,即拥有自己独立的bean空间,同时各个servlet共享相同的bean,即根上下文定义的那些bean。

C 0条回复 评论

帖子还没人回复快来抢沙发