今天我们来详细探究Helloworld是如何运行的,从而了解Spring Boot的运行原理,let's go!
整合后的示例代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
上面的代码由下面几个重要部分组成:
1. 启动类和@SpringBootApplication注解:
HelloWorldApplication
类是程序主类,并在类上声明了@SpringBootApplication
注解。这个注解是Spring Boot的核心,它包括了多个注解的组合,这个注解告诉Spring Boot这是一个Spring应用程序的入口点。
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
,用于自动配置Spring应用程序。
2. main方法:
众所周知,main方法
是所有Java程序的入口点,即主函数。在Spring Boot应用程序中,通常由SpringApplication.run
方法来启动应用程序。这个方法接受两个参数:启动类的Class对象和命令行参数。
3. SpringApplication.run方法:
SpringApplication.run
方法用于启动Spring Boot应用程序。它执行以下关键步骤:
- 创建一个Spring应用程序上下文(ApplicationContext):Spring Boot会自动配置应用程序上下文,包括加载应用程序的配置、扫描组件并创建bean。
- 执行自动配置:Spring Boot会根据classpath中的依赖和条件(Conditional)来自动配置应用程序。这包括配置数据库连接、Web服务器、安全性等。
- 启动嵌入式Web服务器:Spring Boot默认使用内嵌的Web服务器(例如Tomcat)来运行Web应用程序。它会根据配置启动服务器,并监听指定的端口。
- 注册Shutdown Hook:Spring Boot会注册一个JVM关闭钩子,以确保在应用程序关闭时执行必要的清理操作。
4. 控制器和路由:
上述代码定义了一个简单的控制器HelloWorldController
,它处理/hello
路径的HTTP GET请求,并返回"Hello, World!"。Spring Boot会自动扫描这个控制器,并将它注册到应用程序上下文中。
5. 内嵌Web服务器:
Spring Boot默认使用内嵌的Web服务器来处理HTTP请求。在示例中,Tomcat是默认的内嵌Web服务器。Spring Boot会自动配置和启动Tomcat服务器,并监听默认的HTTP端口(通常是8080)。
6. 处理HTTP请求:
当浏览器或HTTP客户端发送GET请求到/hello
路径时,内嵌Web服务器将请求路由到HelloWorldController
的hello
方法。该方法返回字符串"Hello, World!",该字符串作为HTTP响应发送回客户端。
7. Spring Boot自动配置:
Spring Boot的一个关键特性是自动配置,能够根据classpath中的依赖和应用程序的配置自动配置Spring应用程序。例如,如果应用程序依赖于数据库,Spring Boot会自动配置数据源。如果应用程序是Web应用程序,它会自动配置Servlet容器。这样,开发者无需手动配置大部分常见的组件,可以更专注于业务逻辑的开发。
如今的开发人员很幸运,因为Spring Boot框架大大简化了Spring应用程序的开发和部署,使开发者能够更轻松地创建高效的Java应用程序。下面来看看使用经典Spring框架要如何实现相同的功能。
- 创建一个Maven项目并添加以下依赖项到
pom.xml
:<dependencies> <!-- Spring Core和Web模块的依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.9</version> </dependency> <!-- Servlet API依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
- 创建一个Spring配置文件,通常命名为
applicationContext.xml
,并将它放在src/main/resources
目录下。配置文件示例:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 启用注解扫描 --> <context:component-scan base-package="com.example.controller"/> <!-- 配置Spring MVC --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
- 创建一个控制器类,通常命名为
HelloWorldController.java
:package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, World!"); return "hello"; // 视图的名称,对应/WEB-INF/views/hello.jsp } }
- 创建一个JSP视图,将其放在
src/main/webapp/WEB-INF/views
目录下,命名为hello.jsp
:<!DOCTYPE html> <html> <head> <title>Hello World</title> </head> <body> <h1>${message}</h1> </body> </html>
- 配置Web.xml文件(默认在
src/main/webapp/WEB-INF
目录下),以将请求映射到Spring的DispatcherServlet
:<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
浏览器输入:http://localhost:8080/your-context-path/hello,显示
"Hello, World!"代表正常运行。