-
Notifications
You must be signed in to change notification settings - Fork 140
Update 10-Component-Scan.md #11
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,13 +19,16 @@ | |
| </beans> | ||
| ``` | ||
|
|
||
| >注意我们去掉了所有的 Bean 定义,也同时去掉了 `<context:annotation-config>`,因为 `<context:component-scan>` 默认就会打开 `<context:annotation-config>`,因此不需要再手动设置 `<context:annotation-config>`。 | ||
| >注意我们去掉了所有的 Bean 定义,也同时去掉了 `<context:annotation-config>` | ||
| >因为 `<context:component-scan>` 默认就会打开 `<context:annotation-config>`,因此不需再手动设置 | ||
|
|
||
| ### Component | ||
|
|
||
| Spring 中定义了若干用于注册 Bean 的 Annotation,包括 `@Component` 以及继承自 `@Component` 的 `@Service` 和 `@Repository`。所谓的 component-scan 也就是告诉 Spring 去扫描标示了这些 Annotation 的类,将它们用于 Bean 的注册。 Spring 中推荐使用 `@Service` 和 `@Component` 标示逻辑和服务层的组件,使用 `@Repository` 标示持久层的组件。 | ||
| Spring 中定义了若干用于注册 Bean 的 Annotation,包括 `@Component` 以及继承自 `@Component` 的 `@Service` 和 `@Repository`。 | ||
| 所谓的 component-scan 也就是告诉 Spring 去扫描标示了这些 Annotation 的类,将它们用于 Bean 的注册。 | ||
| Spring 中推荐使用 `@Service` 和 `@Component` 标示逻辑和服务层的组件,使用 `@Repository` 标示持久层的组件。 | ||
|
|
||
| >`@Service` 和 `@Repository` 都继承自 `@Component`,因此在 Bean 注册的层面,它们没有本质上的区别,不过 `@Repository` 在持久层会有一个 exception 转换的作用,这里先略去不讲。 | ||
| >`@Service` 和 `@Repository` 都继承自 `@Component`,因此在 Bean 注册的层面,它们没有本质上的区别。不过 `@Repository` 在持久层会有一个 exception 转换的作用,这里先略去不讲。 | ||
|
|
||
| 创建一个新类 MyPersonComponent: | ||
|
|
||
|
|
@@ -55,8 +58,23 @@ public class MyPersonComponent { | |
| } | ||
| ``` | ||
|
|
||
| 可以看到,我们基本就是把原先在 XML 当中的配置,挪到了 Java 代码当中。运行代码,得到和之前一样的结果 | ||
| 可见,我们其实就是把原先在 XML 当中的配置,挪到了 Java 代码里。运行代码,得到的结果和之前一样。 | ||
|
|
||
| >注意到我们的 `MyServiceImpl` 仅仅设置了 greeting,为什么这样就能够正常工作呢?还记得上一节的 `@Autowired` 吗,它依然在发挥自己的作用,帮我们找到了 Person 的实例,并且赋给了 `MyServiceImpl`。 | ||
| >注意到我们的 `MyServiceImpl` 仅仅设置了 greeting,为什么这样就能够正常工作呢?还记得上一节的 `@Autowired` 吗,它依然在发挥自己的作用,帮我们找到了 Person 的实例,并且赋给了 `MyServiceImpl`。 | ||
|
|
||
|
|
||
| <hr> | ||
| 小结一下: | ||
| > 其实这里的 `MyPersonComponent` 和之前三个 "依赖注入(DI)" 的类是完全不一样的 | ||
| > 对比一下,你会发现前三种DI都是有 sayHello()方法的,而这里的 `MyPersonComponent` 只是代替了 XML 的部分工作,本身并没有 sayHello方法。 | ||
|
|
||
| - 之前的三种 依赖注入(DI) 方式的实现都是: 由 Spring 容器通过 XML 来生成 `MyServiceImpl` 类的Bean对象(类本身就是 Spring 容器操作的对象) | ||
| - 而这里的 `MyPersonComponent` 类只是用来生成`MyServiceImpl` 的Bean对象,它是代替了 XML 的绝大部分工作(这个类本身和`MyServiceImpl`是没有继承关系,所以也没有 sayHello方法) | ||
|
|
||
| **本节代码示例** | ||
| ```Java | ||
| 写在Main函数里(命名稍微改动,请变通): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个例子是想表达什么东西呢,在前面有一个类似的代码
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果你把前面的类都改了名字,就能发现这段示例挺重要的
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 前面是说这一节的前面吗。还是 IoC 那一节
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 指的是:前面的 DI
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我没太明白这个示例是想干啥。它重要在哪儿,
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么要强调没有继承关系呢。。其实是没有任何关系啊,都不是一层层面的东西