Skip to content

Commit

Permalink
Merge pull request #448 from marszhangzhixing/patch-32
Browse files Browse the repository at this point in the history
Update spring-dependency-injection.md
  • Loading branch information
fuzhengwei authored Jun 30, 2024
2 parents 3f19e4c + 9e4c51a commit 5bee238
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions docs/md/road-map/spring-dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lock: need

**你可以不用,但不能不会!**

在小傅哥带着大家做的实战项目中「 [https://gaga.plus](https://gaga.plus) 」,有着大量的高级编程技巧和设计模式结合运用的手段,帮你打开能”拿捏“写代码的强悍能力。这些能力都是越早积累越好,否则你以为,为啥一边人有喊着寒冬,一遍有人拿着高薪
在小傅哥带着大家做的实战项目中「 [https://gaga.plus](https://gaga.plus) 」,有着大量的高级编程技巧和设计模式结合运用的手段,帮你打开能”拿捏“写代码的强悍能力。这些能力都是越早积累越好,否则你以为,为啥一边有人喊着寒冬,一边有人拿着高薪

>文末提供了整合案例源码,以及5个业务应用级项目,5个技术组件项目。🛫
Expand All @@ -36,18 +36,18 @@ lock: need
<img src="https://bugstack.cn/images/roadmap/tutorial/road-map-spring-dependency-injection-01.png" width="400px">
</div>

- config工程所需的配置类对象的使用案例。
- domain一个接口多个实现类、实现类优先级创建、原型对象、Spring 加载的对象。
- infrastructure仓储注解实例化对象的使用。
- triggerlist注入、map注入、依赖注入,对象初始和销毁的监听。
- ApiTest测试各类注入案例的验证。
- config工程所需的配置类对象的使用案例。
- domain一个接口多个实现类、实现类优先级创建、原型对象、Spring 加载的对象。
- infrastructure仓储注解实例化对象的使用。
- triggerlist注入、map注入、依赖注入,对象初始和销毁的监听。
- ApiTest测试各类注入案例的验证。

## 二、实践案例

**实例化注解**

- `@Component`:组件注解
- `@Serivice`:服务注解
- `@Service`:服务注解
- `@Repository`:仓储注解,提供对持久化类数据的操作的服务。
- `@Controller/@RestController()`:对外提供服务的注解。

Expand Down Expand Up @@ -80,7 +80,7 @@ public Response<String> distributeAward(@RequestParam String userId, @RequestPar
}
```

- **场景**:IAwardService 接口有多个实现类,可以通过 @Resource@Autowired 注解注入,也可以通过构造函数注入。在 Spring 官网文档中,是推荐使用构造函数注入的`The Spring team generally advocates constructor injection, as it lets you implement application components as immutable objects and ensures that required dependencies are not null.` [https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html](https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html)
- **场景**:IAwardService 接口有多个实现类,可以通过 @Resource@Autowired 注解注入,也可以通过构造函数注入。在 Spring 官网文档中,是推荐使用构造函数注入的`The Spring team generally advocates constructor injection, as it lets you implement application components as immutable objects and ensures that required dependencies are not null.` [https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html](https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html)
- **用途**:Map 注入是一个非常好的注入手段,我们可以把每个 IAwardService 实现类设定好 Bean 的名称为数据库中的奖品 awardKey。在发奖的时候,可以直接根据 awardKey 从 Map 中获取到对应的 Bean 对象,这样也就省去了 `if···else` 大量的判断操作。

### 2. 空注入判断
Expand Down Expand Up @@ -160,7 +160,7 @@ public String redisson02() {
@ConfigurationProperties(prefix = "sdk.config", ignoreInvalidFields = true)
public class AutoConfigProperties {

/** 状态open = 开启、close 关闭 */
/** 状态open = 开启、close 关闭 */
private boolean enable;
/** 转发地址 */
private String apiHost;
Expand All @@ -184,7 +184,7 @@ sdk:
```

- **场景**:模拟创建 createTopic,入参的对象为注入的操作,@Qualifier 注解可以指定要注入哪个名字的对象。之后 `@ConditionalOnProperty` 注解可以通过配置的 enabled 值,来确定是否实例化对象。
- **用途**这个场景场景是非常使用的,比如你做了一个组件,或者业务中要增加一些配置。启动或关闭某些服务,就可以使用了。而不需要把 pom 中引入的组件注释掉。
- **用途**这个场景是非常使用的,比如你做了一个组件,或者业务中要增加一些配置。启动或关闭某些服务,就可以使用了。而不需要把 pom 中引入的组件注释掉。

### 6. 自定义Condition,判断是否实例化对象

Expand Down Expand Up @@ -442,4 +442,4 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
4. **实例化 Bean**
`ConstructorResolver` 使用找到的依赖,调用 `AwardController` 的构造函数,创建 `AwardController` 实例。
5. **注入依赖**
`DefaultListableBeanFactory` 将创建好的 `Map<String, IAwardService>` 注入到 `AwardController` 的构造函数中。
`DefaultListableBeanFactory` 将创建好的 `Map<String, IAwardService>` 注入到 `AwardController` 的构造函数中。

0 comments on commit 5bee238

Please sign in to comment.