【问题标题】:Spring DI for Repository (JAVA)Spring DI for Repository (JAVA)
【发布时间】: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


【解决方案1】:

我认为您的问题在于@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") 中指定了要扫描的包

如果你的 Repository 位于不同的包中,你需要将包指定为@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 注释您的 Product 实体

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    相关资源
    最近更新 更多