一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - maven項目下solr和spring的整合配置詳解

maven項目下solr和spring的整合配置詳解

2021-06-15 10:57JavaIT程序員 Java教程

這篇文章主要介紹了maven項目下solr和spring的整合配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言:

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 人人爱天天做夜夜爽88 | 免费日批视频 | 日韩精品视频在线观看免费 | 亚洲男gay同性同志 亚洲免费在线看 | 日本人在线看片 | 91麻豆精品国产片在线观看 | 男女姓交大视频免费观看 | poronovideos极度残酷 | 深夜成人 | 91东航翘臀女神在线播放 | 国产亚洲高清国产拍精品 | 成人不卡在线 | 亚洲男人天 | 欧美精选视频 | 无码乱人伦一区二区亚洲 | 高h辣文小说网 烧书阁 | 亚洲精品日韩专区在线观看 | 日本大学生xxxxx69泡妞 | 亚洲精品 欧美 | 日本爽p大片免费观看 | 白丝h视频 | 欧美一区二区三区gg高清影视 | 欧美日韩成人在线视频 | 国产精品亚洲综合久久 | 狠狠色伊人亚洲综合网站色 | 天天gan | 欧美free激情野战hd | 国内自拍2019| 国产精品国产高清国产专区 | 免费一级欧美片在线观看 | 精品午夜中文字幕熟女人妻在线 | 国内精品久久久久影院男同志 | 精品视频免费在线观看 | 亚洲国产天堂久久综合网站 | 国产亚洲精品一区久久 | 9久热这里只有精品视频在线观看 | 风间由美理论片在线观看 | 成年美女黄网色大观看全 | 久久 这里只精品 免费 | 日韩福利一区 | 欧美日本道免费一区二区三区 |