资源

入门文档
https://spring.io/guides
https://spring.io/quickstart

API 文档
https://docs.spring.io/spring/docs/current/

视频教程 @网易云课堂 https://study.163.com/course/courseMain.htm?courseId=1004475015
视频教程 @尚硅谷 https://www.bilibili.com/video/BV1Et411Y7tQ

一些博客教程
Spring系列教程 https://github.com/wuyouzhuguli/SpringAll
一步到位springboot https://blog.csdn.net/chaitoudaren/article/details/105624082
老实人spring源码解析 https://blog.csdn.net/chaitoudaren/article/details/104833634

关于官方文档
这里是一些小项目类型的入门教程
image.png
这里是各个部分详细的教程和文档
image.png

试玩

https://spring.io/guides/gs/rest-service/

For all Spring applications, you should start with the Spring Initializr.
The Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the setup for you.


If you use Gradle, you can run the application by using ./gradlew bootRun
Alternatively, you can build the JAR file by using ./gradlew build and then run the JAR file, as follows:

  1. java -jar build/libs/gs-rest-service-0.1.0.jar


If you use Maven, you can run the application by using ./mvnw spring-boot:run
Alternatively, you can build the JAR file with ./mvnw clean package and then run the JAR file, as follows:

  1. java -jar target/gs-rest-service-0.1.0.jar


springboot的项目往往以xxxx-starter命名,springboot之所以受欢迎便是因为其约定大于配置的思想,默认的帮我们做了很多事情
引入一个spring-boot-starter-web,实际上springboot就帮我们引入了spring-webmvctomcat等等包

SpringBoot 有许多默认配置, 可以在 /src/main/java/resources/application.properties 中添加自定义配置
或者可以用 yml 格式, 文件改成 application.yml
springboot默认读取的是application.yml,我们需要在application.yml中指定当前生效的配置文件

  1. spring:
  2. profiles:
  3. active: dev

springboot会按照约定寻找application-*.yml文件
springboot的配置文件可以在以下 5 处,按照优先级从高到低的顺序为:

  • 启动命令时指定目录,也就是java -jar xxx强制指定的
  • 当前项目同级目录下/config中,也就是../config/application.yml
  • 当前项目同级目录,也就是../application.yml
  • 项目内config包中,也就是src/main/resources/config/application.yml
  • 项目内,也就是src/main/resources/application.yml

概念

https://www.w3cschool.cn/wkspring/dcu91icn.html
image.png

The @RestController and @RequestMapping annotations are Spring MVC annotations (they are not specific to Spring Boot). See the MVC section in the Spring Reference Documentation for more details.

IOC

将对象的创建交给Spring框架处理
两种方式

  • 配置文件
  • 注解

XML
dom4j
工厂模式
反射

  1. <bean id="userService" class="com.example.UserService" />
  1. # Spring 内容帮创建对象的大致的过程
  2. String className = "xxx"; // dom4j 从 xml 中解析处理
  3. Class aClass = Class.formName(className);
  4. UserService service = aClass.newInstance();

在测试中使用
image.png

Annotation

@Component

@Component is a Spring bean and a Singleton.
If the class belongs to the service layer you may want to annotate it with @Service instead
But have in mind that in order for these annotations to be detected, you need to place this line in applicationContext.xml:

  1. <context:component-scan base-package="com.yourcompany" />

However, you can override this behavior using the @Scope annotation. For example: @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

@Controller

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Controller
  5. @ResponseBody
  6. public @interface RestController {
  7. @AliasFor(
  8. annotation = Controller.class
  9. )
  10. String value() default "";
  11. }

@responseBody注解将controller的方法返回的数据写入到response对象的body区。
也就是说@Controller返回的可能是视图也可能是数据。但是@RestController则一定是数据,并且自动的写入到responsebody

Spring Bean Scopes

https://www.baeldung.com/spring-bean-scopes

IDE 配置

插件 RestfulToolkit
约等于集成版的 Postman, 该工具可以自动发现controller层的相关请求,不用自己手动写地址或参

插件 Lombok

插件 阿里巴巴Java规约
https://github.com/alibaba/p3c/tree/master/idea-plugin

Annex

教程 @W3Cschool
https://www.w3cschool.cn/wkspring/pesy1icl.html

spring initializer
https://start.spring.io/