B站学习视频:SpringBoot视频教程(idea版)_2018_Java视频_spring boot_springboot核心篇+springboot整合篇-尚硅谷
1 创建maven工程
目录结构
2 导入springboot相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
说明
2.1 父依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
让我们戳进去看一看:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
它也有一个父依赖,接着戳:
该依赖配置了很多版本号,因此只要其中配置了版本号的依赖,在以后添加依赖时都可以不声明版本号,用父依赖的版本号。
<properties>
<activemq.version>5.15.9</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.75</appengine-sdk.version>
<artemis.version>2.6.4</artemis.version>
<aspectj.version>1.9.4</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.9.13</byte-buddy.version>
<caffeine.version>2.6.2</caffeine.version>
<cassandra-driver.version>3.6.0</cassandra-driver.version>
<classmate.version>1.4.0</classmate.version>
<commons-codec.version>1.11</commons-codec.version>
<commons-dbcp2.version>2.5.0</commons-dbcp2.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.6.2</commons-pool2.version>
<couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>
<couchbase-client.version>2.7.7</couchbase-client.version>
<derby.version>10.14.2.0</derby.version>
......
2.2 spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- spring-boot-starter-xxx:spring boot的场景启动器
- spring-boot-starter-web:web场景启动器,导入了web模块正常运行所依赖的组件。
戳进去看一看:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
3 编写主程序,用于启动Spring Boot应用
1)在com.keke包下新建HelloWorldApplication类
2)添加注解 @SpringBootApplication,用来标注此为spring boot主程序类
3)编写main方法
package com.keke;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication用来标注此为spring boot主程序类
*/
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
//启动spring boot应用
SpringApplication.run(HelloWorldApplication.class, args);
}
}
说明
3.1 @SpringBootApplication
@SpringBootApplication标注在某一个类上,表示该类为spring boot的主配置类。运行该类的main方法可以启动spring boot应用。
先戳:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
-
@SpringBootConfiguration:Spring Boot配置类,由以下注解组合而成:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration注解@Configuration表示该类为配置类,类似于配置文件,其实也是容器的一个组件@Component
-
@EnableAutoConfiguration:开启自动配置功能
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
@AutoConfigurationPackage:自动配置包
将主配置类(@SpringBootApplication标注的类)所在的包,及该包下的所有子包中的所有组件扫描到spring容器中。
4 编写业务逻辑,如controller,service等
1)新建controller类
2)编写代码
package com.keke.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "Hello World";
}
}
注解
- @Controller:该类为一个控制器类
- @RequestMapping("/hello"):该方法用来处理“/hello”请求
- @ResponseBody:通过转换器将返回对象转换为指定的格式,然后写入Response对象的body中,通常用来返回JSON数据或XML数据。
5 启动并测试
6 简化部署
1)导入插件
<!--用于打包的插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2)打包:Maven Projects->springboot-01-helloWorld->Lifecycle->package
成功后,target文件夹如下所示
将该jar包复制到桌面,win+R输入cmd调出命令行,在桌面的目录下运行:
java -jar springboot-01-helloWorld-1.0-SNAPSHOT.jar