字段 vehicleRepository 需要一个找不到的 ..VehicleInterface 类型的 bean

Posted

技术标签:

【中文标题】字段 vehicleRepository 需要一个找不到的 ..VehicleInterface 类型的 bean【英文标题】:Field vehicleRepository required a bean of type ..VehicleInterface that could not be found 【发布时间】:2019-10-21 04:43:57 【问题描述】:

我正在尝试从服务实现类@autowire 存储库引用,但我仍然收到 bean not found 错误

我已经尝试在父级中移动 App.java 文件

App.java

package com.trucker;
import org.springframework.context.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App

    public static void main( String[] args )
        ApplicationContext applicationContext = SpringApplication.run(App.class,args);

    

VehicleService.java

package com.trucker.service;

import com.trucker.entity.Vehicle;
import com.trucker.repository.VehicleRepositoryInterface;

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

@Service
public class VehicleService implements VehicleServiceI 

    @Autowired
    VehicleRepositoryInterface vehicleRepository;

    @Override
    public Vehicle addVehicles(Vehicle vehicle) 
        System.out.println(vehicle);
        return vehicleRepository.save(vehicle);
    


VehicleRepositoryInterface.java

package com.trucker.repository;

import com.trucker.entity.Vehicle;
import org.springframework.context.annotation.Bean;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

public interface VehicleRepositoryInterface extends CrudRepository<Vehicle,String> 


VehicleController.java

package com.trucker.controller;

import com.trucker.entity.Vehicle;
import com.trucker.service.VehicleServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class VehicleController 

    @Autowired
    VehicleServiceI vehicleService;

    @RequestMapping(path = "/vehicles", method = RequestMethod.PUT)
    public String addVehicles(@RequestBody Vehicle vehicle) 
        System.out.println("************IN THE CONTROLLER************"+vehicle);
        vehicleService.addVehicles(vehicle);
        return "";
    

Vehicle.java

package com.trucker.entity;

import javax.persistence.Entity;
import java.sql.Timestamp;
import javax.persistence.Id;

@Entity
public class Vehicle 

    @Id
    String vin;
    String make;
    String model;
    int year;
    int redlineRpm;
    int maxFuelVolume;
    Timestamp lastServiceDate;

    public String getVin() 
        return vin;
    

    public void setVin(String vin) 
        this.vin = vin;
    

    public String getMake() 
        return make;
    

    public void setMake(String make) 
        this.make = make;
    

    public String getModel() 
        return model;
    

    public void setModel(String model) 
        this.model = model;
    

    public int getYear() 
        return year;
    

    public void setYear(int year) 
        this.year = year;
    

    public int getRedlineRpm() 
        return redlineRpm;
    

    public void setRedlineRpm(int redlineRpm) 
        this.redlineRpm = redlineRpm;
    

    public int getMaxFuelVolume() 
        return maxFuelVolume;
    

    public void setMaxFuelVolume(int maxFuelVolume) 
        this.maxFuelVolume = maxFuelVolume;
    

    public Timestamp getLastServiceDate() 
        return lastServiceDate;
    

    public void setLastServiceDate(Timestamp lastServiceDate) 
        this.lastServiceDate = lastServiceDate;
    

    @Override
    public String toString() 
        return "Vehicle" +
                "vin='" + vin + '\'' +
                ", make='" + make + '\'' +
                ", model='" + model + '\'' +
                ", year=" + year +
                ", redlineRpm=" + redlineRpm +
                ", maxFuelVolume=" + maxFuelVolume +
                ", lastServiceDate=" + lastServiceDate +
                '';
    

application.properties

application.properties
server.port=8084
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.datasource.driver-class=oracle.jdbc.driver.OracleDriver
spring.datasource.username=system
spring.datasource.password=system
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

目录结构:

src
src/main
src/main/resources/application.properties
src/main/java
src/main/java/com.trucker
src/main/java/com.trucker/App.java
src/main/java/com.trucker.controller
src/main/java/com.trucker.controller.VehicleController
src/main/java/com.trucker.entity.Vehicle
src/main/java/com.trucker.repository
src/main/java/com.trucker.repository.VehicleRepositoryInterface
src/main/java/com.trucker.service
src/main/java/com.trucker.service.vehicleService

【问题讨论】:

【参考方案1】:

看看这篇文章有明确的答案

Spring boot autowiring an interface with multiple implementations

Spring autowire interface

【讨论】:

以上是关于字段 vehicleRepository 需要一个找不到的 ..VehicleInterface 类型的 bean的主要内容,如果未能解决你的问题,请参考以下文章

为啥如果有一个无效的字段,需要有效的表单 2 次才能获得有效的下一个字段?

如果字段为空需要另一个字段,Laravel 验证规则

仅当前一个字段已填写时才需要表单字段

Symfony 表单验证:如果另一个字段不为空,则需要字段

表单字段需要一个数组

带有两个提交按钮的 Django 表单。 . .一个需要字段,一个不需要