java - Spring应用程序上下文有我的bean,但@autowired看不到它

标签 java spring autowired

我正在尝试使用注释配置我的 Spring Boot 应用程序并在其中使用 @Autowired 注释。当我检查是否加载了 Bean 时,它已加载,但使用 @Autowired 时,它显示 NoSuchBeanDefinitionException

正如您进一步看到的,我尝试检查我的 Bean 是否确实已加载,因此当我运行应用程序时,我可以在控制台中看到我的 Bean 的名称。 另外,我尝试将 'scanBasePackages = "com.log.iei.Logistica"' 添加到我的 @SpringBootApplication 注释中,但它没有改变任何内容。 另外,我尝试了现场 Autowiring

这是我的主要类(class):

@SpringBootApplication(scanBasePackages = "com.log.iei.Logistica")
public class LogisticaApplication extends Application {
   public static ConfigurableApplicationContext context;

   @Override
   public void init() throws Exception {
       SpringApplicationBuilder builder = new SpringApplicationBuilder(AppConfig.class);
       context = builder.run(getParameters().getRaw().toArray(new String[0]));
       String[] beanNames = context.getBeanDefinitionNames();
       Arrays.sort(beanNames);
       for (String beanName : beanNames) {
           System.out.println(beanName);
       }

   }

这是VehicleService类的一部分:

@Component("vehicleService")
public class VehicleService implements IDao<VehicleEntity> {

    private static VehicleService vehicleService;
    GenericDao<VehicleEntity> dao;

    public VehicleService(){
        dao = new GenericDao<>(VehicleEntity.class);
        System.out.println("==== VehicleService was created ====");
    }

这是@Autowired 部分的一部分:

@Component("cargoPage")
public class CargoPage extends TablePageTemplate {

    @Autowired
    public CargoPage(VehicleService vehicleService){
        getAboveTableLine().getChildren().addAll(getAboveTableLineSetup());

        setTable(getTable(), vehicleService.findAll(), VehicleEntity.getTableMapping());

这里有一个错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.log.iei.Logistica.data.controllers.Services.VehicleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    ... 24 more

UPD:问题可能出在 VehicleService 实现通用接口(interface)上。

最佳答案

首先,您必须将基础包设置为:

@SpringBootApplication(scanBasePackages = "com.log")

您正确映射了所有内容,但是,用于 @Autowire beans 的构造函数不应调用任何其他逻辑。如果您需要在 bean 初始化后立即执行某些操作,请使用 @PostConstruct

这就是您的代码的样子:

@Service
public class CargoPage extends TablePageTemplate {

    private VehicleService vehicleService;

    @Autowired
    public CargoPage(VehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }

    @PostConstruct
    public void init() {
        getAboveTableLine().getChildren().addAll(getAboveTableLineSetup());
        setTable(getTable(), vehicleService.findAll(), VehicleEntity.getTableMapping());
    }
}

关于java - Spring应用程序上下文有我的bean,但@autowired看不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56709614/

相关文章:

java - 在后台运行没有丑陋命令提示框的java文件

java - 如何在 Spring HandlerInterceptorAdapter 中将 header 添加到 http 响应?

java - 使用 JdbcTemplate.update() 执行存储过程是一个好习惯吗?

java - 如何通过@Autowired 或 property setter 获取相关的 Spring beans

java - Android:与屏幕交互锁定按钮

java - 如何使用 Java 小程序安全地与数据库通信

java - java中的任何trie实现(使用maven repo)

java - Spring中未加载Bean方法 'dataSource'

java - Autowiring 的 Spring 集成测试很慢

java - 父类(super class)中带有 @Autowired 的 Spring 工厂 bean