Spring не сканирует классы, аннотированные @Component/@Service и т.д.JAVA

Программисты JAVA общаются здесь
Anonymous
Spring не сканирует классы, аннотированные @Component/@Service и т.д.

Сообщение Anonymous »

У меня есть приложение на Java 21, которое представляет собой плагин Minecraft Spigot. Я использую Spring Framework 6.1.10. Это мои зависимости:

Код: Выделить всё

        
org.springframework
spring-core
6.1.10


org.springframework
spring-context
6.1.10

Это основной класс моего плагина:

Код: Выделить всё

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");
}
}
И это CommandTest с аннотацией @Component

Код: Выделить всё

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 распечатывает все доступные компоненты, и это результат:

Код: Выделить всё

[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 в конфигурации, а класс CommandTest, аннотированный @Component, — нет.
Я не знаю, что мне с этим делать.

Подробнее здесь: https://stackoverflow.com/questions/786 ... ervice-etc

Вернуться в «JAVA»