Spring Boot (1) —— 创建简单的hello world项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014454538/article/details/88419483

1. 创建Spring Boot项目

  • 创建一个新的Module,选择Spring Initializer

在这里插入图片描述

  • 点击next,在新的界面中填写以下信息
    在这里插入图片描述

  • 选择Web --> Web ,点击next

在这里插入图片描述

  • 点击Finish,完成创建.

在这里插入图片描述

  • 创建好的工程结构如下图所示,在com.lucy.boot1包下有一个自动生成的Boot1Application类,该类是Spring Boot项目的启动类,运行其中的主方法,项目就可以启动了,十分简单快捷。
    在这里插入图片描述

2. 创建Spring Boot项目

  • Boot1Application类的内容如下
package com.lucy.boot1;

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

@RestController
@SpringBootApplication
public class Boot1Application {
    @RequestMapping("/")
    public String helloSpringBoot(){
        System.out.println("Hello Spring Boot");
        return "Hello Spring Boot";
    }
    public static void main(String[] args) {
        SpringApplication.run(Boot1Application.class, args);
    }
}

@SpringBootApplication是Sprnig Boot项目的核心注解,主要目的是开启自动配置。
@RestController注解等价于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出。
@RequestMapping此注解提供的是路由信息。它告诉Spring任何来自“/”路径的请求都会被映射到helloSpringBoot()方法。

  • Run “Boot1Application”的方式启动项目,发现他会自动启动tomcat,运行端口为8080
    在这里插入图片描述
    在浏览器中输入http://localhost:8080/,显示内容如下:
    在这里插入图片描述
  • mvn spring-boot:run的方式启动项目,发现与 Run “Boot1Application”的方式一样,启动信息如下:
C:\Users\lucy\IdeaProjects\boot1>  mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building boot1 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) > test-compile @ boot1 >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ boot1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ boot1 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\lucy\IdeaProjects\boot1\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ boot1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\lucy\IdeaProjects\boot1\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ boot1 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\lucy\IdeaProjects\boot1\target\test-classes
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) < test-compile @ boot1 <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) @ boot1 ---

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

2019-03-12 14:27:14.740  INFO 2100 --- [           main] com.lucy.boot1.Boot1Application          : Starting Boot1Application on DESKTOP-58RL20I with PID 2100 (C:\Users\lucy\IdeaProjects\boot1\target\classes started by lucy in C:\Us
ers\lucy\IdeaProjects\boot1)
2019-03-12 14:27:14.744  INFO 2100 --- [           main] com.lucy.boot1.Boot1Application          : No active profile set, falling back to default profiles: default
2019-03-12 14:27:15.783  INFO 2100 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-12 14:27:15.807  INFO 2100 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-12 14:27:15.807  INFO 2100 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-12 14:27:15.817  INFO 2100 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.lib
rary.path: [C:\Program Files\java\jdk\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Python27\;C:\Python27\Scripts;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(
R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Component
s\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86
)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Python36\;C:\Python36\Scripts\;C:\Program Files\apache-maven-3.5.2\bi
n;C:\Program Files\java\jdk\bin;"C:\Program Files\apache-tomcat8\bin;C:\Program Files\apache-tomcat8\lib";C:\Program Files\mysql5.7\bin;C:\Python36\Scripts\;C:\Python36\;C:\Program Files (x86)\MPICH2\bin;;.]
2019-03-12 14:27:15.905  INFO 2100 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-12 14:27:15.905  INFO 2100 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1117 ms
2019-03-12 14:27:16.119  INFO 2100 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-12 14:27:16.292  INFO 2100 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-12 14:27:16.297  INFO 2100 --- [           main] com.lucy.boot1.Boot1Application          : Started Boot1Application in 1.862 seconds (JVM running for 4.912)
  • 选择 maven --> package 进行打包时,会打包成一个可以直接运行的 JAR 文件,使用java -jar命令就可以直接运行。
    在这里插入图片描述
    打包成功,显示信息如下:
    在这里插入图片描述
    工程目录中,会有相应的jar包出现:
    在这里插入图片描述
    打开cmd,切换到jar包所在目录:
>cd C:\Users\lucy\IdeaProjects\boot1\target
>java -jar boot1-0.0.1-SNAPSHOT.jar

运行结果如下,也可以在浏览器中查看信息。
在这里插入图片描述

参考链接:
Spring Boot干货系列:(一)优雅的入门篇
使用IntelliJ IDEA创建SpringBoot项目
快速创建springboot项目:使用http://start.spring.io/网页创建,使用IDEA的Spring Initializr创建,使用SpringBoot CLI创建

猜你喜欢

转载自blog.csdn.net/u014454538/article/details/88419483