Spring @ComponentScan 不扫描

Spring @ComponentScan does not scan

我已经设置了一个 Spring 项目(非引导)。我这样做是为了在我的世界(纸质)插件中使用它。我现在面临的问题描述如下:我有一个 BukkitSpringApplication class 和一个 Subserver class (这是 bukkit 的主要 class插入)。 Subserver class 包含一个 onEnable() 插件启动时调用的方法。那时我想启动我的 Spring 应用程序更好地说:我想扫描我的包中的组件。我想我做对了,但我在控制台中得到的唯一结果是有 5 个 bean,其中 4 个是 spring 相关 bean,最后一个是 Application bean (BukkitSpringApplication ).

控制台:

[15:35:36] [Server thread/INFO]: [Subserver-bukkit] Enabling Subserver-bukkit v1.0*
[15:35:36] [Server thread/INFO]: 5
[15:35:36] [Server thread/INFO]: [Subserver-bukkit] org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[15:35:36] [Server thread/INFO]: [Subserver-bukkit] org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[15:35:36] [Server thread/INFO]: [Subserver-bukkit] org.springframework.context.event.internalEventListenerProcessor
[15:35:36] [Server thread/INFO]: [Subserver-bukkit] org.springframework.context.event.internalEventListenerFactory
[15:35:36] [Server thread/INFO]: [Subserver-bukkit] bukkitSpringApplication
[15:35:36] [Server thread/ERROR]: Error occurred while enabling Subserver-bukkit v1.0 (Is it up to date?)
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'de.nebelniek.BukkitConfiguration' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1172) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at de.nebelniek.Subserver.onEnable(Subserver.java:22) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:561) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:475) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.MinecraftServer.loadWorld(MinecraftServer.java:733) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:353) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1230) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.MinecraftServer.lambda$spin[=11=](MinecraftServer.java:322) ~[patched_1.17.1.jar:git-Purpur-1418]
    at java.lang.Thread.run(Thread.java:831) ~[?:?]

我的Subserverclass:

public class Subserver extends JavaPlugin {

    private AnnotationConfigApplicationContext context;

    @SneakyThrows
    @Override
    public void onEnable() {
        context = new AnnotationConfigApplicationContext(BukkitSpringApplication.class);
        System.out.println(context.getBeanDefinitionCount());
        for (String beanName : context.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }
        BukkitConfiguration bukkitConfiguration = context.getBean(BukkitConfiguration.class);
        bukkitConfiguration.startMinecraftPlugin(context, this);
        context.registerShutdownHook();
    }

    @Override
    public void onDisable() {
        context.close();
        context = null;
    }
}
@Configuration
@ComponentScan(basePackages = "de.nebelniek")
public class BukkitSpringApplication {

}

我的 BukkitConfiguration :

package de.nebelniek;

@Getter
@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BukkitConfiguration {

    private final ApplicationEventPublisher eventPublisher;

    private final HomeController homeController;
    private final VerifyController verifyController;

    private CredentialManager credentialManager;

    public void startMinecraftPlugin(ApplicationContext context, JavaPlugin plugin) {
        this.eventPublisher.publishEvent(new BukkitPluginEnableEvent(context, plugin));
        this.homeController.setupRoutes();
        this.verifyController.setupRoutes();
    }

    @Setter
    private PaperCommandManager commandManager;

}

所以有人知道我做错了什么吗? 谢谢你的帮助! <3

我已经设法解决了这个问题。这里的问题是,spring 使用默认值 ClassLoader 但是,由于我使用的是 bukkit,classes 由 PluginClassLoader.

加载

那么我如何获得 PluginClassLoader

在扩展 JavaPlugin 的 class 中,您可以轻松调用 getClassLoader() 来获取 PluginClassLoader.

现在如何将它设置为 spring 正在使用的那个?

把这个扔进你的 onEnable() 就可以了。

        Thread.currentThread().setContextClassLoader(getClassLoader());

无论如何,谢谢你的帮助。