generated from just-the-docs/just-the-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
216 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
--- | ||
layout: default | ||
title: SpringContext | ||
parent: Spring | ||
--- | ||
|
||
|
||
# 1. ConfigurableApplicationContext的结构 | ||
|
||
```mermaid | ||
classDiagram | ||
direction BT | ||
class ApplicationContext { | ||
<<Interface>> | ||
} | ||
class ApplicationEventPublisher { | ||
<<Interface>> | ||
} | ||
class AutoCloseable { | ||
<<Interface>> | ||
} | ||
class BeanFactory { | ||
<<Interface>> | ||
} | ||
class Closeable { | ||
<<Interface>> | ||
} | ||
class ConfigurableApplicationContext { | ||
<<Interface>> | ||
} | ||
class EnvironmentCapable { | ||
<<Interface>> | ||
} | ||
class FunctionalInterface | ||
class HierarchicalBeanFactory { | ||
<<Interface>> | ||
} | ||
class Lifecycle { | ||
<<Interface>> | ||
} | ||
class ListableBeanFactory { | ||
<<Interface>> | ||
} | ||
class MessageSource { | ||
<<Interface>> | ||
} | ||
class ResourceLoader { | ||
<<Interface>> | ||
} | ||
class ResourcePatternResolver { | ||
<<Interface>> | ||
} | ||
ApplicationContext --> ApplicationEventPublisher | ||
ApplicationContext --> EnvironmentCapable | ||
ApplicationContext --> HierarchicalBeanFactory | ||
ApplicationContext --> ListableBeanFactory | ||
ApplicationContext --> MessageSource | ||
ApplicationContext --> ResourcePatternResolver | ||
FunctionalInterface .. ApplicationEventPublisher | ||
Closeable --> AutoCloseable | ||
ConfigurableApplicationContext --> ApplicationContext | ||
ConfigurableApplicationContext --> Closeable | ||
ConfigurableApplicationContext --> Lifecycle | ||
HierarchicalBeanFactory --> BeanFactory | ||
ListableBeanFactory --> BeanFactory | ||
ResourcePatternResolver --> ResourceLoader | ||
``` | ||
|
||
BeanFactory本身功能并不充分,通过一系列的后置处理器来完善,ApplicationContext就实现一系统的后置处理器来完成功能增强。 | ||
具体增强哪些功能,在每个Bean定义时,去指定具体的接口,来完成具体的功能。 | ||
|
||
# 2. Spring 上下文初始化过程 | ||
|
||
|
||
# 3. Spring常见的一些后置处理器 | ||
|
||
|
||
# 4. Spring 上下文类型 | ||
|
||
|
||
|
||
|
||
|
||
# 5. Aware接口 | ||
|
||
# 6. Spring事件 | ||
|
||
## 6.1. 模型 | ||
|
||
- 事件 org.springframework.context.ApplicationEvent | ||
- 事件的监听者 org.springframework.context.ApplicationListener | ||
- 事件的发布者 org.springframework.context.ApplicationEventPublisher | ||
- 事件的广播者 org.springframework.context.event.SimpleApplicationEventMulticaster | ||
|
||
如下图所示:发布一个事件后,会调用multicastEvent方法,在multicastEvent方法可能线程池异步的调用invokeListener方法。 | ||
在invokeListener方法中调用doInvokeListener,真正执行具体的ApplicationListener类中onApplicationEvent方法。 | ||
|
||
```mermaid | ||
--- | ||
title: Spring Event | ||
--- | ||
classDiagram | ||
class ApplicationListener { | ||
+ void onApplicationEvent(E event) | ||
} | ||
namespace Publisher { | ||
class ApplicationEventPublisher { | ||
<<Interface>> | ||
+ void publishEvent(ApplicationEvent event) | ||
} | ||
class AbstractApplicationContext { | ||
+ void publishEvent(ApplicationEvent event) | ||
} | ||
} | ||
namespace Multicaster { | ||
class SimpleApplicationEventMulticaster { | ||
void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) | ||
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) | ||
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) | ||
} | ||
class ApplicationEventMulticaster { | ||
<<Interface>> | ||
void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) | ||
} | ||
} | ||
AbstractApplicationContext --|> ApplicationEventPublisher | ||
SimpleApplicationEventMulticaster --|> ApplicationEventMulticaster | ||
SimpleApplicationEventMulticaster ..> ApplicationListener | ||
ApplicationEventPublisher ..> SimpleApplicationEventMulticaster | ||
``` | ||
|
||
## 6.2. 异步发布事件 | ||
|
||
Spring 发布事件是使用这个类来发布事件,可以指定线程池来异步发布事件 | ||
org.springframework.context.event.SimpleApplicationEventMulticaster | ||
```sqlite-psql | ||
@Override | ||
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) { | ||
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event)); | ||
Executor executor = getTaskExecutor(); | ||
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) { | ||
//此处默认是空的 | ||
if (executor != null) { | ||
executor.execute(() -> invokeListener(listener, event)); | ||
} | ||
else { | ||
invokeListener(listener, event); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
@Configuration | ||
public class Config { | ||
|
||
@Bean | ||
public SimpleApplicationEventMulticaster applicationEventMulticater() { | ||
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster(); | ||
multicaster.setTaskExecutor(Executors.newFixedThreadPool(1)); // 自定义线程池 | ||
return multicaster; | ||
} | ||
} | ||
|
||
``` | ||
|
||
# 7. spring中的资源 | ||
```sqlite-psql | ||
// 从类路径下 | ||
Resource [] list = context.getResource("classpath:"); | ||
Resource [] list = context.getResource("classpath:*"); | ||
Resource [] list = context.getResource("classpath:*/META-INF"); | ||
// 从文件路径下 | ||
Resource [] listcontext.getResource("file:"); | ||
``` |
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,25 @@ | ||
--- | ||
layout: default | ||
title: 设计方法 | ||
parent: Spring | ||
--- | ||
|
||
|
||
# 编程方法论 | ||
- 不能确定的就定义为一个接口 | ||
- 面向接口编码 | ||
- DO / PO / DTO / BO / VO / UID | ||
|
||
## 命令参考 | ||
### Service / DAO 层方法命名规约: | ||
* 1)获取单个对象的方法用 get 做前缀。 | ||
* 2)获取多个对象的方法用 list 做前缀,复数结尾,如:listObjects | ||
* 3)获取统计值的方法用 count 做前缀。 | ||
* 4)插入的方法用 save / insert 做前缀。 | ||
* 5)删除的方法用 remove / delete 做前缀。 | ||
* 6)修改的方法用 update 做前缀。 | ||
### 领域模型命名规约: | ||
* 1)数据对象:xxxDO,xxx 即为数据表名。 | ||
* 2)数据传输对象:xxxDTO,xxx 为业务领域相关的名称。 | ||
* 3)展示对象:xxxVO,xxx 一般为网页名称。 | ||
* 4)POJO 是 DO / DTO / BO / VO 的统称,禁止命名成 xxxPOJO。 |