Home > Blockchain >  Bean creation error on @Repository interface
Bean creation error on @Repository interface

Time:04-25

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface FormRepository extends MongoRepository<Form, Integer> {
    
    Optional<Form> findById(Integer formId);
}

This is a mongo repository I have created. I am accessing it as follows :

@Service
public class FormServiceImpl implements FormService {

        @Autowired
        FormRepository formRepository;
    @Override
    public List<Question> getQuestions(Integer formId, ){
            List<Question> questionList = new ArrayList<>();
            ..........
            ......................
            ........ initialise questionlist .............
            ......................

            formRepository.save(new Form(questionList, "firstForm"));
            return questionList;
        }
    }

Error I'm getting ->

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

Description:

Field formRepository in *.service.impl.FormServiceImpl required a bean of type '*.service.FormRepository' 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 '*.service.FormRepository' in your configuration.


Process finished with exit code 1

My thoughts : The @Repository should be enough to register the FormRepository as a bean. However, that is not the case. I'm not able to register the FormRepository interface as a bean. The two files above lie in the same package. I have replaced the prefix path with * - but rest assured * has the same meaning everywhere. Please let me know if any additional logs.

Edit :

I added @EnableMongoRepositories on my main application class. That leads to this error ->

2022-04-24 19:20:29.085 ERROR 2941 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field formRepository in *.service.impl.FormServiceImpl required a bean named 'mongoTemplate' 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 named 'mongoTemplate' in your configuration.


Process finished with exit code 1

I think MongoTemplate and MongoRepository are two very different things. Why does my mongo repo need a dependency to MongoTemplate?

CodePudding user response:

since you are using mongo you need to add an additional annotation.

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = FormRepository .class)
public class MainClass {}

also make sure your folders are structured correctly.

for the other Issue you should add a dependency in your 'POM'

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

It is all about mongo configuration. Hope this helps.

  • Related