I am working with spring boot tutorial from javabrains and everything was clear until putting CrudRepository inside project. Below you can find my main class:

package pl.springBootStarter.app;

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

@SpringBootApplication
public class CourseApiDataApplication
{
    public static void main(String args[])
{
    SpringApplication.run(CourseApiDataApplication.class,args);
}
}

Service class:

package pl.springBootStarter.app.topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TopicService
{
    @Autowired
    private TopicRepository topicRepository;

    private List<Topic> topics =  new ArrayList<>(Arrays.asList(
            new Topic("spring","spring framework", "spring framework dectription"),
            new Topic("sprin","spring framework", "spring framework dectription"),
            new Topic("spri","spring framework", "spring framework dectription")));

    public  List<Topic> getAllTopics()
    {
    //    return topics;
    List<Topic> t = new ArrayList<Topic>();
    topicRepository.findAll().forEach(t::add);
    return t;
    }

    public Topic getTopic (String id)
    {
        return   topics.stream().filter( t -> t.getId().equals(id)).findFirst().get();
    }

    public void addTopic(Topic topic) {
        topicRepository.save(topic);
    }

    public void updateTopic(Topic topic, String id)
    {
        topics.set(topics.indexOf(topics.stream().filter(t-> t.getId().equals(id)).findFirst().get()), topic);
    }

    public void deleteTopic(String id)
    {
        topics.remove(topics.stream().filter(t -> t.getId().equals(id)).findFirst().get());
    }
}

And Repository interface:

package pl.springBootStarter.app.topic;

import org.springframework.data.repository.CrudRepository;

public interface TopicRepository extends CrudRepository<Topic,String>
{

}

When I run the app there is a problem with injection of TopicRepository into topicRepository field in TopicService class. I get following error:

Error starting ApplicationContext. To display the conditions report re-       run your application with 'debug' enabled.
2019-05-01 10:33:52.206 ERROR 6972 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field topicRepository in pl.springBootStarter.app.topic.TopicService required a bean of type 'pl.springBootStarter.app.topic.TopicRepository' that could not be found.

The injection point has the following annotations:
-    @org.springframework.beans.factory.annotation.Autowired(required=true)

What could be the reason that Spring cannot do the autowiring?

2 Answers

2
lealceldeiro On Best Solutions

You need to annotate TopicRepository as a @Repository.

@Repository
public interface TopicRepository extends CrudRepository<Topic,String>
{
}

Also be sure the class is scanned by spring (this may help if that's the problem: Intellij Springboot problems on startup).

See a demo code here: https://github.com/lealceldeiro/repository-demo

1
Piotr Podraza On

Spring cannot inject bean because it has not been created.

You have to instruct Spring to generate implementation of declared repository interfaces by using @EnableJpaRepositories(basePackages={"pl.springBootStarter.app"}) annotation on any of your configuration classes or class annotated with @SpringBootApplication. That should fix your problem.