【问题标题】:Consider defining a bean of type in your configuration with my Service考虑使用我的服务在您的配置中定义一个类型的 bean
【发布时间】:2022-01-05 12:18:04
【问题描述】:

即使在我的类中添加了@Component、@Service 和 @Repository 注释后,我的代码也出现了这种情况:

这是它的资源:

package app.gym.v1.Resource.API;

import app.gym.v1.Model.Domain.Gym;
import app.gym.v1.Service.GymService;
import app.gym.v1.Utility.Constant.SwaggerConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping(path = "/gym")
@Api(tags = {SwaggerConstant.API_TAG3})
public class GymControl {
    private GymService gymService;

    @Autowired
    public GymControl(GymService gymService) {
        this.gymService = gymService;
    }

    @ApiOperation(value = "Get all available gyms", notes = "Retrieve a list of all gyms")
    @ApiResponses({@ApiResponse(responseCode = "200", description = "The list of gyms retrieved"),
            @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"),
            @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"),
            @ApiResponse(responseCode = "500", description = "An internal server error occurred"),
            @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"),
            @ApiResponse(responseCode = "401", description = "You don't have permission to this resource")
    })
    @GetMapping("/list_gym")
    public ResponseEntity<List<Gym>> getAllGyms() {
        List<Gym> gyms = gymService.getGyms();
        return new ResponseEntity<>(gyms, OK);
    }

    @ApiOperation(value = "Finding gym from list", notes = "Retrieve a gym from the search engine")
    @ApiResponses({@ApiResponse(responseCode = "200", description = "The gym was found"),
            @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"),
            @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"),
            @ApiResponse(responseCode = "500", description = "An internal server error occurred"),
            @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"),
            @ApiResponse(responseCode = "401", description = "You don't have permission to this resource")
    })
    @GetMapping("/find/{gymName}")
    public ResponseEntity<Gym> getGym(@PathVariable("gymName") String gymName) {
        Gym gym = gymService.findGymByGymName(gymName);
        return new ResponseEntity<>(gym, OK);
    }

}

这是它的存储库:

package app.gym.v1.Repo;

import app.gym.v1.Model.Domain.Gym;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GymRepo extends JpaRepository<Gym, Long> {

    Gym findByGymName(String gymName);
}

最后这就是它的服务:

package app.gym.v1.Service;

import app.gym.v1.Model.Domain.Gym;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface GymService {

    List<Gym> getGyms();

    Gym findGymByGymName(String gymName);
}

如您所见,我添加了所有必需的注释,但出现相同的错误并在调试器中不断给我相同的答案:


应用程序启动失败


说明:

app.gym.v1.Resource.API.GymControl 中构造函数的参数 0 需要一个无法找到的“app.gym.v1.Service.GymService”类型的 bean。

行动:

考虑在你的配置中定义一个“app.gym.v1.Service.GymService”类型的bean。

进程以退出代码 0 结束

*** 我搜索了很多关于它的内容,并尝试了 StackOverFlow 中的每一个答案和 GitHub 中的每一个答案,并回复了很多书,但一切都像停止。

【问题讨论】:

  • 到目前为止,您已将GymService 定义为接口。您没有任何实现 - 因此:没有 bean。你需要把它改成一个类,然后它就会开始工作了。
  • @MirekPluta 它不工作
  • @bhaskarkh 我看到了,明白了

标签: java hibernate jpa jakarta-ee spring-data


【解决方案1】:

Where should @Service annotation be kept? Interface or Implementation?你一定要勾选这个。基本上你需要实现GymService接口,然后才可以这样尝试。

@Service
public class GymServiceImpl  implements GymService{
    
    @Autowired
    GymRepo gymRepo

    @Override
    public List<Gym> getGyms() {
        // TODO Auto-generated method stub
        return gymRepo.findAll();
    }

    @Override
    public Gym findGymByGymName(String gymName) {
        // TODO Auto-generated method stub
        return gymRepo.findByGymName(gymName);
    }

}

【讨论】:

  • 它有效,但我无法为答案点赞,我没有足够的分数来做到这一点
  • 您可以接受正确的答案。请考虑这样做;)谢谢!
猜你喜欢
  • 2017-05-30
  • 2018-08-27
  • 2018-06-22
  • 1970-01-01
  • 2021-05-30
  • 2020-08-01
  • 2019-02-14
  • 2018-12-21
  • 2019-05-10
相关资源
最近更新 更多