我在 java 21 中有一个应用程序,它是一个 Minecraft spigot 插件。我使用 spring 框架 6.1.10。这些是我的依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.10</version>
</dependency>
这是我的插件主类:
package xyz.abc;
import org.mineacademy.fo.Common;
import org.mineacademy.fo.plugin.SimplePlugin;
import org.mineacademy.fo.remain.Remain;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import xyz.abc.commands.CommandTest;
public class SparkCore extends SimplePlugin {
@Override
protected void onPluginStart() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Common.log("Available beans:");
for (String beanName : applicationContext.getBeanDefinitionNames()) {
Common.log(beanName);
}
Remain.registerCommand(applicationContext.getBean(CommandTest.class));
}
}
这是我的 Spring 配置:
package xyz.abc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "xyz.abc")
public class SpringConfig {
@Bean
Test test() {
return new Test("Test");
}
}
这是用@Component注释的CommandTest
package xyz.abc.commands;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.mineacademy.fo.command.SimpleCommand;
import org.mineacademy.fo.menu.model.ItemCreator;
import org.mineacademy.fo.remain.CompMaterial;
import org.springframework.stereotype.Component;
import xyz.abc.Test;
@Component
public final class CommandTest extends SimpleCommand {
private static final ItemStack wing = ItemCreator.of(CompMaterial.DIAMOND)
.modelData(2).make();
public CommandTest(Test test) {
super("test|test2");
System.out.println(test.name());
}
@Override
protected void onCommand() {
System.out.println(12);
checkConsole();
Player player = getPlayer();
}
}
正如您所看到的 onPluginLoad 打印所有可用的 beans,这就是结果:
[22:16:42 INFO]: Available beans:
[22:16:42 INFO]: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[22:16:42 INFO]: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[22:16:42 INFO]: org.springframework.context.annotation.internalCommonAnnotationProcessor
[22:16:42 INFO]: org.springframework.context.annotation.internalPersistenceAnnotationProcessor
[22:16:42 INFO]: org.springframework.context.event.internalEventListenerProcessor
[22:16:42 INFO]: org.springframework.context.event.internalEventListenerFactory
[22:16:42 INFO]: springConfig
[22:16:42 INFO]: test
但是后来我得到一个错误:
[22:31:37 INFO]: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'xyz.abc.commands.CommandTest' available
如您所见,名为“test”的 bean 已在配置中通过 @Bean 注解初始化,但使用 @Component 注解的类 CommandTest 尚未初始化。
我不知道我该怎么办。
答案如下:Spring @ComponentScan 不扫描
总结一下,我刚刚在 onPluginStart 方法中添加了
Thread.currentThread().setContextClassLoader(getClassLoader());
。