Home > Mobile >  Spring Boot unable to scan MongoRepository
Spring Boot unable to scan MongoRepository

Time:07-19

I have a persistance interface that implements MongoRepository interface that looks as follows:

package org.prithvidiamond1.DB.Repositories;

import org.prithvidiamond1.DB.Models.SomeModel;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ServerRepository extends MongoRepository<SomeModel, String> {
}

Despite this, I keep getting the following error:

Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'botApplication': 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'org.prithvidiamond1.DB.Repositories.ServerRepository' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {}


PPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.prithvidiamond1.BotApplication required a bean of type 'org.prithvidiamond1.DB.Repositories.ServerRepository' that could not be found.


Action:

Consider defining a bean of type 'org.prithvidiamond1.DB.Repositories.ServerRepository' in your configuration.

I have tried many solutions (custom @ComponentScan, using @Service instead of @Component, etc.) I found on the internet but none could help me solve the problem, can someone explain to me what is wrong and how I should fix this?

Note: The directory structure is as follows (this is not the full directory structure, but I think this should be enough to get an idea):

org.prithvidiamond1
   |
    --BotApplication.java
   |
    --DB
      |
       --Repository
      |
       --ServerRepository.java

BotApplication.java looks as follows:

package org.prithvidiamond1;

import org.jc.api.Api;
import org.jc.api.ApiBuilder;
import org.jc.api.entity.server.Server;
import org.prithvidiamond1.DB.Models.Server;
import org.prithvidiamond1.DB.Repositories.ServerRepository;
import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class BotApplication {
    private final Api api;
    private final ServerRepository serverRepository;

    public BotApplication(ServerRepository serverRepository, Logger logger){
        String botToken = System.getenv().get("BOT_TOKEN");

        this.api = new ApiBuilder();

        this.serverRepository = serverRepository;

        appRuntime(logger);
    }

    public void appRuntime(Logger logger){
        logger.info("Bot has started!");

        // Handling server entries in the database
        if (this.serverRepository.findAll().isEmpty()) {
            logger.trace("server data repository empty, initializing data repository...");
            Collection<Server> servers = api.getServers();
            for (Server server : servers) {
                this.serverRepository.save(new Server(String.valueOf(server.getId())));
            }
            logger.trace("server data repository initialized");
        }
    }

    @Bean
    public Api getApi() {
        return this.api;
    }
}

CodePudding user response:

use annotation @Autowired with the constructor of BotApplication class as shown below -

@Autowired
public BotApplication(ServerRepository serverRepository, Logger logger){
        String botToken = System.getenv().get("BOT_TOKEN");

        this.api = new ApiBuilder();

        this.serverRepository = serverRepository;

        appRuntime(logger);
    }

CodePudding user response:

You need to use @EnableMongoRepositories on top of your main application class to enable scanning of mongo repository beans.

CodePudding user response:

You have Created Repository bean (ServerRepository)within your application.

But in your BotApplication component (which itself is a bean), you are not telling spring to inject dependency of Repository bean (i.e. Dependancy Injection).

Such a dependancy injection can be achieved by constructor , field-based or setter based methodologies.

You can either remove ServerRepository serverRepository from public BotApplication(ServerRepository serverRepository, Logger logger) constructor & just use :

@Autowired
private final ServerRepository serverRepository;

Or as other answer suggested, use @Autowired in Constructor itself and remove field ServerRepository serverRepository :

`
@Autowired
public BotApplication(ServerRepository serverRepository, Logger logger)
`

Please note, here, this.serverRepository = serverRepository; is also not required in constructor as dependency injection will take care of this.

  • Related