Search code examples
javaspringspring-bootautowired

Springboot posgresql reposity bean can't be autowired


I have simple Sprinngboot app where actual database is PostgreSQL.

My model:-

@Table("carrier")
@Entity
public class MyCarrier { 
@Id
@Column("id")
private UUID id;
    @Size(
            max = 100
    )
    @Column("carrier_name")
    private String carrierName;
    @Size(
            max = 3
    )
    @Column("smdg_code")
    private String smdgCode;
    @Size(
            max = 4
    )
    @Column("nmfta_code")
    private String nmftaCode;

    public MyCarrier() {
    }

    public UUID getId() {
        return this.id;
    }

    public String getCarrierName() {
        return this.carrierName;
    }

    public String getSmdgCode() {
        return this.smdgCode;
    }

    public String getNmftaCode() {
        return this.nmftaCode;
    }

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

    public void setCarrierName(final String carrierName) {
        this.carrierName = carrierName;
    }

    public void setSmdgCode(final String smdgCode) {
        this.smdgCode = smdgCode;
    }

    public void setNmftaCode(final String nmftaCode) {
        this.nmftaCode = nmftaCode;
    }


    protected boolean canEqual(final Object other) {
        return other instanceof Carrier;
    }
}

Repository:-

@Repository
public interface MyCarrierRepository extends JpaRepository<MyCarrier, Long> {
}

Controller:-

@RestController
@RequestMapping(path = "/upload")
public class ReactiveUploadResource {
    Logger LOGGER = LoggerFactory.getLogger(ReactiveUploadResource.class);


    private final SqlRequestHandler sqlRequestHandler;

    @Autowired
    private  MyCarrierRepository myCarrierRepository;

    public ReactiveUploadResource(SqlRequestHandler sqlRequestHandler) {
        this.sqlRequestHandler = sqlRequestHandler;
    }
}

I got this error:-

Description:

Field myCarrierRepository in com.consumer.controller.ReactiveUploadResource required a bean of type 'com.consumer.repository.MyCarrierRepository' that could not be found.

What is missing? Why Springboot doesn't find this repository?


Solution

  • You have to put the repository inside the package at the same level as Application class the packages to allow Spring boot to scan it