SpringBoot start and automatic assembly principle

A, servlet2 (old spring-mvc)

  1. Profiles:
  • web.xml: The main configuration items startup items
  • application-context.xml: The main scanning package configuration items, all kinds of bean, transaction management
  • springMVC.xml: a main configuration controller package scanning viewresolver, parameter parser
  1. Boot process:
  • Each project requires a spring to initialize spring-context startup, the initialization process can be triggered in the context of the program main method for non-web project.
  • Since the start of the project at the entrance web container, so developers can not directly trigger initialization spring-context, we need to start the process initialization process of the container and stronger association.
  • Due to parse web.xml file loads when the container is started, it is a good place to establish precisely web.xml association. web.xml configured in ContextLoadListener point is this association that listens to the container will start to initialize the spring-context.
  • Since DispatcherServlet is a global core controller, all requests need to be to intercept it, so his start timing needs to be configured, the default is only the first instance of a request comes in it.
  • However, it can be configured to start the initializing its container, the initialization process DispatcherServlet springMVC.xml to read the configuration file, to complete the mapping controller, view resolution, and so the parameter analysis.

Two, servlet3

  1. Servlet3 provides that if the current project under classPath, there META-INF / services / javax.servlet.ServletContainerInitializer configuration file, and the file name ServletContainerInitializer wrote a whole class implementation class, class method to achieve onStartup will start when the container It is called. ServletContainerInitializer interface class in the spring is arranged org.springframework.web.SpringServletContainerInitializer.
  2. The above-mentioned spring is provided to achieve a ServletContainerInitializer @HandlesTypes (WebApplicationInitializer.class) based on the annotation, the annotation will brackets WebApplicationInitializer interfaces for all classes implemented in assembly project is a set, traversing the onStartup method of SpringServletContainerInitializer examples of call instances and 11 onStartup method, WebApplicationInitializer class interface is provided SpringBoot SpringBootServletInitializer, spring-contextdispatcherServlet initializing operation at this time items can be placed in this method, the thus substituted web.xml.
  3. By @Configuration annotation, need to be configured before the spring-application-context.xml bean poured into the container, then the corresponding xml configuration file was replaced.
  4. The main function is to scan springMVC.xml having @Controller class, since the start @SpringBootApplication annotation class integrated @ComponentScan, it scans all the default start classes the current class packets and sub-packets, the functions are substituted springMVC.xml a.

Three, spring-boot boot

  1. When performing the main function of the spring-boot startup class, it instantiates a Tomcat object and then call a series of methods Tomcat object:
 Tomcat tomcat = new Tomcat();
 tomcat.addWebApp("/", "D:/app/");  //调用此方法是将项目当做web项目放入容器,同时也会触发ServletContainerInitializer的onStartup方法
 tomcat.setPort(8080);
 tomcat.start();
 tomcat.getServer().await();  //启动线程,阻塞等待
  1. @ResponseBody way to add annotations represents the return value to the message conversion process, otherwise it to view parser.
@Configuration
public class GsonMessageConverterConfig implements WebMvcConfigurer {

	@Override //自定义消息解析器
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(new GsonHttpMessageConverter());
	}

	@Override //自定义视图解析器
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.jsp("/", ".jsp");
	}
}

Guess you like

Origin www.cnblogs.com/JaxYoun/p/12597676.html