Reputation: 343
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?
Upvotes: 5
Views: 39456
Reputation: 1
1 the main package is ex: com.demo.project and the remaining package naming should start with com.demo.project 2.For controller package will be like com.demo.project.controller 3.Simlarly for service all well com.demo.project.service
Upvotes: 0
Reputation: 1
Upvotes: 0
Reputation: 4078
In my cases, the necessary configuration from org.springframework.boot.autoconfigure.jdbc.
has been excluded at SpringBootApplication, causing relevant bean not added properly. Check your main application java file and see if you can find following configuration in the exclusion list
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
@SpringBootApplication(
exclude = {
DataSourceAutoConfiguration.class, // REMOVE THIS
DataSourceTransactionManagerAutoConfiguration.class, // REMOVE THIS
}
)
and remove them from exclusion list.
Upvotes: 0
Reputation: 14988
Be sure the class is scanned by spring!
(this may help if that's the problem: Intellij Springboot problems on startup).
Optionally you may want to annotate TopicRepository
as a @Repository
.
@Repository
public interface TopicRepository extends CrudRepository<Topic,String>
{
}
See a demo code here: https://github.com/lealceldeiro/repository-demo
Upvotes: 5
Reputation: 1
I got a similar message.
the thing was my main package was com.example and the package for other classes was com.xyz
so when I Changed the name of the package of other class to com.example.topic
i.e. finally The main package was com.example and the package for the other class was com.example.topic
A simple mistake, posting in case it helps anyone else.
Upvotes: 0
Reputation: 56
I got a similar message and I was missing the @Service annotation in the Service class. Simple mistake, posting in case it helps anyone else.
Upvotes: 3
Reputation: 385
For anybody who was brought here by googling the generic bean error message, but who is actually trying to add a feign client to their Spring Boot application via the @FeignClient
annotation on your client interface, none of the above solutions will work for you.
To fix the problem, you need to add the @EnableFeignClients
annotation to your Application class, like so:
@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {
Upvotes: 1
Reputation: 2029
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.
Upvotes: 3