有应用生命周期，便会有基于时机点的扩展。有时机点便有事件，有事件便要有事件总线。应用在各个时机点上，通过事件总线进行事件分发，由订阅者进行扩展。

Solon 内核，自带了一个基于强类型的本地事件总线：

* 强类型事件
* 基于发布/订阅模型
* 同步分发，可传导异常，进而支持事务回滚

目前事件总线的主要使用场景：

### 1、应用生命周期的分发

这也是事件总线最初的使用场景。说是分发，其实使用时主要是订阅：

```java
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventBus;

EventBus.subscribe(AppLoadEndEvent.class, event->{
   log.info("应用启动完成了");
});
```


### 2、用户层面的应用（自定义事件）

//如果需要主题的本地总线，可以考虑：[DamiBus](https://gitee.com/noear/damibus)

#### a）定义强类型的事件模型（约束性强）

```java
@Getter
public class HelloEvent {
    private String name;
    public HelloEvent(String name){
        this.name = name;
    }
}
```

#### b）订阅或监听事件

```java
import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventBus;

//注解模式
@Component
public class HelloEventListener implements EventListener<HelloEvent>{
    @Override
    public void onEvent(HelloEvent event) throws Throwable {
        System.out.println(event.getName());
    }
}

//手动模式
EventBus.subscribe(HelloEvent.class, event->{
    System.out.println(event.getName());
});
```


#### c）发布事件

```java
import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.EventBus;

@Component
public class DemoService {
    public void hello(String name){
        //同步发布事件
        EventBus.publish(new HelloEvent(name));
        
        //异步发布事件（一般不推荐）//不能传导异常（不能做事务传播）
        //EventBus.publishAsync(new HelloEvent(name));
    }
}
```