Spring Boot:找不到类型的 Bean。考虑定义一个bean

问题描述 投票:0回答:1

我知道这个问题已经被问过多次,我觉得自己真的很愚蠢,但我无法让我的 Spring Boot REST 应用程序运行。我正在为我正在玩的游戏构建一个东西,并且我是第一次尝试 Spring Boot。这是我的应用程序/包结构:

com.findersgame.questtracker
|- FindersGameQuestTrackerApiApplication.java
|
|- controller
|  |- QuestController.java
|
|- model
|  |- Location.java
|  |- Provider.java
|  |- Quest.java
|
|- repository
|  |- LocationRepository.java
|  |- ProviderRepository.java
|  |- QuestRepository.java
|
|- service
   |- QuestService.java

据我了解,这是 Spring Boot 中划分控制器、模型、存储库、服务等的“正确”方式。

转到主文件,

FindersGameQuestTrackerApiApplication.java

package com.findersgame.questtracker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication(scanBasePackages={ "com.findersgame.questtracker.*" })
@EnableJpaRepositories("com.findersgame.questtracker.repository.*")
@ComponentScan(basePackages = { "com.findersgame.questtracker.*" })
public class FindersGameQuestTrackerApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(FindersGameQuestTrackerApiApplication.class, args);
    }
}

根据我通过谷歌搜索、搜索 SO 和 Reddit 发现的信息,我添加了所有正确的接口(

@SpringBootApplication
@EnableJpaRepositories
@ComponentScan
)。

接下来是我到目前为止创建的控制器、服务和存储库:

package com.findersgame.questtracker.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.findersgame.questtracker.service.QuestService;

@RestController
public class QuestController {
    private QuestService questService;

    public QuestController(QuestService questService) {
        super();
        this.questService = questService;
    }
    
    @GetMapping("fix-quests")
    public void fixQuests() {
        questService.fixQuests();
    }
}
package com.findersgame.questtracker.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.findersgame.questtracker.model.Location;
import com.findersgame.questtracker.model.Provider;
import com.findersgame.questtracker.model.Quest;
import com.findersgame.questtracker.repository.LocationRepository;
import com.findersgame.questtracker.repository.ProviderRepository;
import com.findersgame.questtracker.repository.QuestRepository;

@Service
public class QuestService {
    
    @Autowired
    private LocationRepository locationRepository;
    
    @Autowired
    private ProviderRepository providerRepository;
    
    @Autowired
    private QuestRepository questRepository;
    
    @Autowired
    public QuestServiceImpl(
            FlooRepository flooRepository,
            GiverRepository giverRepository,
            QuestRepository questRepository) {

        super();
        this.flooRepository = flooRepository;
        this.giverRepository = giverRepository;
        this.questRepository = questRepository;
    }

    public void fixQuests() {
        List<Quest> quests = questRepository.findAll();
        
        quests.forEach(quest -> {
            String locationName = quest.getLocationName();
            String providerName = quest.getProviderName();
            
            if (locationName != null) {
                Location location = locationRepository.findByName(locationName);
                
                if (location != null) {
                    quest.setLocationId(location.getId());
                }
            }
            
            if (providerName != null) {
                Provider provider = providerRepository.findByName(providerName);
                
                if (provider != null) {
                    quest.setLocationId(provider.getId());
                }
            }
            
            questRepository.save(quest);
        });
    }
}
package com.findersgame.questtracker.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.findersgame.questtracker.model.Location;

@Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
    @Query(value = "SELECT * FROM locations WHERE `name` = ?1", nativeQuery = true)
    Location findByName(String name);
}
package com.findersgame.questtracker.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.findersgame.questtracker.model.Provider;

@Repository
public interface ProviderRepository extends JpaRepository<Provider, Long> {
    @Query(value = "SELECT * FROM providers WHERE `name` = ?1", nativeQuery = true)
    Provider findByName(String name);
}
package com.findersgame.questtracker.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.findersgame.questtracker.model.Quest;

@Repository
public interface QuestRepository extends JpaRepository<Quest, Long> {
    //pass
}

我目前拥有的型号:

  • Quest
    id
    locationId
    locationName
    providerId
    providerName
    name
  • 到目前为止,
    Location
    Provider
    都只有
    id
    name
  • 以上所有都有 getter 和 setter。

我的问题是我收到以下消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.findersgame.questtracker.service.QuestService required a bean of type 'com.findersgame.questtracker.repository.LocationRepository' that could not be found.


Action:

Consider defining a bean of type 'com.findersgame.questtracker.repository.LocationRepository' in your configuration.

根据我在网上的搜索,我遇到了以下问题:

  • @EnableJpaRepositories
    添加到他的主类中,做到了。
  • @ComponentScan
    添加到主类中,就完成了。
  • @EnableJpaRepositories
    添加到主类中,就完成了。
  • @Service
    添加到服务中,并将
    @Repository
    添加到存储库中,但我从一开始就这样做了。
    • 也尝试过不使用
      @Repository
      ,因为不需要它(已经是
      JpaRepository
      的一部分)。
  • 在服务中的属性和构造函数之前尝试使用和不使用
    @Autowired
  • 我还尝试完全删除
    LocationRepository
    ProviderRepository
    ,以确保我编写的查询不会影响它,或者拥有多个服务不会影响它。
    • 唯一的区别是不是找不到
      QuestRepository
      而是。

到目前为止我尝试过的方法都不起作用。我确信我错过了一些简单的东西,但从我所有的搜索中我还没有找到它。请帮忙!

java spring spring-boot repository
1个回答
0
投票

您正确地陈述了所有结构,但更基本的实现是创建一个名为 QuestService 的接口,然后添加一个名为 QuestServiceImpl 的服务来实现 QuestService 。您必须指定注入类型并将其注入到构造函数中。**

@Service
public class QuestServiceImpl implements QuestService {

@Autowired
private LocationRepository locationRepository;

@Autowired
private ProviderRepository providerRepository;

@Autowired
private QuestRepository questRepository;

public QuestServiceImpl(
        LocationRepository locationRepository,
        ProviderRepository providerRepository,
        QuestRepository questRepository) {
    this.locationRepository = locationRepository;
    this.providerRepository = providerRepository;
    this.questRepository = questRepository;
}

public void fixQuests() {
    List<Quest> quests = questRepository.findAll();

    quests.forEach(quest -> {
        String locationName = quest.getLocationName();
        String providerName = quest.getProviderName();

        if (locationName != null) {
            Location location = locationRepository.findByName(locationName);

            if (location != null) {
                quest.setLocationId(location.getId());
            }
        }

        if (providerName != null) {
            Provider provider = providerRepository.findByName(providerName);

            if (provider != null) {
                quest.setLocationId(provider.getId());
            }
        }

        questRepository.save(quest);
    });
}

}

© www.soinside.com 2019 - 2024. All rights reserved.