1. What is SpringBoot framework?
SpringBoot is a fast integrated third-party framework; Simplified XML configuration, fully annotated; Built-in HTTP servers (Jetty and Tomcat), and ultimately execute as Java applications.
Note: The Bottom layer of SpringCloud relies on SpringBoot to implement the microservice interface (SpringBoot Web component integrated with SpringMVC) and uses SpringMVC to write the interface.
2. SpringBoot principle analysis.
2.1. Quick integration of third-party frameworks. The principle is: Maven child parent dependency, which is equivalent to the Jar of the environment that needs to be integrated to encapsulate dependency information.
2.2. No configuration file (annotated). How do I initialize it? How does Tomcat start without web.xml? Annotations started above spring2.5, and the SpringMVC built-in annotations load the entire SpringMVC container. Write SpringMVC configuration initialization in Java code. @ EnableWebMvc annotation.
2.3. The built-in HTTP server uses the Java language to create tomcat containers and load class files.
3. Code implementation.
3.1. The POM. XML file is the JAR package that you need to rely on.
<dependencies> <! </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> The < version > 8.5.16 < / version > < / dependency > <! -- spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> < version > 5.0.4. RELEASE < / version > < scope > compile < / scope > < / dependency > <! -- spring-mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 5.0.4. RELEASE < / version > < scope > compile < / scope > < / dependency > <! <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper</artifactId> The < version > 8.5.16 < / version > < / dependency > < / dependencies >Copy the code
3.2. Start the main class of the program.
public class AppTomcat { public static void main(String[] args) throws ServletException, LifecycleException {// Use Java's built-in Tomcat to run the SpringMVC framework: Tomcat loads into // SpringMVC annotated startup mode and creates the SpringMVC container start(); } public static void start() throws ServletException, LifecycleException {// create Tomcat container Tomcat tomcatServer = new Tomcat(); // TomcatServer.setport (9090); StandardContext CTX = (StandardContext) tomcatServer.addWebApp ("/", new File("src/main").getAbsolutePath()); // Disable reloading of ctx.setreloadable (false); File additionWebInfClasses = new File("target/classes"); // Create WebRoot WebResourceRoot Resources = new StandardRoot(CTX); / / tomcat internal reading Class execution resources. AddPreResources (new DirResourceSet (resources, "/ WEB - INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); tomcatServer.start(); Tomcatserver.getserver ().await(); // Wait asynchronously for the request to execute tomcatServer.getServer().await(); }}Copy the code
3.3. Load SpringMVC DispatchServlet class.
Initialization AbstractAnnotationConfigDispatcherServletInitializer this class is responsible for “the Spring container, for SpringMVC containers, configuration DispatcherServlet”. The getRootConfigClasses() method is used to get the Spring application container configuration file. Here we are given a predefined rootconfig.class; GetServletConfigClasses takes care of getting the Spring MVC application container, passing in the pre-defined webConfig. class; The getServletMappings() method is responsible for specifying the path to be mapped by the DispatcherServlet. Given “/”, this means that the DispatcherServlet handles all requests to this application.
Public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {/ / loaded root spring core configuration information protected Class<? >[] getRootConfigClasses() { return new Class[] { RootConfig.class }; } // protected Class<? >[] getServletConfigClasses() { return new Class[] { WebConfig.class }; Protected String[] getServletMappings() {return new String[] {"/"}; // getServletMappings() {return new String[] {"/"}; }}Copy the code
Load the Spring container
/** ** @configuration @componentScan ("com.xxxx") public class componentConfig {}Copy the code
Load the SpringMVC container
Just as there are many ways to configure dispatcherServlets, there are many ways to start the Spring MVC feature. It turns out that we use MVC: Annotation-driven elements in XML files to launch the annotation-driven Spring MVC feature.
/** * SpringMVC Configuration information ** @enableWebMVC Enabling springMVC <br> */ @Configuration @enableWebMvc @ComponentScan(basePackages = {"com.xxx.controller"}) public Class WebConfig extends WebMvcConfigurerAdapter {// SpringBoot integrates JSP // Need to configure the view converter / / create for SpringMVC view parser @ Bean public ViewResolver ViewResolver () {InternalResourceViewResolver ViewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); / / can be accessed through ${} in JSP page beans viewResolver. SetExposeContextBeansAsAttributes (true); return viewResolver; }}Copy the code
3.6 Controller and Service layer methods.
@RestController public class IndexController { @Autowired private UserService userService; @RequestMapping(value = "/index", produces = "text/html; Charset = utF-8 ") public String index() {// return "SpringBoot ok!" return userService.index(); }}Copy the code
@service public class UserService {public String index() {return "SpringBoot2"; ; }}Copy the code
4, the specific source code, please check the code cloud: gitee.com/llsydn_admi…