maven 注入点有以下注解:@org.springframework.beans.factory.annotation.Autowired(required=true)

zlwx9yxi  于 2022-10-26  发布在  Maven
关注(0)|答案(1)|浏览(212)

你好,我是新来的春靴,我收到这个错误有一段时间了,不幸的是无法修复它。从那时起,我一直在谷歌上搜索,但仍然找不到我做错了什么。我认为错误存在于服务类中。我试图删除字段注入(@Autwire),并将其实现为构造函数注入,但这并不能很好地工作,如下面的代码所示:
实体:

package com.devops.maven.cars_api_maven.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "CARS")
@SequenceGenerator(name="seq", initialValue=4, allocationSize=100)
public class Car {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
    private Long id;
    String manufacturer;
    String model;
    int build;

    public Car() {
    }

    public Car(Long id, String manufacturer, String model, int build) {
        this.id = id;
        this.manufacturer = manufacturer;
        this.model = model;
        this.build = build;
    }

    public Long getId() {
        return id;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getModel() {
        return model;
    }

    public int getBuild() {
        return build;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

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

    public void setBuild(int build) {
        this.build = build;
    }
}

package com.devops.maven.cars_api_maven.repositories;

import com.devops.maven.cars_api_maven.model.Car;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CarsRepository extends JpaRepository<Car, Long> {
}

主要

package com.devops.maven.cars_api_maven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication (
        exclude = {DataSourceAutoConfiguration.class },
        scanBasePackages={
            "com.devops.maven", "com.devop.application"}
        )

    public class CarsApplication {

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

服务类别

package com.devops.maven.cars_api_maven;

import com.devops.maven.cars_api_maven.model.Car;
import com.devops.maven.cars_api_maven.repositories.CarsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cars")
@Service
public class CarsController {
   @Autowired
   private CarsRepository repository;

    @GetMapping
    public List<Car> getCars() {
        return repository.findAll();
    }

    @PostMapping
    public Car addCar(@RequestBody Car car) {
        return repository.save(car);
    }

    @SuppressWarnings("deprecation")
    @GetMapping(value = "/{id}")
    public Car getCarById(@PathVariable("id") long id) {
        return repository.getOne(id);
    }

    @DeleteMapping(value = "/{id}")
    public void removeCarById(@PathVariable("id") long id) {
        repository.deleteById(id);
    }
}

错误输出:

描述:
Com.devops.maven.cars_api_maven.CarsController中的字段存储库需要找不到的‘com.devops.maven.cars_api_maven.repositories.CarsRepository’类型的Bean。
注入点具有以下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:
考虑在您的配置中定义一个‘com.devops.maven.cars_api_maven.repositories.CarsRepository’类型的Bean。

e4yzc0pl

e4yzc0pl1#

请从下面的类中删除exclude = {DataSourceAutoConfiguration.class },然后重新运行。它会解决这个问题。

package com.devops.maven.cars_api_maven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication (
        exclude = {DataSourceAutoConfiguration.class },
        scanBasePackages={
            "com.devops.maven", "com.devop.application"}
        )

    public class CarsApplication {

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

相关问题