SpringBoot(一)初遇
环境: IDEA 2018.1.3 , jdk 1.8 , maven 3.3.9
零
第一次接触springboot, 如何学习比较困惑, 思前想后最后决定从文档来学习, 以下为学习中的参考资料:
-
官方手册: Spring Boot Reference Guide
脉络比较清晰, 给了学习的方向, 如下图:
-
官方指南: Guide
提供了很多demo, 并且有源码, 有步骤 -
第三方博客教程, 选择了两位大佬博客教程:
- 方志朋的 springboot非官方教程
- 翟永超的 Spring Boot基础教程
- 记录方式: 码云(代码+笔记) + 博客园(简要笔记)
一、New Project
- SpringBoot2.1.1系统要求: Java8+, maven 3.3+,spring5.13+
- SpringBoot模块简要: 13.5 Starters
打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了;
然后等待maven下载spring boot需要的jar包即可
运行SpringbootFirstApplication的main方法即可启动项目
0. Building an Application with Spring Boot
- jdk1.8+
- maven 3.2+
然后根据手册编写HelloController.java
Create a simple web application
https://spring.io/guides/gs/spring-boot/
1. Serving Web Content with Spring MVC
- 码云: gs-serving-web-content
- 指南: https://spring.io/guides/gs/serving-web-content/
- Java模板引擎thymeleaf: https://www.thymeleaf.org/
2. Building a RESTful Web Service
- 码云:gs-rest-service
- 指南: https://spring.io/guides/gs/rest-service/
二、Import Project
可参考官方手册: https://spring.io/guides/gs/intellij-idea/
三、配置类
@Configuration
@Import 导入额外的配置类
@ComponentScan 扫描所有Spring组件
@ImportResource 加载xml配置文件
- 文档: 15. Configuration Classes
四、自动装配
- @SpringBootApplication简化注解
- 禁用指定类
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) // 禁用指定的配置类
public class MyConfiguration {
}
- 文档: 16. Auto-configuration
五、 Spring Beans和依赖注入
@ComponentScan 自动扫描bean
@Autowired 做构造函数注入
如果bean有一个构造函数,则可以省略@Autowired
- 文档: 17. Spring Beans and Dependency Injection
六、关于@SpringBootApplication
- 文档: 8. Using the @SpringBootApplication Annotation
七、运行
- 文档: 19. Running Your Application
八、开发者工具
- 完全打包程序时会被禁用
- 相关配置
- 远程应用 (20.5 Remote Applications)
- 文档: 20. Developer Tools
idea中使用热部署
- 手册: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-hotswapping
修改配置和引入jar包后注意重新启动项目
1.pom.xml
devtools插件
org.springframework.boot
spring-boot-devtools
true
maven依赖
org.springframework.boot
spring-boot-maven-plugin
true
2.修改设置
3.registry设置