spring加载冲突问题
public interface INpcFunctionExecutor { /** * 执行功能 */ void execute(UserSocket socket, TalkNpcFuncData data); /** * 获取支持的功能类型 */ TalkNpcFunc getSupportedFunc(); } @Slf4j @Component public class TaskExecutor implements INpcFunctionExecutor { @Override public void execute(UserSocket socket, TalkNpcFuncData data) { int taskId = Integer.parseInt(data.getFuncParam()); EventDispatcher.getInstance().fireEvent( new TalkChoseEvent(EventType.TALK_CHOSE, socket.getRole(), data.getNpcId(), taskId) ); log.info("触发任务功能: roleId={}, npcId={}, taskId={}", socket.getRole().getId(), data.getNpcId(), taskId); } @Override public TalkNpcFunc getSupportedFunc() { return TalkNpcFunc.Task; } } @Slf4j @Component public class NpcFunctionExecutorDispatcher { private final Map<TalkNpcFunc, INpcFunctionExecutor> executors = new EnumMap<>(TalkNpcFunc.class); @Autowired(required = false) private List<INpcFunctionExecutor> executorList; @PostConstruct public void init() { // 这儿初始时没能加载TaskExecutor if (executorList != null) { for (INpcFunctionExecutor executor : executorList) { executors.put(executor.getSupportedFunc(), executor); log.info("注册NPC功能执行器: {} -> {}", executor.getSupportedFunc(), executor.getClass().getSimpleName()); } } } /** * 执行功能 * @return 是否找到并执行了执行器 */ public boolean dispatch(UserSocket socket, TalkNpcFuncData data) { int funcId = data.funcId; TalkNpcFunc func = TalkNpcFunc.fromId(funcId); INpcFunctionExecutor executor = executors.get(func); if (executor != null) { try { executor.execute(socket, data); log.debug("执行NPC功能: func={}, npcId={}", func, data.getNpcId()); return true; } catch (Exception e) { log.error("执行NPC功能失败: func={}, npcId={}", func, data.getNpcId(), e); return false; } } log.warn("未找到对应的功能执行器: funcId={}, func={}", funcId, func); return false; } }
在spring boot下面 些处没能自动加载TaskExecutor
原因:allow-bean-definition-overriding: true 导致 Spring Boot 的 taskExecutor bean 覆盖了 NPC 的 TaskExecutor。
原因:application.yml 里开了 spring.main.allow-bean-definition-overriding: true,而 @Component 的 TaskExecutor 默认 bean 名是 taskExecutor,与 Spring Boot 自带的异步 taskExecutor 重名,被后者静默覆盖,所以不会出现在 List<INpcFunctionExecutor> 里。
浙公网安备 33010602011771号