前言:
solr和spring整合其實很簡單,只要注意導(dǎo)入依賴的配置文件即可。廢話不多說,上代碼。
第一步:編寫maven項目的pom文件,導(dǎo)入依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<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測試 --> <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)容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?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}" /> <!-- 建立連接的最長時間 --> <property name= "connectiontimeout" value= "${solr.connectiontimeout}" /> </bean> </beans> |
solr.properties配置文件的內(nèi)容:
1
2
3
|
solr.url=http: //127.0.0.1:8983/millery solr.maxretries= 1 solr.connectiontimeout= 500 |
第三步:編寫applicationcontext.xml配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?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的包掃描,自動注入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 href= "http://write.blog.csdn.net/user.java" rel= "external nofollow" ><span style= "color: rgb(0, 0, 255);" ></span></a><pre name= "code" class = "html" > </bean> </beans> |
第四步:寫測試代碼
user實體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package com.millery.spring_solr.pojo; /** * * @項目名稱:spring-solr @類名稱:user @類描述:用戶實體類 br/> * @創(chuàng)建人:millery @創(chuàng)建時間:2015年11月5日 上午10:42:43 @version: br/> */ public class user { private long id; // 用戶編號 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類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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; /** * * @項目名稱: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; /** * * @項目名稱:spring-solr @類名稱:springsolrtest @類描述:測試類 br/> * @創(chuàng)建人:millery @創(chuàng)建時間: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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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" ); //獲取對象 this .springsolr = applicationcontext.getbean(springsolr. class );br/>} @test public void test() throws solrserverexception { // 測試方法,輸出結(jié)果 user user = springsolr.getuser(( long ) 1 ); system.out.println(user); } } |
運行代碼結(jié)果:
org.apache.solr.client.solrj.solrserverexception: ioexception occured when talking to server at: http://127.0.0.1:8983/millery
這里拋異常時因為我本機上沒有安裝solr,無法連接solr,此時說明代碼已經(jīng)沒有問題,可以執(zhí)行查詢操作了。
總結(jié)建工程時存在的小問題:
1、在建立工程時打包方式使用jar和war的選擇可能存在糾結(jié),只想說不用糾結(jié),選哪個都是一樣的。
2、在工程pom.xml配置文件配置完成后,可能會出現(xiàn)下圖的報錯問題,此時就需要簡單的處理一下就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.51cto.com/13932491/2318092