maven项目下solr和spring的整合配置

前言:

solr和spring整合其实很简单,只要注意导入依赖的配置文件即可。废话不多说,上代码。

第一步:编写maven项目的pom文件,导入依赖

[html] view plain copy
<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"&gt;??
????<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配置文件的内容:

[html] view plain copy
<?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"&gt;??
??????
????<!--定义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配置文件的内容:

[html] view plain copy
solr.Url=http://127.0.0.1:8983/millery??
solr.maxRetries=1??
solr.connectionTimeout=500??

第三步:编写applicationContext.xml配置文件

[html] view plain copy
<?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"&gt;??
??
????<!--配置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"><span?style="color:?rgb(0,?0,?255);"></span></a><pre?name="code"?class="html"><span?style="white-space:pre">???</span></bean>??
[html] view plain copy
</beans>??

第四步:写测试代码

User实体类:

[html] view plain copybr/>package?com.millery.spring_solr.pojo;??
??
/**??
?*???
?*[email protected]项目名称:spring-solr??
?[email protected]类名称:User??
?
[email protected]类描述:用户实体类??br/>?*[email protected]创建人:millery??
?[email protected]创建时间:2015年11月5日?上午10:42:43???
?
[email protected]:??
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;??
????}??
??
[email protected]??
????public?String?toString()?{??
????????return?"User?[id="?+?id?+?",?username="?+?username?+?",?loginPwd="??
????????????????+?loginPwd?+?",?email="?+?email?+?"]";??
????}??
}??
SpringSolr类:

[html] view plain copy??
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;??
??
/**??
?*???
?*[email protected]项目名称: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;??
??
/**??
?*???
?*[email protected]项目名称:spring-solr??
?[email protected]类名称:SpringSolrTest??
?
[email protected]类描述:测试类??
br/>?*[email protected]创建人:millery???
?[email protected]创建时间:2015年11月5日?上午10:48:57???
?
[email protected]:??
br/>?*/??
@Component??
public?class?SpringSolr?{??
br/>??
[email protected]??
????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类:

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 {
// 测试方法,输出结果
User user = springSolr.getUser((long) 1);
System.out.println(user);
}
}
来自CODE的代码片
SpringSolrTest.java

运行代码结果:

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配置文件配置完成后,可能会出现下图的报错问题,此时就需要简单的处理一下就可以了。

原文地址:http://blog.51cto.com/13932491/2318092

时间: 2024-10-08 01:40:47

maven项目下solr和spring的整合配置的相关文章

Maven项目下HttpServletRequest 或 HttpServletResponse需引用的依赖包

转载: http://xyly624.blog.51cto.com/842520/865630/ Maven项目下HttpServletRequest 或 HttpServletResponse需引用的依赖包:servlet-api.jar,并将scope设置为provided. pom.xml: <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifac

Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderL

Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderL     严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.co

Maven项目下 java.lang.ClassNotFoundException 常规解决办法

网上很多要修改.class .project的比较麻烦有时候还不一定管用.下面的方法适合于项目用已经引用了jar,但是运行时却ClassNotFound,请用如下方法试试: 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web

Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置

Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservletmysql 在公司一直没有什么机会直接折腾SSH“原生态”的SSH当今比较流行的轻量级的框架,用着公司的框架也是郁闷异常,今天没事整整原来用过的一个项目的配置,发现就算是自己曾经用过的东西,如果较长时间不返过去重新学习,许多你半熟不熟的知识就是异常陌生.下面贴上我的一些配置,暂且权当备份吧. web

Spring Hibernate4 整合配置文档

1 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:context="http:/

maven项目下tomcat直接启动不了(LifecycleException)。报错如下截图

经查,tomcat项目下的lib中没有jar包,发布的时候没有将jar包发布上去.这个问题在我的博客中以前遇到过.如何将maven的jar发布到项目中,我的博客里面有记载

idea的maven项目下spring与mybatis整合

两周前学习mybatis框架,参考了网上多位大神的博客,但因为各种原因(不解释)总是没法成功搭建环境并运行项目.周末花了点时间阅读了文档并整理之前琐碎的内容,解决掉之前遇到的问题.现将整合环境的关键步骤整理成学习手记一篇. 文章只提取了整合环境的主体过程,没有太深入的解析其中内容,若想深入学习mybaits,请自行阅读文档.源码,或参看网上其他大神的博客,本人菜鸟一只,只是做做学习笔记(ps:吃货请直接拖到底部). 1.导入spring核心包.spring测试包.mybatis核心包.mysql

idea下maven项目下spring junit 测试用例

使用idea在编写的类下右键Go->Test或者ctrl+shift+t,点击create new test会在相应目录下创建test类 别写代码如下 @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/config/**/applicationContext*.xml" }, loader = GenericXmlContextLoader

maven项目下jsp文件中el表达式失效问题

本来是为了写个springmvc的小demo,雏形搭建起来后想起来做成maven的好了,就重新建的maven项目,坑就从这里开始了... maven创建web项目默认使用的是web 2.3版本,web.xml文件头中也是2.3的版本..说实话之前没怎么用maven创建过项目,后来觉得maven项目看源码太方便了才更偏向于用maven的,所以这里也根本没在意..引入的javax-servlet-api 2.5的版本.然后发现jsp文件中的el表达式都原样输出了,看起来el表达式根本没解析..这就怪