原文: https://howtodoinjava.com/spring-boot-tutorials/

Spring Boot 是一个 Spring 框架模块,为 Spring 框架提供 RAD(快速应用程序开发)功能。 它高度依赖入门模板功能,该功能非常强大且可以完美运行。

SpringBoot 教程 - 图1

Spring boot 模块

1. 什么是入门模板?

Spring Boot 启动器是模板,其中包含启动特定功能所需的所有相关传递依赖项的集合。 例如,如果要创建 Spring WebMVC 应用程序,则在传统设置中,您自己会包含所有必需的依赖项。 这就留下了版本冲突的机会,这最终会导致更多运行时异常

使用 Spring boot,要创建 MVC 应用程序,只需导入spring-boot-starter-web依赖项。

pom.xml

  1. <!-- Parent pom is mandatory to control versions of child dependencies -->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.1.6.RELEASE</version>
  6. <relativePath />
  7. </parent>
  8. <!-- Spring web brings all required dependencies to build web application. -->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>

spring-boot-starter-web依赖关系之上,内部导入所有给定的依赖关系并将其添加到您的项目中。 请注意,某些依赖关系是直接的,某些依赖关系进一步引用了其他入门模板,这些模板可过渡地下载更多的依赖关系。

另外,请注意不需要将版本信息提供到子依赖项中。 相对于父级启动器的版本,已解决所有版本(在我们的示例中为2.0.4.RELEASE)。

Dependencies brought in by webmvc starter template

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-json</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-tomcat</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.hibernate.validator</groupId>
  16. <artifactId>hibernate-validator</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-webmvc</artifactId>
  25. </dependency>
  26. </dependencies>

阅读更多: Spring Boot 入门模板列表

2. Spring Boot 自动配置

通过@EnableAutoConfiguration注解启用自动配置。 Spring Boot 自动配置扫描类路径,在类路径中找到库,然后尝试为它们猜测最佳配置,最后配置所有此类 bean。

自动配置会尝试尽可能智能化,并且在您定义更多自己的配置时会自动退出。

自动配置始终在注册用户定义的 bean 之后应用。

Spring Boot 自动配置逻辑在spring-boot-autoconfigure.jar中实现。 Yoy 可以在此处验证软件包的列表

SpringBoot 教程 - 图2

Spring boot 自动配置包

例如,查看 Spring AOP 的自动配置。 它执行以下操作

  1. 扫描类路径以查看是否存在EnableAspectJAutoProxyAspectAdviceAnnotatedElement类。
  2. 如果不存在类,则不会为 Spring AOP 进行自动配置。
  3. 如果找到了类,则使用 Java config 注解@EnableAspectJAutoProxy配置 AOP。
  4. 它检查属性spring.aop的值可以是truefalse
  5. 基于属性的值,设置proxyTargetClass属性。

AopAutoConfiguration.java

  1. @Configuration
  2. @ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
  3. AnnotatedElement.class })
  4. @ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
  5. public class AopAutoConfiguration
  6. {
  7. @Configuration
  8. @EnableAspectJAutoProxy(proxyTargetClass = false)
  9. @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
  10. public static class JdkDynamicAutoProxyConfiguration {
  11. }
  12. @Configuration
  13. @EnableAspectJAutoProxy(proxyTargetClass = true)
  14. @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
  15. public static class CglibAutoProxyConfiguration {
  16. }
  17. }

3. 嵌入式服务器

SpringBoot 应用程序始终将 tomcat 作为嵌入式服务器依赖项。 这意味着您可以从命令提示符运行 Spring Boot 应用程序,而无需使用复杂的服务器基础结构。

您可以根据需要排除 tomcat,并包括任何其他嵌入式服务器。 或者,您可以完全排除服务器环境。 全部基于配置。

例如,以下配置排除了 tomcat包括了 Jetty作为嵌入式服务器。

pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-jetty</artifactId>
  14. </dependency>

4. 运行应用程序

运行应用程序,我们需要使用@SpringBootApplication注解。 在幕后,它们相当于@Configuration@EnableAutoConfiguration@ComponentScan

它可以扫描配置类,文件并将它们加载到 spring 上下文中。 在下面的示例中,执行从main()方法开始。 它开始加载所有配置文件,对其进行配置,然后根据/resources文件夹中application.properties文件中的应用程序属性运行应用程序。

MyApplication.java

  1. @SpringBootApplication
  2. public class MyApplication
  3. {
  4. public static void main(String[] args)
  5. {
  6. SpringApplication.run(Application.class, args);
  7. }
  8. }

application.properties

  1. ### Server port #########
  2. server.port=8080
  3. ### Context root ########
  4. server.contextPath=/home

要执行该应用程序,可以从 IDE 中运行main()方法,例如 eclipse ,或者可以构建 jar 文件并从命令提示符下执行。

Console

  1. $ java -jar spring-boot-demo.jar

5. Spring Boot 的优点

  • Spring Boot 帮助解决依赖冲突。 它标识所需的依赖项并为您导入它们。
  • 对于所有依赖项,它都具有兼容版本的信息。 它最大程度地减少了运行时类加载器问题。
  • 它的“预设默认配置”方法可帮助您配置幕后最重要的部分。 仅在需要时才覆盖它们。 否则,一切都会完美地进行。 它有助于避免样板代码,注解和 XML 配置。
  • 它提供了嵌入式 HTTP 服务器 Tomcat,因此您可以快速进行开发和测试。
  • 它与 eclipse 和智能想法等 IDE 具有出色的集成。

6. Spring Boot 配置

  1. Spring Boot 注解
  2. Spring Boot 登录指南
  3. Spring Boot – Spring Boot 入门模板
  4. Spring Boot – 入门级父项依赖项
  5. Spring Boot – 获取所有已加载的 bean
  6. Spring Boot – @SpringBootApplication和自动配置
  7. Spring Boot – 更改应用程序根目录
  8. Spring Boot – 配置 Jetty 服务器
  9. Spring Boot – Tomcat 默认端口
  10. Spring Boot – WAR 打包示例
  11. Spring Boot – 日志和 yml 配置
  12. Spring Boot – 日志和属性配置
  13. Spring Boot – SSL(https)
  14. Spring Boot – CommandLineRunner
  15. Spring Boot – 开发人员工具模块教程
  16. Spring Boot – 在@Bean@Compoment中注入应用程序参数
  17. Spring Boot 嵌入式服务器日志
  18. Spring Boot 嵌入式 tomcat 配置

7. 开发 REST API 和 SOAP Web 服务

  1. Spring Boot – REST API
  2. Spring Boot – Jersey
  3. Spring Boot – Spring HATEOAS 示例
  4. Spring Boot – 请求验证 REST API
  5. Spring Boot – 基于角色的安全性
  6. Spring Boot – SOAP Web 服务
  7. Spring Boot – SOAP 客户端
  8. Spring Boot 2 和 ehcache 3 示例

8. 其他有用的话题

  1. 在启动时禁用横幅
  2. Spring Boot – JSP 视图
  3. Spring Boot – 自定义PropertyEditor
  4. Spring Boot – @EnableScheduling
  5. Spring Boot – JMSTemplate
  6. Spring Boot – RSS / ATOM Feed
  7. Spring Boot 读取资源文件夹读取文件
  8. Spring Boot 面试问题

学习愉快!