https://spring.io/quickstart
官方 quickstart

https://spring.io/projects/spring-boot#learn
这里有各版本的 Reference Doc 和 API Doc
Reference Doc - 有完整的教程/学习路径

https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/htmlsingle/
2.3.0 的 Reference Doc

What

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。
该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

SpringBoot框架有两个非常重要的策略:开箱即用和约定优于配置。
开箱即用,Outofbox,是指在开发过程中,通过在MAVEN项目的pom文件中添加相关依赖包,然后使用对应注解来代替繁琐的XML配置文件以管理对象的生命周期。这个特点使得开发人员摆脱了复杂的配置工作以及依赖的管理工作,更加专注于业务逻辑。
约定优于配置,Convention over configuration,是一种由SpringBoot本身来配置目标结构,由开发者在结构中添加信息的软件设计范式。这一特点虽降低了部分灵活性,增加了BUG定位的复杂性,但减少了开发人员需要做出决定的数量,同时减少了大量的XML配置,并且可以将代码编译、测试和打包等工作自动化。

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

Cli

Spring Boot CLI(Command Line Interface)是一个命令行工具,可以用它来快速构建Spring原型应用。
通过Spring Boot CLI,可以通过编写Groovy脚本来快速的构建出Spring Boot应用,并通过命令行的方式将其运行起来。

安装: https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/htmlsingle/#getting-started-installing-the-cli

  • 直接运行 groovy 脚本 ```java // spring run ./hello.groovy // hello.groovy

@RestController class ThisWillActuallyRun {

  1. @RequestMapping("/")
  2. String home() {
  3. "Hello World!"
  4. }

}

  1. - 创建一个 spring 项目
  2. ```java
  3. spring init --name hello-world --artifactId hello-world --groupId org.springbooks --language java --boot-version 1.5.13.RELEASE --type maven-project --dependencies web,thymeleaf,devtools --extract

https://zhuanlan.zhihu.com/p/46347136

Annotation

类/方法/属性上都可以使用 Annotation

@SpringBootApplication

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes

If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) are automatically registered as Spring Beans.

自动配置机制等, Component扫描范围等, 详见 官方文档

@Component, @Service, @Repository, @Controller 这四个 Annotation 都是创建类的对象 ( 区别 )
同时使用 @Scope 指定对象作用域

  1. @Component( value="accountService")
  2. @Scope("singleton")
  3. public class DefaultAccountService() {}
  4. // 等于
  5. <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>

自动装配

详见 Annotation-based Container Configuration

@Autowired 与@Resource:
1、@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。

2、@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如: @Autowired(required=false)

  1. @Autowired
  2. private DefaultAccountService service;

3、@Resource 是JDK1.6支持的注解默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

  1. @Resource(name="accountService") // 同@Component里的value值
  2. private DefaultAccountService service;

https://www.zhihu.com/question/39356740/answer/80926247

Bean

https://www.zhihu.com/question/19773379

符合一定规范编写的Java类,不是一种技术,而是一种规范。

1、所有属性为private
2、提供默认构造方法
3、提供getter和setter
4、实现serializable接口

  • 在java1996年发布,当年12月即发布了java bean1.00-A,有什么用呢?通过统一的规范可以设置对象的值(get,set方法),这是最初的java bean;
  • 在实际企业开发中,需要实现事务,安全,分布式,javabean就不好用了.sun公司就开始往上面堆功能,这里java bean就复杂为EJB;
  • EJB功能强大,但是太重了.此时出现DI(依赖注入),AOP(面向切面)技术,通过简单的java bean也能完成EJB的事情,这里的java bean简化为POJO;

C#、Scala等比Java新的面向对象语言自身就提供了语言特性来实现这些常用需求,所以根本不需要Java Bean这样繁琐的约定.

Bean Scopes

image.png
https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html
TODO 换新版文档链接

singletion : 默认值
单实例 per container and per bean
不同于 设计模式GoF 里的 singleton

prototype:
多实例 每次创建新的实例

Spring 整合 Web 项目的原理:
服务启动时, 会为每一个项目创建一个 ServletContext 对象
监听器 ( 实现 ServletContextListener ) 监听到 ServletContext 对象创建时:

  • 加载 Spring 配置文件, 创建配置文件中的对象
  • 把创建出来的对象放到 ServletContext 对象里面 ( setAttribute )

用到对象时, 从 ServletContext 获取 ( getAttribute )

Bean 类型

Spring Boot - 图3