Magnus
Magnus

Reputation: 728

'Field required a bean of type that could not be found.' Spring can't find my repository interface

I'm trying to get my skeleton Spring webapp up and running so I can start testing POST requests etc. However, the builder throws an exception:

Description:

Field repository in com.internal.signing.model.SigningOrder.SigningOrder required a bean of type 'com.internal.signing.model.SigningOrder.SigningOrderRepository' that could not be found.

Action:

Consider defining a bean of type 'com.internal.signing.model.SigningOrder.SigningOrderRepository' in your configuration.

I do indeed have a class com.internal.signing.model.SigningOrder.SigningOrderRepository. In what context am I supposed to define this bean? My project structure is defined below. There is a similar question here, but the answers do not apply to my case (or so I think).

└── com
   └── internal
      └── signing
         ├── Application.java
         ├── controller
         │  └── SigningOrderController.java
         └── model
            └── SigningOrder
               ├── SigningOrder.java
               └── SigningOrderRepository.java

My SigningOrderController.java:

package com.internal.signing.controller;

import com.internal.signing.model.SigningOrder.SigningOrder;
import com.internal.signing.model.SigningOrder.SigningOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SigningOrderController {

    @Autowired
    private SigningOrderRepository repo;

    @PostMapping("/send")
    @ResponseBody
    public String foo()
    {
        Iterable<SigningOrder> orders = repo.findAll();
        return orders.toString();
    }
}

My SigningOrder.java:

package com.internal.signing.model.SigningOrder;

import com.internal.signing.model.Notification;
import com.internal.signing.model.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import java.io.Serializable;
import java.util.List;

@Component
@Entity
public class SigningOrder implements Serializable {

    @Autowired
    private SigningOrderRepository repository;

    @GeneratedValue
    private int id;

    private List<Subject> listOfSubjects;
.
    private List<String> authenticationMethods;

    private List<String> signingMethods;

    private List<Notification> listofNotifications;

    //Getters and setters
}

My SigningOrderRepository.java:

package com.internal.signing.model.SigningOrder;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

@Service //This was a tip in an answer with many upvotes in the linked questions. I have tried with
// "@Repository" annotation as well, but to no avail.
public interface SigningOrderRepository extends CrudRepository<SigningOrder, Integer> {

}

My Application.java:

package com.internal.signing;

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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("In application");
    }
}

Upvotes: 0

Views: 1310

Answers (1)

mrkernelpanic
mrkernelpanic

Reputation: 4471

Change:

@Component
@Entity
public class SigningOrder implements Serializable {

    @Autowired
    private SigningOrderRepository repository;

    @GeneratedValue
    private int id;

    private List<Subject> listOfSubjects;

    private List<String> authenticationMethods;

    private List<String> signingMethods;

    private List<Notification> listofNotifications;

        //Getters and setters
    }

to

@Entity
public class SigningOrder implements Serializable {

    @GeneratedValue
    private int id;

    private List<Subject> listOfSubjects;

    private List<String> authenticationMethods;

    private List<String> signingMethods;

    private List<Notification> listofNotifications;

    //Getters and setters
}

And you need to extend your Spring Config to add following annotations:

@EnableJpaRepositories(basePackages = {"com.internal.signing.model"})
@EntityScan(value = {"com.internal.signing.model"})
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("In application");
    }
}

Upvotes: 1

Related Questions