服务器监听器
1. 实现服务器监听器
创建 MyServerListener
类,该类实现了 TioBootServerListener
接口。在服务器启动完成后,这个类将启动 HotSwapWatcher
来监听类文件的变化:
package com.litongjava.tio.web.hello.config;
import com.litongjava.hotswap.watcher.HotSwapWatcher;
import com.litongjava.hotswap.wrapper.tio.boot.TioBootArgument;
import com.litongjava.hotswap.wrapper.tio.boot.TioBootRestartServer;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.jfinal.aop.AopManager;
import com.litongjava.tio.boot.constatns.ConfigKeys;
import com.litongjava.tio.boot.context.Context;
import com.litongjava.tio.boot.server.TioBootServerListener;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyServerListener implements TioBootServerListener {
protected static volatile HotSwapWatcher hotSwapWatcher;
@Override
public void boforeStart(Class<?>[] primarySources, String[] args) {
}
@Override
public void afterStarted(Class<?>[] primarySources, String[] args, Context context) {
Enviorment enviorment = Aop.get(Enviorment.class);
String env = enviorment.get(ConfigKeys.appEnv);
if("dev".endsWith(env)) {
}
}
}
在 afterStarted
方法中,如果处于开发环境,则 执行部分自定义代码
2. 注册服务器监听器
最后,编写 TioBootServerListenerConfig
类来在启动前将 TioBootServerListener
添加到 Aop 容器中:
package com.litongjava.tio.web.hello.config;
import com.litongjava.jfinal.aop.annotation.Bean;
import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration;
import com.litongjava.tio.boot.server.TioBootServerListener;
@BeforeStartConfiguration
public class TioBootServerListenerConfig {
@ABean
public TioBootServerListener tioBootServerListener() {
return new MyServerListener();
}
}
这将确保 MyServerListener
能够正确注册并在应用启动时被调用。