From 91b2fe2efa4cb62a70f557b148adb22c7391b437 Mon Sep 17 00:00:00 2001 From: junhyung Date: Thu, 25 Jan 2024 00:09:04 +0900 Subject: [PATCH] [Hotfix] Fix @ConditionalOnMissingBean not working --- gradle.properties | 2 +- spring-bukkit-command/build.gradle.kts | 1 + .../command/CommandConfiguration.kt | 29 +++++++++++++++++++ .../command/CommandInitializer.kt | 16 +++++----- .../command/GenericCommandExecutor.kt | 7 ----- .../listener/BukkitListenerConfiguration.kt | 7 +++++ .../listener/SimpleEventExecutorFactory.kt | 8 ----- 7 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandConfiguration.kt diff --git a/gradle.properties b/gradle.properties index 12fbd04..c204d67 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ kotlin.code.style=official project.group=kr.summitsystems -project.version=0.0.8-beta2 +project.version=0.0.8-beta3 project.name=spring-bukkit project.url=https://github.com/summit-systems/spring-bukkit project.url.scm=https://github.com/summit-systems/spring-bukkit.git diff --git a/spring-bukkit-command/build.gradle.kts b/spring-bukkit-command/build.gradle.kts index 12a8998..2851203 100755 --- a/spring-bukkit-command/build.gradle.kts +++ b/spring-bukkit-command/build.gradle.kts @@ -4,6 +4,7 @@ plugins { dependencies { compileOnly(libs.spigot) + api(libs.jakarta.annotation) api(projects.core) diff --git a/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandConfiguration.kt b/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandConfiguration.kt new file mode 100644 index 0000000..d884694 --- /dev/null +++ b/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandConfiguration.kt @@ -0,0 +1,29 @@ +package kr.summitsystems.springbukkit.command + +import org.springframework.beans.factory.config.BeanDefinition +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Role +import org.springframework.core.convert.ConversionService + +@Role(BeanDefinition.ROLE_INFRASTRUCTURE) +@Configuration +class CommandConfiguration { + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + @ConditionalOnMissingBean(CommandExecutor::class) + @Bean + fun defaultCommandExecutor( + commandMappingRegistry: CommandMappingRegistry, + commandFeedbackSource: CommandFeedbackSource, + commandContextHolder: CommandContextHolder, + commandArgumentConversionService: ConversionService + ): CommandExecutor { + return GenericCommandExecutor( + commandMappingRegistry, + commandFeedbackSource, + commandContextHolder, + commandArgumentConversionService + ) + } +} \ No newline at end of file diff --git a/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandInitializer.kt b/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandInitializer.kt index fcc7c8b..bdee3ee 100644 --- a/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandInitializer.kt +++ b/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/CommandInitializer.kt @@ -1,21 +1,21 @@ package kr.summitsystems.springbukkit.command +import jakarta.annotation.PostConstruct import org.bukkit.command.Command import org.bukkit.command.CommandSender import org.bukkit.plugin.Plugin import org.bukkit.plugin.PluginDescriptionFile import org.bukkit.plugin.java.JavaPlugin -import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @Component -class CommandInitializer { - @Autowired - fun registerCommand( - plugin: Plugin, - pluginDescriptionFile: PluginDescriptionFile, - commandExecutor: CommandExecutor - ) { +class CommandInitializer( + private val plugin: Plugin, + private val pluginDescriptionFile: PluginDescriptionFile, + private val commandExecutor: CommandExecutor +) { + @PostConstruct + fun registerCommand() { if (plugin is JavaPlugin) { pluginDescriptionFile.commands.keys.forEach { name -> val command = plugin.getCommand(name) ?: return@forEach diff --git a/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/GenericCommandExecutor.kt b/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/GenericCommandExecutor.kt index 687e50c..39d8b43 100644 --- a/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/GenericCommandExecutor.kt +++ b/spring-bukkit-command/src/main/kotlin/kr/summitsystems/springbukkit/command/GenericCommandExecutor.kt @@ -4,22 +4,15 @@ import kr.summitsystems.springbukkit.command.annotation.CommandAuthorize import kr.summitsystems.springbukkit.core.util.extension.getLocale import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.springframework.beans.factory.config.BeanDefinition -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean -import org.springframework.context.annotation.Role import org.springframework.core.annotation.AnnotationUtils import org.springframework.core.convert.ConversionService import org.springframework.core.convert.ConverterNotFoundException import org.springframework.core.convert.TypeDescriptor -import org.springframework.stereotype.Component import java.lang.reflect.InvocationTargetException import java.lang.reflect.Method import kotlin.reflect.jvm.jvmErasure import kotlin.reflect.jvm.kotlinFunction -@ConditionalOnMissingBean(GenericCommandExecutor::class) -@Component -@Role(BeanDefinition.ROLE_INFRASTRUCTURE) open class GenericCommandExecutor( protected val commandMappingRegistry: CommandMappingRegistry, protected val commandFeedbackSource: CommandFeedbackSource, diff --git a/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/BukkitListenerConfiguration.kt b/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/BukkitListenerConfiguration.kt index bfdc201..71a5f62 100644 --- a/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/BukkitListenerConfiguration.kt +++ b/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/BukkitListenerConfiguration.kt @@ -29,4 +29,11 @@ class BukkitListenerConfiguration { ): BukkitListenerAnnotationBeanPostProcessor { return BukkitListenerAnnotationBeanPostProcessor(applicationContext) } + + @ConditionalOnMissingBean(EventExecutorFactory::class) + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + @Bean + fun defaultEventExecutorFactory(): EventExecutorFactory { + return SimpleEventExecutorFactory() + } } \ No newline at end of file diff --git a/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/SimpleEventExecutorFactory.kt b/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/SimpleEventExecutorFactory.kt index 9c8c8de..9ee9553 100644 --- a/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/SimpleEventExecutorFactory.kt +++ b/spring-bukkit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/listener/SimpleEventExecutorFactory.kt @@ -1,16 +1,8 @@ package kr.summitsystems.springbukkit.core.listener import org.bukkit.plugin.EventExecutor -import org.springframework.beans.factory.config.BeanDefinition -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Role -import org.springframework.stereotype.Component import java.lang.reflect.Method -@ConditionalOnMissingBean(EventExecutorFactory::class) -@Role(BeanDefinition.ROLE_INFRASTRUCTURE) -@Component class SimpleEventExecutorFactory : EventExecutorFactory { override fun create(eventClass: Class<*>, listenerInstance: Any, method: Method): EventExecutor { return SimpleEventExecutor(eventClass, listenerInstance, method)