Reputation: 457
I have created a small spring boot project which will do DB interaction, i am using @Controller
, @Service
and @Repository
.
The spring boot main class is in parent package com.bisnu.main
, then the controller,service and repository are under the parent package like com.bisnu.main.service
....
Every thing is fine but while @Autowired
for repository, its not able to create bean for the repository, its giving error.
@EnableSwagger2
@RestController
@CrossOrigin(origins="*",allowedHeaders="*")
@RequestMapping("/TestController")
public class TestController {
@Autowired
private SourceService sourceService;
@GetMapping("/getSourcelist")
public List<SourceListDTO> getAllTelevisionSource(){
List<SourceListDTO> televisionSource = null;
televisionSource = sourceService.getTelevisionSource();
return televisionSource;
}
}
@Service
public class SourceService {
@Autowired
private TestRepository testRepo;
public List<SourceListDTO> getTelevisionSource() {
Pageable pageable = PageRequest.of(0, 100);
List<SourceListDTO> list = testRepo.findSourceList(pageable);
return list;
}
}
public interface TestRepository extends JpaRepository<MyTelevisionSource,Long> {
@Query("select query")
List<SourceListDTO> findSourceList(Pageable pageable);
}
I am getting below error,
Field testRepo in com.tivo.extract.core.service.SourceService required a bean of type 'com.tivo.extract.core.repository.TestRepository' 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 'com.tivo.extract.core.repository.TestRepository' in your configuration.
Can anyone guide, what is going wrong here.
thanks
Upvotes: 0
Views: 359
Reputation: 7521
Add @Repository
annotation to the class
@Repository
public interface TestRepository extends JpaRepository<MyTelevisionSource,Long> {
...
And add following annotation either to main class (marked with @SpringBootApplication
) or any @Configuration
-marked class
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.example.repositories"})
@EntityScan("com.example.entities")
public class BootApplication {
...
Upvotes: 1