Spring Boot
Spring Boot简介
Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.
这是摘自Spring Boot官网的原文 大概理解就是: Spring Boot 是所有基于 Spring 进行开发项目的起点。而Spring Boot 的设计是为了让你的 Spring 应用程序能够更快速的运行 并且尽可能减少你的配置文件。
Spring Boot是什么
它并不是什么新的框架,只是使用了 “习惯优于配置”的方式,来减轻以往spring项目中存在大量配置文件的问题,大大提高了项目开发效率,能够让你的项目更快速的运行起来,Spring Boot 整合了所有框架,并通过Maven来管理所有的jar依赖
Spring Boot快速搭建
开始
使用idea
版本: java8 spring boot 2.0.5
创建项目 New Project->Spring Initializr->点击Next
填写你的Group 和 Artifact 点击Next
点击web选项 把右边的web选项也勾上
填写project name 和 location
点击Finish
下面就是整个工程的结构了 看上去挺清爽的,少了很多配置文件,了解下都有些什么文件:
-
SpringbootApplication 一个带有 main() 方法的类,用于启动应用程序
-
SpringbootApplicationTests 一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文
-
application.properties 一个空的 properties 属性配置文件,可以根据需要添加配置属性
-
pom.xml Maven 构建配置文件
项目创建好了 那么接下来 先写个简单的例子来看看如何使用Spring boot 开发 _____________________________
HelloWord
创建HelloController
1.在 SpringbootApplication类的路径下创建一个 controller类
Spring Boot 会自动扫描标注有 @SpringBootApplication 所在类的 同级包以及子级包里的 Bean
入口类建议就配置在 com.yb.springboot 包下 (grounpID + arctifactID 下)
@RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版
2.到SpringbootApplication类中来启动项目 右键->Run…
** Spring Boot 内置了 Tomcat 所以无需再配置 **
看后台打印的信息 Spring boot启动
3.访问测试
Spring Boot 热部署
添加pom.xml配置
- 添加依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
- 添加插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
idea的配置
如果是Eclipse中,只需上面的配置就已经能够正常使用了,但idea并不会在修改源代码后自动编译启动,需要使用ctrl+F9手动make项目
- 修改源代码
将hello改为hello2 使用ctrl+F9手动编译代码
开启idea自动make
还有种方式就是修改idea的配置,因为idea默认并不会自动编译代码,只需修改一些配置项
- ctrl+shift+a
输入Registry
找到 compiler.automake.allow.when.app.running项 打上勾
再使用ctrl+shift+a 输入:make project
找到 make project automatically 项 打上勾
测试
修改源码 看看是否配置成功
在修改源码后 看到后打印的日志 项目被停止后又启动了
END