SpringBoot 教程

SpringBoot 教程

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

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

Spring boot modules

Spring boot 模块

1. 什么是入门模板?

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

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

pom.xml

<!-- Parent pom is mandatory to control versions of child dependencies -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath />
</parent>

<!-- Spring web brings all required dependencies to build web application. -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

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

Dependencies brought in by webmvc starter template

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-json</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
	</dependency>
	<dependency>
		<groupId>org.hibernate.validator</groupId>
		<artifactId>hibernate-validator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
	</dependency>
</dependencies>

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

2. Spring Boot 自动配置

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

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

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

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

Spring boot autoconfiguration packages

Spring boot 自动配置包

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

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

AopAutoConfiguration.java

@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
		AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration 
{

	@Configuration
	@EnableAspectJAutoProxy(proxyTargetClass = false)
	@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
	public static class JdkDynamicAutoProxyConfiguration {

	}

	@Configuration
	@EnableAspectJAutoProxy(proxyTargetClass = true)
	@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
	public static class CglibAutoProxyConfiguration {

	}

}

3. 嵌入式服务器

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

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

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

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

4. 运行应用程序

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

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

MyApplication.java

@SpringBootApplication
public class MyApplication 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(Application.class, args);
    }
}

application.properties

### Server port #########
server.port=8080

### Context root ########
server.contextPath=/home

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

Console

$ 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 面试问题

学习愉快!