spring 引导应用程序中的存储库无法识别@Autowired 注释
@Autowired annotation is not recognized for repository in spring boot application
我创建了一个小型 spring 启动项目,它将进行数据库交互,我正在使用 @Controller
、@Service
和 @Repository
。
spring boot main class 在父包 com.bisnu.main
中,然后控制器、服务和存储库在父包下 com.bisnu.main.service
...
一切都很好,但是 @Autowired
对于存储库,它无法为存储库创建 bean,它给出错误。
@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);
}
我遇到了以下错误,
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.
谁能指导一下,这是怎么回事。
谢谢
将 @Repository
注释添加到 class
@Repository
public interface TestRepository extends JpaRepository<MyTelevisionSource,Long> {
...
并在主 class(标有 @SpringBootApplication
)或任何标有 @Configuration
的 class
中添加以下注释
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.example.repositories"})
@EntityScan("com.example.entities")
public class BootApplication {
...
我创建了一个小型 spring 启动项目,它将进行数据库交互,我正在使用 @Controller
、@Service
和 @Repository
。
spring boot main class 在父包 com.bisnu.main
中,然后控制器、服务和存储库在父包下 com.bisnu.main.service
...
一切都很好,但是 @Autowired
对于存储库,它无法为存储库创建 bean,它给出错误。
@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);
}
我遇到了以下错误,
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.
谁能指导一下,这是怎么回事。
谢谢
将 @Repository
注释添加到 class
@Repository
public interface TestRepository extends JpaRepository<MyTelevisionSource,Long> {
...
并在主 class(标有 @SpringBootApplication
)或任何标有 @Configuration
的 class
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.example.repositories"})
@EntityScan("com.example.entities")
public class BootApplication {
...