-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
> Spring 是什么? | ||
Spring 是一个功能强大的企业级开发框架,提供了一系列的模板,用来支持不同的应用需求,如:依赖注入(DI)、面向切面编程(AOP)、事务管理、Web 应用程序开发等。然后 Spring Boot 框架的出现,主要起到了简化 Spring 应用程序的开发,有利于快速构建开发应用程序。 | ||
|
||
> Spring Boot 提供了什么功能? | ||
1. 自动装配 | ||
|
||
通过依赖一个 spring-boot-starter-xxx 的依赖,然后通过配置文件来简化配置,简化业务逻辑的开发。 | ||
|
||
2. 内嵌 Web 服务器 | ||
|
||
Spring Boot 内置了`Tomcat`和`Jetty`Web 服务器,所以无需另外下载`Web`服务器便可以运行程序。 | ||
|
||
那么是如何启动`Web`项目的呢? | ||
|
||
如图: | ||
|
||
![启动类方法](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202312131550304.png) | ||
|
||
我们可以看到,在`main`方法中有一个`SpringApplication`类的静态方法`run()`来启动`Web`项目,然后`Spring Boot`会扫描我们的全局依赖,然后结合配置文件中的配置来启动程序。 | ||
|
||
3. 约定大于配置思想 | ||
|
||
简单来说就是配置与业务分离,而且并不需要开发者关心配置如何实现的,只需在配置文件中编写相关配置即可。例如:应用程序通过读取`application.yml`或者`application.properties`文件获取配置,极大程度上,让开发者更加专注于应用程序的开发。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
> 怎么理解 Spring 中的控制反转?控制怎么反转了? | ||
控制反转即`IOC`-- `Inversion Of Control`,顾名思义,就是`Bean`的控制权交给了 Spring 来进行管理,也就是对象的控制权。 | ||
|
||
例如: | ||
|
||
```Java | ||
class 张三 { | ||
} | ||
class 李四 { | ||
// 需要手动 new 出来 | ||
张三 zs = new 张三(); | ||
} | ||
``` | ||
|
||
```Java | ||
// 通过注解,把 Bean 注入 Spring 容器 | ||
@Component | ||
class 张三 { | ||
} | ||
class 李四 { | ||
// 因为张三的 Bean 已注入容器中,所以可以直接取出使用 | ||
@Autowire | ||
private 张三 zs; | ||
} | ||
``` | ||
|
||
因此,控制反转就是:本来是代码中 new 出一个对象,代码拥有控制权,然后在 Spring 中,通过`@Component`注解,把类表示为 Spring 的一个组件,被标记的类就会被 Spring 自动扫描,然后作为 Bean 注册到 Spring 的 IOC 容器中。然后控制权就交给了 Spring ,随用随取。 | ||
|
||
|
||
|
||
> IOC 还有什么优点? | ||
|
||
|
||
|
||
|
||
> IOC 是如何实现的? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
1. `@Component`:通用的注解,用于标识一个类为 Spring 组件。被 `@Component` 注解标记的类将被自动扫描并注册为 Spring 的 bean。 | ||
2. `@Controller`:用于标识一个类为 MVC 控制器的组件。通常用于处理用户请求和返回视图。 | ||
3. `@Service`:用于标识一个类为业务逻辑层的组件。通常用于封装业务逻辑,并被其他组件调用。 | ||
4. `@Repository`:用于标识一个类为数据访问层的组件。通常用于封装数据库操作,与数据库进行交互。 | ||
5. `@Autowired`:用于自动装配依赖关系。通过 `@Autowired` 注解,Spring 将自动在应用程序上下文中查找匹配的 bean,并将其注入到标记了 `@Autowired` 的字段、构造函数或方法参数中。 | ||
6. `@Qualifier`:用于指定具体的 bean,当存在多个匹配的候选 bean 时,可通过 `@Qualifier` 注解指定要注入的 bean。 | ||
7. `@Value`:用于注入配置属性值。通过 `@Value` 注解,可以将配置文件中的值注入到标记了 `@Value` 的字段或方法参数中。 | ||
8. `@RequestMapping`:用于映射请求路径到处理方法。通过 `@RequestMapping` 注解,可以定义处理特定请求的方法,并指定请求的 URL、HTTP 方法、请求参数等。 |