【发布时间】:2019-05-03 11:13:00
【问题描述】:
我做了一个测试 Spring Controller 应用程序 -> 服务 -> 存储库
控制器->
@RestController
public class HelloController {
@Autowired
private ProductServiceImpl productService;
@RequestMapping("/getAll")
public List getAll(){
return productService.getAll();
}
}
服务->
@Service
public class ProductServiceImpl implements Services.ProductService {
@Autowired
private ProductRepository productRepository;
@Override
public List<Product> getAll() {
return productRepository.findAll();
}
}
存储库->
@Repository
public interface ProductRepository extends JpaRepository<Product,Long> {
}
应用 ->
@SpringBootApplication
@EnableJpaRepositories("Repository")
@ComponentScan("com.lopamoko")
public class CloudliquidApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(CloudliquidApplication.class, args);
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
我正在尝试在控制器中做 @自动连线 私有 ProductServiceImpl productServiceImpl; - 他发誓这没有豆子。 - 我在 Application Bean 中做 - 它开始发誓,它现在找不到 ProductRepository(接口)的 Bean - 当我从服务中调用它时。如何为接口制作 bean?
【问题讨论】:
-
try @EnableJpaRepositories(basePackages="org.my.pkg") // 提供@Repository注解类的基础包。
-
@EnableJpaRepositories - Application 类中已经存在
-
@ДимаГуманов 尝试将
@Autowired private ProductServiceImpl productService;更改为@Autowired private Services.ProductService productService; -
顺便说一句,Spring Data JPA 类(派生自
org.springframework.data.repository.Repository接口)上不需要@Repository- docs.spring.io/spring-boot/docs/current/reference/html/…
标签: java spring spring-data-jpa inversion-of-control