Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions source/10-Component-Scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public class MyPersonComponent {
> 其实这里的 `MyPersonComponent` 和之前三个 "依赖注入(DI)" 的类是完全不一样的
> 对比一下,你会发现前三种DI都是有 sayHello()方法的,而这里的 `MyPersonComponent` 只是代替了 XML 的部分工作,本身并没有 sayHello方法。

- 这里的 `MyPersonComponent` 用处:生成`MyServiceImpl` 的Bean对象,代替了 XML 的绝大部分工作,该类本身是和`MyServiceImpl`无关的(没有继承)
- 而之前的三种 依赖注入(DI) 方式都是通过 XML 来生成 `MyServiceImpl` 的Bean对象,类运行时就是 Bean 对象

- 之前的三种 依赖注入(DI) 方式的实现都是: 由 Spring 容器通过 XML 来生成 `MyServiceImpl` 类的Bean对象(类本身就是 Spring 容器操作的对象)
- 而这里的 `MyPersonComponent` 类只是用来生成`MyServiceImpl` 的Bean对象,它是代替了 XML 的绝大部分工作(这个类本身和`MyServiceImpl`是没有继承关系,所以也没有 sayHello方法)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要强调没有继承关系呢。。其实是没有任何关系啊,都不是一层层面的东西


**本节代码示例**
```Java
Main函数中的例子(命名稍微改动,请变通):
写在Main函数里(命名稍微改动,请变通):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个例子是想表达什么东西呢,在前面有一个类似的代码

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果你把前面的类都改了名字,就能发现这段示例挺重要的

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前面是说这一节的前面吗。还是 IoC 那一节

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指的是:前面的 DI

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我没太明白这个示例是想干啥。它重要在哪儿,

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段跟 https://skyline75489.github.io/Heart-First-JavaWeb/7-First-Spring-Container.html 这里的最下面那段代码,不一回事吗

ApplicationContext ComponentContext = new ClassPathXmlApplicationContext("Component-scan.xml");
MyServiceImpl service = ComponentContext.getBean("myService", MyServiceImpl.class);
System.out.println(service.sayHello());
Expand Down