From 338662e4dfc0342e039a1bb0abf28dc5b62ddd9f Mon Sep 17 00:00:00 2001 From: Kou Shuang Date: Wed, 11 Sep 2019 21:09:43 +0800 Subject: [PATCH] Update springboot-questions.md --- .../framework/spring/springboot-questions.md | 85 ++++++++++++++++++- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/docs/system-design/framework/spring/springboot-questions.md b/docs/system-design/framework/spring/springboot-questions.md index dee84784..fc09e091 100644 --- a/docs/system-design/framework/spring/springboot-questions.md +++ b/docs/system-design/framework/spring/springboot-questions.md @@ -20,12 +20,91 @@ 7. Spring Boot提供命令行接口(CLI)工具,用于开发和测试Spring Boot应用程序,如Java或Groovy。 8. Spring Boot提供了多种插件,可以使用内置工具(如Maven和Gradle)开发和测试Spring Boot应用程序。 -### 为什么需要Spring Boot? +### 3. 为什么需要Spring Boot? Spring Framework旨在简化J2EE企业应用程序开发。Spring Boot Framework旨在简化Spring开发。 ![why-we-need-springboot](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/why-we-need-springboot.png) -### 什么是 Spring Boot Starters? +### 4. 什么是 Spring Boot Starters? + +Spring Boot Starters 是一系列依赖关系的集合,因为它的存在,项目的依赖之间的关系对我们来说变的更加简单了。举个例子:在没有Spring Boot Starters之前,我们开发REST服务或Web应用程序时; 我们需要使用像Spring MVC,Tomcat和Jackson这样的库,这些依赖我们需要手动一个一个添加。但是,有了 Spring Boot Starters 我们只需要一个只需添加一个**spring-boot-starter-web**一个依赖就可以了,这个依赖包含的字依赖中包含了我们开发REST 服务需要的所有依赖。 + +```xml + + org.springframework.boot + spring-boot-starter-web + +``` + +### 如何在Spring Boot应用程序中使用Jetty而不是Tomcat? + +Spring Boot Web starter使用Tomcat作为默认的嵌入式servlet容器, 如果你想使用 Jetty 的话只需要修改pom.xml(Maven)或者build.gradle(Gradle)就可以了。 + +**Maven:** + +```xml + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + org.springframework.boot + spring-boot-starter-jetty + +``` + +**Gradle:** + +```groovy +compile("org.springframework.boot:spring-boot-starter-web") { + exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' +} +compile("org.springframework.boot:spring-boot-starter-jetty") +``` + +说个题外话,从上面可以看出使用 Gradle 更加简洁明了,但是国内目前还是 Maven 使用的多一点,我个人觉得 Gradle 在很多方面都要好很多。 + +### 介绍一下@SpringBootApplication注解 + +```java +package org.springframework.boot.autoconfigure; +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@SpringBootConfiguration +@EnableAutoConfiguration +@ComponentScan(excludeFilters = { + @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), + @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) +public @interface SpringBootApplication { + ...... +} +``` + +```java +package org.springframework.boot; +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Configuration +public @interface SpringBootConfiguration { + +} +``` + +可以看出大概可以把 `@SpringBootApplication `看作是 `@Configuration`、`@EnableAutoConfiguration`、`@ComponentScan ` 注解的集合。根据 SpringBoot官网,这三个注解的作用分别是: + +- `@EnableAutoConfiguration`:启用 SpringBoot 的自动配置机制 +- `@ComponentScan`: 扫描被`@Component` (`@Service`,`@Controller`)注解的bean,注解默认会扫描该类所在的包下所有的类。 +- `@Configuration`:允许在上下文中注册额外的bean或导入其他配置类 -Spring Boot Starters 是一系列 \ No newline at end of file -- GitLab