1.init初始化webApplicationContext 然后加载xmlbeanFactory,加载各种xml,
2.按项目包的顺序从上到下依次初始化service和repository,跨包有依赖关系的先加载依赖的service,等加载到这个service的包后也不会再重复初始化这个service,如果没有配置的话接着初始化@Controller
3.然后初始化spring-core-config.xml中的bean标签的类,如:<bean class="com.xxx.xxx.utils.ControllerUtils" lazy-init="false" init-method="init">如果没有在别的类中使用@Autowired时在@controller和@Service之后调用,<bean>可以替换@service,在WebApplicationContext: initialization completed之前调用
4.如:org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) INFO - Root WebApplicationContext: initialization completed in 85329 ms,
5.最后加载spring dispatcherservlet namespace webApplicationContext ,即Refreshing WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Sun Mar 29 11:22:18 CST 2020]; parent: Root WebApplicationContext,
6.按项目包的顺序从上到下依次初始化controller,
7.然后把requestmapping中的url绑定到对应的controller的方法上,例如以下
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:220) INFO - Mapped "{[/queryDetailRows],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.xxx.xxx.xxx.controller.xxxController.queryDetailRows()
以下的xml配置就是去掉步骤2中初始化controller,让其只在初始化dispatcherservlet后初始化controller
applicationContext.xml的内容:
<context:component-scan base-package="com.xxx.seller.module,com.xxx.seller.h5.module,com.xxx.service.module">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.xxx.seller.module,com.xxx.seller.h5.module,com.xxx.extend" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

本文详细阐述了Spring框架的启动过程,包括初始化WebApplicationContext,加载XML BeanFactory,按项目包顺序初始化Service和Repository,以及Controller的注册与RequestMapping的绑定过程。
174万+

被折叠的 条评论
为什么被折叠?



