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

网友投稿 384 2023-01-18


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

前言:

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

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;

4.0.0

com.millery.spring_solr

spring-solr

0.0.1-SNAPSHOT

war

org.springframework

spring-context

4.1.3.RELEASE

org.springframework

spring-beans

4.1.3.RELEASE

org.springframework

spring-jdbc

4.1.3.RELEASE

<groupId>org.springframework

spring-aspects

4.1.3.RELEASE

org.apache.solr

solr-solrj

4.10.1

junit

junit

4.10

test

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;

4.0.0

com.millery.spring_solr

spring-solr

0.0.1-SNAPSHOT

war

org.springframework

spring-context

4.1.3.RELEASE

org.springframework

spring-beans

4.1.3.RELEASE

org.springframework

spring-jdbc

4.1.3.RELEASE

<groupId>org.springframework

spring-aspects

4.1.3.RELEASE

org.apache.solr

solr-solrj

4.10.1

junit

junit

4.10

test

第二步:编写applicationContext-solr.xml和solr.properties配置文件

applicationContext-solr.xml配置文件的内容:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">;

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">;

solr.properties配置文件的内容:

solr.Url=http://127.0.0.1:8983/millery

solr.maxRetries=1

solr.connectionTimeout=500

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">;

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:solr.properties

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">;

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:solr.properties

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:solr.properties

第四步:写测试代码

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.java

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);

}

}

运行代码结果:

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring MVC处理参数中的枚举类型通用实现方法
下一篇:Java 中运行字符串表达式的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~