手动创建一个Spring Boot 2.x项目

  1. spring boot 2.1.9版本quick start参考文档地址:https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/getting-started-first-application.html#getting-started-first-application-pom

  2. 新建一个Maven项目

  3. pom.xml文件中增加导入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这时项目上有个小红叉,这是由于项目可能有一些必要的组件没有更新,解决方法如下:

// 在项目上右键,选择Maven->Update Project...
// 可以看到小红叉消失了
  1. 书写启动类
package com.fei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zxf on 2019年10月17日
 */
// @RestController注解是@Controller和@ResponseBody两个注解的组合
@RestController
@EnableAutoConfiguration
public class SpringBootDemoApplication {

	/**
	 * 对外提供一个/服务
	 * @return
	 */
	@RequestMapping("/")
	public String home() {
		return "Hello World!";
	}

	// Spring Boot应用启动类
	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemoApplication.class, args);
	}
}
  1. 直接像运行Java程序那样就可以了,然后在浏览器输入http://localhost:8080/访问即可看到Hello world!

  2. 控制台打印的日志信息如下

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019-10-17 23:27:00.528  INFO 9320 --- [           main] com.fei.SpringBootDemoApplication        : Starting SpringBootDemoApplication on Dell-pc with PID 9320 (E:\Developing\workspace-sts\spring-boot-demo\target\classes started by Dell in E:\Developing\workspace-sts\spring-boot-demo)
2019-10-17 23:27:00.542  INFO 9320 --- [           main] com.fei.SpringBootDemoApplication        : No active profile set, falling back to default profiles: default
2019-10-17 23:27:02.268  INFO 9320 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-10-17 23:27:02.348  INFO 9320 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-17 23:27:02.352  INFO 9320 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-10-17 23:27:02.568  INFO 9320 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-10-17 23:27:02.568  INFO 9320 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1945 ms
2019-10-17 23:27:02.930  INFO 9320 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-17 23:27:03.190  INFO 9320 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-17 23:27:03.195  INFO 9320 --- [           main] com.fei.SpringBootDemoApplication        : Started SpringBootDemoApplication in 3.371 seconds (JVM running for 5.42)
2019-10-17 23:28:54.760  INFO 9320 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-10-17 23:28:54.761  INFO 9320 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-10-17 23:28:54.771  INFO 9320 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 10 ms


从日志信息我们可以知道以下信息:

  • 所用Spring Boot的版本为:2.1.9.RELEASE :: Spring Boot :: (v2.1.9.RELEASE)

  • 当前运行的Spring Boot进程PID:with PID 9320

  • 当前应用使用的是内嵌的tomcat,并且运行在8080端口上:o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''

posted on 2019-10-17 23:35  行之间  阅读(904)  评论(0编辑  收藏  举报