maven项目下solr和spring的整合配置
2016-11-22 17:05
295 查看
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">
<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">
<!--定义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">
<!--配置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
copy
package com.millery.spring_solr.pojo;
/**
*
* @项目名称:spring-solr
* @类名称:User
* @类描述:用户实体类
* @创建人:millery
* @创建时间:2015年11月5日 上午10:42:43
* @version:
*/
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类:
[html]
view plain
copy
</pre></p><p>SpringSolr<span style="font-family:宋体;">类:</span></p><p><pre name="code" class="html">package com.millery.spring_solr.test;
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
* @类描述:测试类
* @创建人:millery
* @创建时间:2015年11月5日 上午10:48:57
* @version:
*/
@Component
public class SpringSolr {
@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类:
来自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配置文件配置完成后,可能会出现下图的报错问题,此时就需要简单的处理一下就可以了。
问题图片:
解决方法就是右击工程-->maven-->update project-->选择当前的工程-->OK,这样报错的红叉就消失了。
第一步:编写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">
<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">
<!--定义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">
<!--配置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
copy
package com.millery.spring_solr.pojo;
/**
*
* @项目名称:spring-solr
* @类名称:User
* @类描述:用户实体类
* @创建人:millery
* @创建时间:2015年11月5日 上午10:42:43
* @version:
*/
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类:
[html]
view plain
copy
</pre></p><p>SpringSolr<span style="font-family:宋体;">类:</span></p><p><pre name="code" class="html">package com.millery.spring_solr.test;
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
* @类描述:测试类
* @创建人:millery
* @创建时间:2015年11月5日 上午10:48:57
* @version:
*/
@Component
public class SpringSolr {
@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类:
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 | 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; /** * * @项目名称:spring-solr * @类名称:SpringSolrTest * @类描述:测试类 * @创建人:millery * @创建时间:2015年11月5日 上午10:56:06 * @version: */public class SpringSolrTest { private SpringSolr springSolr; @Before public void setUp() throws Exception { // 初始化Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml", "applicationContext-solr.xml"); //获取对象 this.springSolr = applicationContext.getBean(SpringSolr.class); } @Test public void test() throws SolrServerException { // 测试方法,输出结果 User user = springSolr.getUser((long) 1); System.out.println(user); } } |
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配置文件配置完成后,可能会出现下图的报错问题,此时就需要简单的处理一下就可以了。
问题图片:
解决方法就是右击工程-->maven-->update project-->选择当前的工程-->OK,这样报错的红叉就消失了。
相关文章推荐
- maven项目下solr和spring的整合配置
- eclipse 中创建 整合springmvc+mybatis+maven 的java web项目步骤,以及maven仓库配置
- (六)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven构建config配置项目
- Springboot整合dubbo构建maven多模块项目(一)- 项目创建和pom.xml中jar包配置
- eclipse 新建 maven 项目 添加 spring hibernate 的配置文件 详情
- SSH2整合项目开发中spring配置文件applicationContext.xml分析
- springmvc + spring + mybatis + maven整合配置文件
- Maven配置Spring+Hibernate Shiro权限控制项目
- Spring+SpringMVC+Mybatis整合系列(一)Maven安装与配置
- Spring+mybatis+maven整合过程加载配置文件jdbc.properties
- 标准版eclipse配置J2EE插件,使用maven创建web项目(spring JDBC)
- 项目总结--maven+springsecurity+solr+springmvc+hibernate 延迟加载处理+hibernate sql对象处理(sql执行插入)+spring注解方式+邮件 + JNDI+项目框架写法+jasperreprot的简单应用
- MongoDB整合Spring(Maven项目)
- Maven整合Spring,Spring mvc4.1.6,Hibernate4.3.10项目搭建总结
- Spring+SpringMVC+Mybatis整合系列(二)Eclipse新建Maven web项目
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- 配置Mybatis--SpringMVC-Mybatis-Maven项目整合(三)
- 控制层基本配置--SpringMVC-Mybatis-Maven项目整合(二)
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
- springmvc+mongodb+maven 项目搭建配置