Skip to content

Commit

Permalink
[Hotfix] Fix @ConditionalOnMissingBean not working
Browse files Browse the repository at this point in the history
  • Loading branch information
vjh0107 committed Jan 24, 2024
1 parent dd787ab commit 91b2fe2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions spring-bukkit-command/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
compileOnly(libs.spigot)
api(libs.jakarta.annotation)

api(projects.core)

Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ class BukkitListenerConfiguration {
): BukkitListenerAnnotationBeanPostProcessor {
return BukkitListenerAnnotationBeanPostProcessor(applicationContext)
}

@ConditionalOnMissingBean(EventExecutorFactory::class)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Bean
fun defaultEventExecutorFactory(): EventExecutorFactory {
return SimpleEventExecutorFactory()
}
}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 91b2fe2

Please sign in to comment.