环境配置
1、JDK版本:Java8
2、IDE版本:Intellij IDEA 2018.1
3、系统:MAC OS
构建步骤
1、创建项目
打开Intellij IDEA,点击File->New->Project
image
选择“Spring Initializer”,点击next
image
这步可以直接next,也可以填写自己需要的信息,然后next
image
无脑next
image
点击Finish,完成项目构建,生成初始项目及项目结构如下
image
maven文件
<pre style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-family: Menlo; font-size: 9pt;"><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.spring.boot</groupId> <artifactId>tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>tutorial</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project></pre>
注意:
自动生成的起步依赖,如spring-boot-starter、spring-boot-starter-test和spring-boot-starter-web。点击进入起步依赖spring-boot-starter,发现其依赖了spring-core等。spring-boot-starter-test依赖了junit、spring-test等,而spring-boot-starter-web则包括启动web应用所需要的依赖,比如spring-boot-starter-tomcat、spring-webmvc等(从这些实现细节可以看出Spring Boot是为了方便日常开发做了针对性抽象和封装,底层用的还是你熟悉的Spring)
build内容是声明部署插件,该pom定义该项目是以jar方式运行,其实也是可以通过war包方式运行的
TutorialApplication
该类是Spring Boot的核心注解,项目的启动入口。
<pre style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-family: Menlo; font-size: 9pt;">import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TutorialApplication { public static void main(String[] args) { SpringApplication.run(TutorialApplication.class, args); } } </pre>
注意:
点击进入注解SpringBootApplication,你会发现,其本质是一个组合注解,包括@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan等注解。具体各个注解类的作用后面有机会再说
Spring Boot项目启动是通过执行SpringApplication的静态方法run启动项目,该方法底层是初始化上下文并启动容器。
其他文件
TutorialApplicationTests文件和appication.properties文件都是空壳,可以按照需要实现。
2、启动项目
在TutorialApplication类上右键Run 'TutorialApplication'即可启动应用
image
添加Controller类
如果你觉得上面的效果太空洞了,需要验证,那么可以新建一个HelloController类
<pre style="margin: 0.5em 0px; padding: 0.4em 0.6em; border-radius: 8px; background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-family: Menlo; font-size: 9pt;">package com.spring.boot.tutorial.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/")public class HelloController { @GetMapping(value = "/index") public String index() { return "hello jackie"; } } </pre>
此时重新运行TutorialApplication,打开浏览器,输入localhost:8080/index,效果如下
image
作者:Jackie_Zheng
链接:https://www.jianshu.com/p/fe4dda32a0e2