I am trying to use a MongoRepository, but Spring complains, that no instance of the MonoRepository-Interface is in the context.
@SpringBootApplication public class BackendApplication { public static void main(String[] args) { SpringApplication.run(BackendApplication.class, args); } } @Document(collection = "tableItems") class TableItem { @Id public String id; public int roundNbr; } interface TableItemsRepository extends MongoRepository<TableItem, String> { } @Service class TableItemsService { @Autowired public TableItemsService(TableItemsRepository tableItemsRepository) { } }
The server complains:
Parameter 0 of constructor in backend.TableItemsService required a bean of type 'backend.TableItemsRepository' that could not be found. Action: Consider defining a bean of type 'backend.TableItemsRepository' in your configuration.
my maven pom.xml:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>3.4.0</version> </dependency>
How can I get a bean instance of my MongoRepository?
Advertisement
Answer
The dependency is incorrect.
spring-data-mongodb
only makes it compile:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>3.4.0</version> </dependency>
But you need additional dependencies, to make it run. For convenience you can import spring-boot-starter-data-mongodb
, which will import all required artifacts as transitive dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>