maven項(xiàng)目下solr和spring的整合配置詳解
前言:
solr和spring整合其實(shí)很簡(jiǎn)單,只要注意導(dǎo)入依賴的配置文件即可。廢話不多說,上代碼。
第一步:編寫maven項(xiàng)目的pom文件,導(dǎo)入依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">; <modelVersion>4.0.0</modelVersion> <groupId>com.millery.spring_solr</groupId> <artifactId>spring-solr</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 添加依賴 --> <dependencies> <!-- Spring依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.3.RELEASE</version> </dependency> <!--solr客戶端solrj的依賴 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.10.1</version> </dependency> <!-- junit測(cè)試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
第二步:編寫applicationContext-solr.xml和solr.properties配置文件
applicationContext-solr.xml配置文件的內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">; <!--定義solr的server--> <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg index="0" value="${solr.Url}"/> <!-- 設(shè)置響應(yīng)解析器 --> <property name="parser"> <bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/> </property> <!-- 設(shè)置重試次數(shù)--> <property name="maxRetries" value="${solr.maxRetries}"/> <!-- 建立連接的最長(zhǎng)時(shí)間 --> <property name="connectionTimeout" value="${solr.connectionTimeout}"/> </bean> </beans>
solr.properties配置文件的內(nèi)容:
solr.Url=http://127.0.0.1:8983/millery solr.maxRetries=1 solr.connectionTimeout=500
第三步:編寫applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">; <!--配置service的包掃描,自動(dòng)注入Service --> <context:component-scan base-package="com.millery" /> <!-- 使用spring自帶的占位符替換功能 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 允許JVM參數(shù)覆蓋 --> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <!-- 忽略沒有找到的資源文件 --> <property name="ignoreResourceNotFound" value="true" /> <!-- 配置資源文件 --> <property name="locations"> <list> <value>classpath:solr.properties</value> </list> </property> <a target=_blank rel="external nofollow" ><span style="color: rgb(0, 0, 255);"></span></a><pre name="code" class="html"> </bean> </beans>
第四步:寫測(cè)試代碼
User實(shí)體類:
package com.millery.spring_solr.pojo; /** * * @項(xiàng)目名稱:spring-solr @類名稱:User @類描述:用戶實(shí)體類 br/> * @創(chuàng)建人:millery @創(chuàng)建時(shí)間:2015年11月5日 上午10:42:43 @version: br/> */ public class User { private Long id;// 用戶編號(hào) private String username;// 用戶名 private String loginPwd;// 用戶登錄密碼 private String email;// 用戶郵箱 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getLoginPwd() { return loginPwd; } public void setLoginPwd(String loginPwd) { this.loginPwd = loginPwd; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", loginPwd=" + loginPwd + ", email=" + email + "]"; } }
SpringSolr類:
import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.millery.spring_solr.pojo.User; /** * * @項(xiàng)目名稱:spring-solr br/> */ import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.millery.spring_solr.pojo.User; /** * * @項(xiàng)目名稱:spring-solr @類名稱:SpringSolrTest @類描述:測(cè)試類 br/> * @創(chuàng)建人:millery @創(chuàng)建時(shí)間:2015年11月5日 上午10:48:57 @version: br/> */ @Component public class SpringSolr { br/> @Autowired private HttpSolrServer httpSolrServer; public User getUser(Long id) throws SolrServerException { //創(chuàng)建查詢條件 SolrQuery query = new SolrQuery(); query.setQuery("id:" + id); //查詢并返回結(jié)果 QueryResponse queryResponse = this.httpSolrServer.query(query); return (User) queryResponse.getBeans(User.class); } }
SpringSolrTest類:
SpringSolrTest.java
package com.millery.spring_solr.test; import org.apache.solr.client.solrj.SolrServerException; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.millery.spring_solr.pojo.User; public class SpringSolrTest { private SpringSolr springSolr;br/>@Before public void setUp() throws Exception { // 初始化Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml", "applicationContext-solr.xml"); //獲取對(duì)象 this.springSolr = applicationContext.getBean(SpringSolr.class);br/>} @Test public void test() throws SolrServerException { // 測(cè)試方法,輸出結(jié)果 User user = springSolr.getUser((long) 1); System.out.println(user); } }
運(yùn)行代碼結(jié)果:
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://127.0.0.1:8983/millery
這里拋異常時(shí)因?yàn)槲冶緳C(jī)上沒有安裝solr,無法連接solr,此時(shí)說明代碼已經(jīng)沒有問題,可以執(zhí)行查詢操作了。
總結(jié)建工程時(shí)存在的小問題:
1、在建立工程時(shí)打包方式使用jar和war的選擇可能存在糾結(jié),只想說不用糾結(jié),選哪個(gè)都是一樣的。
2、在工程pom.xml配置文件配置完成后,可能會(huì)出現(xiàn)下圖的報(bào)錯(cuò)問題,此時(shí)就需要簡(jiǎn)單的處理一下就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12java排查一個(gè)線上死循環(huán)cpu暴漲的過程分析
這篇文章主要介紹了java排查一個(gè)線上死循環(huán)cpu暴漲的過程分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08java jackson 將對(duì)象轉(zhuǎn)json時(shí),忽略子對(duì)象的某個(gè)屬性操作
這篇文章主要介紹了java jackson 將對(duì)象轉(zhuǎn)json時(shí),忽略子對(duì)象的某個(gè)屬性操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10java HttpServletRequest和HttpServletResponse詳解
這篇文章主要介紹了java HttpServletRequest和HttpServletResponse詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12MyBatis typeAliases元素標(biāo)簽(含注解方式)及其屬性、設(shè)置方式
這篇文章主要介紹了MyBatis typeAliases元素標(biāo)簽(含注解方式)及其屬性、設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09通過反射注解批量插入數(shù)據(jù)到DB的實(shí)現(xiàn)方法
今天小編就為大家分享一篇關(guān)于通過反射注解批量插入數(shù)據(jù)到DB的實(shí)現(xiàn)方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03mybatis打印的sql日志不寫入到log文件的問題及解決
這篇文章主要介紹了mybatis打印的sql日志不寫入到log文件的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08