Home > Software design >  The injection point has the following annotations: @org.springframework.beans.factory.annotation.Aut
The injection point has the following annotations: @org.springframework.beans.factory.annotation.Aut

Time:10-18

Hello there I am new to spring boot, i am getting this error since a while, unfortunately can't fix it. I am googling since then but still not find what i did wrong. I believe the error exists in the Service Class. I tried to remove the field injection ( @Autowired) and implemented as a constructor injection but that did not work as well Find below my code:

Entity:

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;
    }
}

DAO

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> {
}

Main

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);
        }
}

Service Class

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);
    }
}

Error output:

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


Description:

Field repository in com.devops.maven.cars_api_maven.CarsController required a bean of type 'com.devops.maven.cars_api_maven.repositories.CarsRepository' that could not be found.

The injection point has the following annotations:

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

Action:

Consider defining a bean of type 'com.devops.maven.cars_api_maven.repositories.CarsRepository' in your configuration.

CodePudding user response:

Please remove exclude = {DataSourceAutoConfiguration.class } from below class and run again. It will fix the issue.

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);
        }
}
  • Related