全文检索Solr7.7.2整合Spring的具体方法,基于使用.xml配置文件方式注入bean对象(HttpSolrClient)

一.环境说明

Windows 10 x64

IDEA 2019.2.3 

Spring 4.3.25.RELEASE

Solr 7.7.2

二.开始吧,少年

最近在做solr的升级工作,从4.10升级到最新的7.7,却发现困难难重重.因为相关可参考的资料太少了,能搜到的基本都是停留在solr4或5之类的内容,而再往上的却只有寥寥,让我这个菜鸟很是忧伤.没办法,只能一步步查源码,翻官方文档,来研究更新了什么。

三.对比

在4.10.3的时候,配置是这样的,新建个application-solr.xml,作为Solr的配置文件(个人喜好,也可以不单独出来)。

首先一个改变就是,在4.10的时候,要配置的是HttpSolrServer,而在7.7的时候,你需要配置的东西改名成HttpSolrClient了。

下面就是我们Spring整合Solr7所需的配置文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans" xsi:schemalocation="https://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
 
<!--版本更新说明:
    1.从solr5.x开始,HttoSolrServer就变成了HttpSolrClient
    2.查看源码发现,之前的构造方法已经修改,以前的注入方法也不再适用.主要由一个静态类builder来构造,而builder需要一个baseUrl,
      所以差不多就是之前的单baseUrl的构造方法(源码179,830)
 -->
<bean class="org.apache.solr.client.solrj.impl.HttpSolrClient" id="httpSolrClient">
    <constructor-arg value="solrCore的远程地址" name="builder">
</constructor-arg></bean>
 
</beans>
 

最后附录:这是solr4.10.3的applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans" xsi:schemalocation="https://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
 
    <bean class="org.apache.solr.client.solrj.impl.HttpSolrServer" id="httpSolrServer">
        <constructor-arg value="solrCore的远程地址">
    </constructor-arg></bean>
 
</beans>
 
发布了95 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_40993412/article/details/102647483