【问题标题】:spring data solr showcaseSpring Data solr 展示
【发布时间】:2014-12-04 22:51:17
【问题描述】:

我正在尝试了解 spring data solr 展示项目。

https://github.com/christophstrobl/spring-data-solr-showcase

花了相当多的时间后,我在https://github.com/christophstrobl/spring-data-solr-showcase/blob/master/src/main/java/org/springframework/data/solr/showcase/product/ProductServiceImpl.java

中找不到productRepository是如何实现注入
@Service class ProductServiceImpl implements ProductService { 
private static final Pattern IGNORED_CHARS_PATTERN = Pattern.compile("\\p{Punct}"); 
private ProductRepository productRepository;


@Autowired
public void setProductRepository(ProductRepository productRepository) {
    this.productRepository = productRepository;
}

ProductRepository 被定义为接口 (https://github.com/christophstrobl/spring-data-solr-showcase/blob/master/src/main/java/org/springframework/data/solr/showcase/product/ProductRepository.java),我没有找到任何实现该接口的代码

interface ProductRepository extends SolrCrudRepository<Product, String> {

    @Highlight(prefix = "<b>", postfix = "</b>")
    @Query(fields = { SearchableProductDefinition.ID_FIELD_NAME, 
                      SearchableProductDefinition.NAME_FIELD_NAME,
                      SearchableProductDefinition.PRICE_FIELD_NAME, 
                      SearchableProductDefinition.FEATURES_FIELD_NAME,
                      SearchableProductDefinition.AVAILABLE_FIELD_NAME }, 
           defaultOperator = Operator.AND)
    HighlightPage<Product> findByNameIn(Collection<String> names, Pageable page);

    @Facet(fields = { SearchableProductDefinition.NAME_FIELD_NAME })
    FacetPage<Product> findByNameStartsWith(Collection<String> nameFragments, Pageable pagebale);
}

下面是 spring 上下文的配置方式: https://github.com/christophstrobl/spring-data-solr-showcase/blob/master/src/main/java/org/springframework/data/solr/showcase/Application.java

如果有人能指出这个接口的实现和注入方向,那就太好了。

【问题讨论】:

    标签: spring-data spring-data-solr


    【解决方案1】:

    展示使用 Spring Data 存储库抽象,使用方法名称的查询派生。因此Spring Data 和 Solr 模块提供的基础设施负责为您创建所需的实现。更详细的解释请看Reference Documentation

    展示本身的构建方式允许您通过查看从一个步骤过渡到另一个步骤的差异来step through several stages of development。所以看看Step 2 展示了如何使用Custom Repository Implementation,而Step 4 演示了如何使用@Highlight 启用突出显示。

    【讨论】:

    • 从马口中...感谢您的回复。
    【解决方案2】:

    Spring Data 的目标是减少样板代码的数量(意味着减少代码的重复)。
    对于保存等基本方法,找到spring提供的实现,spring将为这些接口创建bean(Objetcs)。
    为了告诉spring这些是我在这个包中的存储库,我们写@987654322 @ 或 &lt;jpa:repositories base-package="com.acme.repositories"/&gt; 用于 JPA 存储库
    对于 solr 存储库,我们必须编写 @EnableSolrRepositories(basePackages="com.spring.repositories"&lt;solr:repositories base-package="com.acme.repositories" /&gt; Spring 将为这些接口创建对象,我们可以使用 @Autowire 注释注入这些接口对象。

    Example:
     @Service
     Pulic class SomeService{
         @Autowire
         private SampleRepository;
    
         /*  @postConstruct annotation is used to execute method just after creating bean 
              and injecting all dependencies by spring*/
         @PostConstruct
         public void postConstruct(){
              System.out.println("SampleRepository implementation class name"+ SampleRepository.getClass());
         }
     }
    

    上面的例子是看SampleRepository接口的实现类(这个类不是用户定义的,是spring给的类)。
    参考文档链接http://docs.spring.io/spring-data/solr/docs/2.0.2.RELEASE/reference/html/
    尝试阅读这个简单的文档,您可以获得更多关于spring-data的知识。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      相关资源
      最近更新 更多