这篇文章主要介绍了maven项目下solr和spring的整合配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
前言:
solr和spring整合其实很简单,只要注意导入依赖的配置文件即可。废话不多说,上代码。
第一步:编写maven项目的pom文件,导入依赖<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配置文件的内容:<?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}"/>
<!-- 设置响应解析器 -->
<property name="parser">
<bean class="org.apache.solr.client.solrj.impl.xmlresponseparser"/>
</property>
<!-- 设置重试次数-->
<property name="maxretries" value="${solr.maxretries}"/>
<!-- 建立连接的最长时间 -->
<property name="connectiontimeout" value="${solr.connectiontimeout}"/>
</bean>
</beans> solr.properties配置文件的内容: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的包扫描,自动注入service -->
<context:component-scan base-package="com.millery" />
<!-- 使用spring自带的占位符替换功能 -->
<bean
class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
<!-- 允许jvm参数覆盖 -->
<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></span></a><pre name="code" class="html">
</bean>
</beans> 第四步:写测试代码
user实体类:package com.millery.spring_solr.pojo;
/**
*
* @项目名称:spring-solr
@类名称:user
@类描述:用户实体类 br/> * @创建人:millery
@创建时间: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类: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/> * @创建人:millery
@创建时间:2015年11月5日 上午10:48:57
@version: br/> */
@component
public class springsolr { br/>
@autowired
private httpsolrserver httpsolrserver;
public user getuser(long id) throws solrserverexception {
//创建查询条件
solrquery query = new solrquery();
query.setquery("id:" + id);
//查询并返回结果
queryresponse queryresponse = this.httpsolrserver.query(query);
return (user) queryresponse.getbeans(user.class);
}
} springsolrtest类:
springsolrtest.javapackage 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 {
// 测试方法,输出结果
user user = springsolr.getuser((long) 1);
system.out.println(user);
}
} 运行代码结果:org.apache.solr.client.solrj.solrserverexception: ioexception occured when talking to server at: http://127.0.0.1:8983/millery 这里抛异常时因为我本机上没有安装solr,无法连接solr,此时说明代码已经没有问题,可以执行查询操作了。
总结建工程时存在的小问题:
1、在建立工程时打包方式使用jar和war的选择可能存在纠结,只想说不用纠结,选哪个都是一样的。
2、在工程pom.xml配置文件配置完成后,可能会出现下图的报错问题,此时就需要简单的处理一下就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家。
原文链接:http://blog.51cto.com/13932491/2318092
|