Spring 存储库的 DI (JAVA)
Spring DI for Repository (JAVA)
我做了一个测试Spring Controller application -> Service -> Repository
控制器->
@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);
}
}
我想在控制器中做
@Autowired
私有 ProductServiceImpl productServiceImpl; - 他发誓说这没有豆子。 - 我在 Application Bean 中做 - 它开始发誓,它现在找不到 ProductRepository(接口)的 Bean - 当我从服务中调用它时。如何为接口创建一个 bean?
我认为您的问题在于 @EnableJpaRepositories
的值,这可能是误导,它指向错误的包?。 @EnableJpaRepositories
的值表示要扫描存储库的基础包。
如果 ProductRepository 位于 "com.lopamoko" 内,您可以将该值留空。
@SpringBootApplication
@EnableJpaRepositories
@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);
}
}
因为您已经在@ComponentScan("com.lopamoko")
中指定了要扫描的包
如果您的存储库位于不同的包中,则需要将包指定为 @EnableJpaRepositories
的值
@SpringBootApplication
@EnableJpaRepositories("com.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);
}
}
不要忘记用 JPA 注释来注释您的产品实体 @Entity
我做了一个测试Spring Controller application -> Service -> Repository
控制器->
@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);
}
}
我想在控制器中做 @Autowired 私有 ProductServiceImpl productServiceImpl; - 他发誓说这没有豆子。 - 我在 Application Bean 中做 - 它开始发誓,它现在找不到 ProductRepository(接口)的 Bean - 当我从服务中调用它时。如何为接口创建一个 bean?
我认为您的问题在于 @EnableJpaRepositories
的值,这可能是误导,它指向错误的包?。 @EnableJpaRepositories
的值表示要扫描存储库的基础包。
如果 ProductRepository 位于 "com.lopamoko" 内,您可以将该值留空。
@SpringBootApplication
@EnableJpaRepositories
@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);
}
}
因为您已经在@ComponentScan("com.lopamoko")
如果您的存储库位于不同的包中,则需要将包指定为 @EnableJpaRepositories
@SpringBootApplication
@EnableJpaRepositories("com.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);
}
}
不要忘记用 JPA 注释来注释您的产品实体 @Entity