Spring Boot学习(一:入门)

提前配置好环境:

JDK 1.8.0_201(及以上版本)

Apache Maven 3.6.0

IntelliJ IDEA 2020

IDEA欢迎页

1.初始化maven

2.初始化JDK设置

一、构建Spring Boot项目

两种方式:Maven方式(1.1运行文件+3.1测试文件)、Spring Initiallizr快捷方式(1.2即全部)。

1.Maven方式构建Spring Boot项目

该方式所有文件及依赖需手工创建(测试项目在后文三,这里仅运行文件的构建)

(1)创建Maven项目

(2)在pom.xml中添加Spring Boot相关依赖

两个依赖:1)统一父类项目管理;2)Web依赖启动器

<!-- 引入Spring Boot依赖 -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
	<!-- 引入Web场景依赖启动器 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>
(3)编写主程序启动类

(ps:好像必须在包内创建类)

标记该类为主程序启动类:@SpringBootApplication

启动主程序类方法:SpringApplication.run()

@SpringBootApplication 
public class ManualChapter01Application {
public static void main(String[] args){
       SpringApplication.run(ManualChapter01Application.class,args);
    }
 }
(4)创建一个用于Web访问的Controller

@RestController                    ==        Spring中@Controller+@ResponseBody注解

@GetMapping("/hello")          ==        Spring框架中@RequestMapping(RequestMethod.GET)注解

@RestController   
public class HelloController {
@GetMapping("/hello")
   public String hello(){
        return "hello Spring Boot";
    }
}
(5)运行项目

启动项目,在浏览器上访问 http://localhost:8080/hello

2.Spring Initiallizr快捷方式构建Spring Boot项目

该方式构建条件:连接网络

该方式将自动生成项目运行与测试文件夹及相关依赖(测试在后文三,这里已生成测试相关)

(1)创建Spring Boot项目

创建好的项目结构如下图所示:

(2)创建一个用于Web访问的Controller
(3)运行项目

2-3同上4-5

二、基础配置

1.核心依赖

1)spring-boot-starter-parent依赖(统一父类项目依赖管理)

对常用技术框架的依赖文件进行统一的版本号管理。

这样,pom.xml引入受管理的依赖文件无需标注依赖文件版本号。(未受管理的依赖文件需使用<version>标签指定依赖文件版本号)

<!-- Spring Boot父项目依赖管理 -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.3.RELEASE</version>
	<relativePath/>
</parent>

2)spring-boot-starter-web依赖(Web依赖启动器)

提供Web开发场景所需的底层所有依赖文件,对Web开发场景所需的依赖文件进行统一管理。

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

2.Spring Boot自动配置

Spring Boot应用的启动入口是@SpringBootApplication注解标注类中的main()方法

@SpringBootApplication能够扫描Spring组件并自动配置Spring Boot。

@SpringBootApplication   =   

@SpringBootConfiguration  +  @EnableAutoConfiguration  +  @ComponentScan

1)@SpringBootConfiguration:表示Spring Boot配置类,可被组件扫描器扫描
2)@EnableAutoConfiguration :表示开启自动配置功能
3) @ComponentScan:组件包扫描器,用于将指定包中的注解类自动装配到Spring的Bean容器中

3.Spring Boot执行流程

主程序启动类 -> 启动项目main()方法 -> 执行方法中SpringApplication.run()  =  启动整个Spring Boot程序

三、单元测试

1.在pom文件中添加spring-boot-starter-test测试启动器

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

2.编写单元测试类

@RunWith(SpringRunner.class) :加载Spring Boot测试注解

@SpringBootTest:加载项目的ApplicationContext上下文环境

@RunWith(SpringRunner.class) 
@SpringBootTest  
public class Chapter01ApplicationTests {
	@Test
	public void contextLoads() {
	}
}

3.编写单元测试方法

@Autowired  //注入了HelloController实例对象
private HelloController helloController;
@Test
public void helloControllerTest() {
	String hello = helloController.hello();
	System.out.println(hello);
}

即整个测试类为:

@RunWith(SpringRunner.class) 
@SpringBootTest  
public class Chapter01ApplicationTests {
    @Autowired
    private HelloController helloController;
    @Test
    public void helloControllerTest() {
	    String hello = helloController.hello();
	    System.out.println(hello);
    }
}

4.运行结果

四、热部署         

避免修改后反复重启服务。开启热部署无须开发人员手动重启项目。

1.在pom文件中添加spring-boot-devtools热部署依赖

<dependency>                
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
</dependency>

2.IDEA中热部署设置

3.热部署测试       

                      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值