Skip to content

Commit

Permalink
更新Application的部分说明
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jun 10, 2024
1 parent b45e022 commit 4ad6bc8
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Writerside/topics/basic-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,96 @@ var application = Applications.launchApplicationBlocking(Simple.INSTANCE, appCon
## Bot的注册

有关Bot注册的相关描述可前往 [Bot管理器](BotManager.md) 章节阅读。

## Spring中获取Application

如果你在使用 Spring Boot starter,那么你可以直接通过bean注入的方式得到 `Application` 实例。

<tabs group="Code">
<tab title="Kotlin" group-key="Kotlin">

```Kotlin
@Component
class MyComponent
@Autowired // 其实注解可以省略,这里只是为了让'注入'行为比较显眼儿
constructor(
private val application: Application // 构造注入 Application

) {
// ...
}
```

</tab>
<tab title="Java" group-key="Java">

```Java
@Component
public class MyComponent {
private final Application application;

@Autowired // 其实注解可以省略,这里只是为了让'注入'行为比较显眼儿
public MyComponent(Application application) { // 构造注入 Application
this.application = application;
}

// ...
}
```

</tab>
</tabs>

你也可以基于此间接地获取 `BotManager``Bot` 等其他信息。

<tabs group="Code">
<tab title="Kotlin" group-key="Kotlin">

```Kotlin
@Component
class MyComponent(
private val application: Application
) {
// 假设这里是个定时任务
// 或者其他什么跟事件没关系的地方
fun runTask() {
// 得到所有的BotManager并遍历
for (botManager in application.botManagers) {
// ...
// 得到每一个BotManager中的所有已注册且未被关闭的Bot并遍历
for (bot in botManager.all()) {
// ...
}
}
}
}
```

</tab>
<tab title="Java" group-key="Java">

```Java
@Component
public class MyComponent {
private final Application application;

public MyComponent(Application application) {
this.application = application;
}

public void runTask() {
// 得到所有的BotManager并遍历
for (var botManager : application.getBotManagers()) {
// ...
// 得到每一个BotManager中的所有已注册且未被关闭的Bot并遍历
for (var it = botManager.all().iterator(); it.hasNext(); ) {
var bot = it.next();
// ...
}
}
}
}
```

</tab>
</tabs>

0 comments on commit 4ad6bc8

Please sign in to comment.