Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 338662e

Browse files
Update springboot-questions.md
1 parent e3ad38a commit 338662e

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

‎docs/system-design/framework/spring/springboot-questions.md‎

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,91 @@
2020
7. Spring Boot提供命令行接口(CLI)工具,用于开发和测试Spring Boot应用程序,如Java或Groovy。
2121
8. Spring Boot提供了多种插件,可以使用内置工具(如Maven和Gradle)开发和测试Spring Boot应用程序。
2222

23-
### 为什么需要Spring Boot?
23+
### 3. 为什么需要Spring Boot?
2424

2525
Spring Framework旨在简化J2EE企业应用程序开发。Spring Boot Framework旨在简化Spring开发。
2626

2727
![why-we-need-springboot](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/why-we-need-springboot.png)
2828

29-
### 什么是 Spring Boot Starters?
29+
### 4. 什么是 Spring Boot Starters?
30+
31+
Spring Boot Starters 是一系列依赖关系的集合,因为它的存在,项目的依赖之间的关系对我们来说变的更加简单了。举个例子:在没有Spring Boot Starters之前,我们开发REST服务或Web应用程序时; 我们需要使用像Spring MVC,Tomcat和Jackson这样的库,这些依赖我们需要手动一个一个添加。但是,有了 Spring Boot Starters 我们只需要一个只需添加一个**spring-boot-starter-web**一个依赖就可以了,这个依赖包含的字依赖中包含了我们开发REST 服务需要的所有依赖。
32+
33+
```xml
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
```
39+
40+
### 如何在Spring Boot应用程序中使用Jetty而不是Tomcat?
41+
42+
Spring Boot Web starter使用Tomcat作为默认的嵌入式servlet容器, 如果你想使用 Jetty 的话只需要修改pom.xml(Maven)或者build.gradle(Gradle)就可以了。
43+
44+
**Maven:**
45+
46+
```xml
47+
<!--从Web启动器依赖中排除Tomcat-->
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-web</artifactId>
51+
<exclusions>
52+
<exclusion>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-tomcat</artifactId>
55+
</exclusion>
56+
</exclusions>
57+
</dependency>
58+
<!--添加Jetty依赖-->
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-jetty</artifactId>
62+
</dependency>
63+
```
64+
65+
**Gradle:**
66+
67+
```groovy
68+
compile("org.springframework.boot:spring-boot-starter-web") {
69+
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
70+
}
71+
compile("org.springframework.boot:spring-boot-starter-jetty")
72+
```
73+
74+
说个题外话,从上面可以看出使用 Gradle 更加简洁明了,但是国内目前还是 Maven 使用的多一点,我个人觉得 Gradle 在很多方面都要好很多。
75+
76+
### 介绍一下@SpringBootApplication注解
77+
78+
```java
79+
package org.springframework.boot.autoconfigure;
80+
@Target(ElementType.TYPE)
81+
@Retention(RetentionPolicy.RUNTIME)
82+
@Documented
83+
@Inherited
84+
@SpringBootConfiguration
85+
@EnableAutoConfiguration
86+
@ComponentScan(excludeFilters = {
87+
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
88+
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
89+
public @interface SpringBootApplication {
90+
......
91+
}
92+
```
93+
94+
```java
95+
package org.springframework.boot;
96+
@Target(ElementType.TYPE)
97+
@Retention(RetentionPolicy.RUNTIME)
98+
@Documented
99+
@Configuration
100+
public @interface SpringBootConfiguration {
101+
102+
}
103+
```
104+
105+
可以看出大概可以把 `@SpringBootApplication `看作是 `@Configuration``@EnableAutoConfiguration``@ComponentScan ` 注解的集合。根据 SpringBoot官网,这三个注解的作用分别是:
106+
107+
- `@EnableAutoConfiguration`:启用 SpringBoot 的自动配置机制
108+
- `@ComponentScan`: 扫描被`@Component` (`@Service`,`@Controller`)注解的bean,注解默认会扫描该类所在的包下所有的类。
109+
- `@Configuration`:允许在上下文中注册额外的bean或导入其他配置类
30110

31-
Spring Boot Starters 是一系列

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /