文章目录
项目:E:\myProject\mySpringDemo
控制反转IOC技术
面向切面编程AOP
使用Spring-4.3.18.RELEASE
一.bean的使用(结合SpringMVC更好,下面的例子只是测试bean的使用)让sprig帮我创建对象,而不用自己亲自创建
1.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
2.创建IntroduceDemo类对象
package com.test1;
//自我介绍类
public class IntroduceDemo {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//bean初始化时调用的方法
public void init(){
System.out.println("Bean初始化");
}
//bean销毁时调用的方法
public void destroy(){
System.out.println("Bean销毁");
}
public void introduceMyself(){
System.out.println("1:你好我叫"+getName()+"我的年龄是:"+getAge());
System.out.println("2:姓名"+this.name+"年龄"+this.age);
}
}
3.配置bean.xml文件
在对bean标签取id=“”时,需要使用小写开头,如果使用类的取名方式会在自动装载autowire时出现空指针错误
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--id 对应类,class中填类在src中的路径-->
<bean id="introduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<property name="name" value="章"/>
<property name="age" value="23"/>
</bean>
</beans>
4.编写测试文件
package com.test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//这是测试类
public class TestMain {
public static void main(String[] args){
//创建Spring上下文(加载bean.xml)
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取HelloWorld实例
IntroduceDemo id = ac.getBean("introduceDemo",IntroduceDemo.class);
// 单例模式
//当<bean ... scope="singleton">时,调用getBean()方法只会创建一个实例
//修改实例id的属性
id.setName("han");
id.setAge(18);
//再接收一个实例id2
IntroduceDemo id2 = ac.getBean("IntroduceDemo",IntroduceDemo.class);
//调用方法
id.introduceMyself();
id2.introduceMyself();//输出结果一致,即id,id2接收的是同一个实例
// 原型模式
//当<bean ... scope="prototype">时,调用getBean()方法会创建多个实例
//输出结果不同
//销毁实例对象
((ClassPathXmlApplicationContext) ac).close();
}
}
二.IOC注入:
定义接口
实现接口
以接口为基础注入
依赖注入方式:
基于构造函数(强依赖时使用)
public class ScrewDriver{
private Herder header;
public ScrewDriver(Header header){
this.header = header;
}
}
基于Setter方法(可选依赖)
public class ScrewDriver{
private Header header;
public setHeader(Header header){
this.header = header;
}
}
依赖注入(基于构造函数)
Header.java
package com.test1;
//Header 接口
public interface Header {
public void doWork();
public String getInfo();
}
StraightHeader.java
package com.test1;
import java.util.Map;
import java.util.Properties;
//一字刀头,StraightHeader类
public class StraightHeader implements Header {
private String color;
private int size;
public StraightHeader(String color, int size) {
this.color = color;
this.size = size;
}
public StraightHeader(Properties props) {
this.color = props.getProperty("color");
this.size = Integer.valueOf(props.getProperty("size"));
}
public StraightHeader(Map<String,String> map) {
//参数是集合类型的注入构造函数
this.color = map.get("color");
this.size = Integer.valueOf(map.get("size"));
//this.size = Integer.parseInt(map.get("size"));
}
@Override
public void doWork() {
System.out.println("Do work with straight header!!");
}
@Override
public String getInfo() {
return "This is straight header color :"+this.color+",size:"+this.size;
}
}
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="IntroduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<property name="name" value="章"/>
<property name="age" value="23"/>
</bean>-->
<bean id="header" class="com.test1.StraightHeader">
<!--注入构造依赖方式一-->
<!-- <constructor-arg index="0" value="red"></constructor-arg>
<constructor-arg index="1" value="23"></constructor-arg>-->
<!--注入构造依赖方式二-->
<!-- <constructor-arg type="java.lang.String" value="green"></constructor-arg>
<constructor-arg value="24" type="int"></constructor-arg>-->
<!--注入构造依赖方式三-->
<!-- <constructor-arg name="color" value="green"></constructor-arg>
<constructor-arg name="size" value="24"></constructor-arg>-->
<!--构造函数参数是map,properties类型-->
<constructor-arg>
<props>
<prop key="color">red</prop>
<prop key="size">43</prop>
</props>
<!-- <map>
<entry key="color" value="green"></entry>
<entry key="size" value="32"></entry>
</map>-->
<!-- <list>
<value>red</value>
</list>-->
</constructor-arg>
</bean>
</beans>
TestHeader.java
package com.test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHeader {
public static void main(String[] args){
//创建Spring上下文(加载bean.xml)
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取HelloWorld实例
Header header = ac.getBean("header",StraightHeader.class);
header.doWork();
System.out.println(header.getInfo());
//销毁实例对象
((ClassPathXmlApplicationContext) ac).close();
}
}
通过配置文件来注入构造函数依赖:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="IntroduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<property name="name" value="章"/>
<property name="age" value="23"/>
</bean>-->
<bean id="header" class="com.test1.StraightHeader">
<!--通过配置文件headerProperties来注入依赖-->
<constructor-arg name="color" value="${color}"></constructor-arg>
<constructor-arg name="size" value="${size}"></constructor-arg>
</bean>
<!--也可以通过更简单的方式配置-->
<!--<context:property-placeholder location="header.properties">
-->
<!--配置properties文件的位置-->
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
bean对象之间的调用
<bean id="screwDriver" class="com.test1.ScrewDriver" >
<constructor-arg>
<ref bean="header"></ref>
</constructor-arg>
</bean>
ScrewDriver.java文件:
package com.test1;
//定义一个调用Header类的类
public class ScrewDriver {
private Header header;
//构造
public ScrewDriver(Header header) {
this.header = header;
}
//自定义方法,使用header
public void useHeader(){
System.out.println("headerInfo:"+header.getInfo());
header.doWork();
}
}
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="IntroduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<property name="name" value="章"/>
<property name="age" value="23"/>
</bean>-->
<bean id="header" class="com.test1.StraightHeader">
<!--通过配置文件headerProperties来注入依赖-->
<constructor-arg name="color" value="${color}"></constructor-arg>
<constructor-arg name="size" value="${size}"></constructor-arg>
<!--配置properties文件的位置-->
</bean>
<!-- 配置ScrewDriver的bean标签,使之可以调用Header类的bean-->
<bean id="screwDriver" class="com.test1.ScrewDriver" >
<constructor-arg>
<ref bean="header"></ref>
</constructor-arg>
</bean>
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
Test文件:
package com.test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHeader {
public static void main(String[] args){
//创建Spring上下文(加载bean.xml)
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取HelloWorld实例
Header header = ac.getBean("header",StraightHeader.class);
//header.doWork();
System.out.println(header.getInfo());
ScrewDriver screwDriver = ac.getBean("screwDriver",ScrewDriver.class);
screwDriver.useHeader();
//销毁实例对象
((ClassPathXmlApplicationContext) ac).close();
}
}
通过set方法注入依赖
<property name="color" value="${color}"></property>
<property name="size" value="${size}"></property>
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="IntroduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<property name="name" value="章"/>
<property name="age" value="23"/>
</bean>-->
<bean id="header" class="com.test1.StraightHeader">
<!--通过配置文件headerProperties来注入依赖,通过set方法注入依赖-->
<property name="color" value="${color}"></property>
<property name="size" value="${size}"></property>
</bean>
<bean id="screwDriver" class="com.test1.ScrewDriver" >
<constructor-arg>
<ref bean="Header"></ref>
</constructor-arg>
</bean>
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
StraightHeader.java
package com.test1;
import java.util.Map;
import java.util.Properties;
//一字刀头,StraightHeader类
public class StraightHeader implements Header {
private String color;
private int size;
//创建set方法,注入依赖
public void setColor(String color){
this.color = color;
}
public void setSize(int size){
this.size = size;
}
@Override
public void doWork() {
System.out.println("Do work with straight header!!");
}
@Override
public String getInfo() {
return "This is straight header color :"+this.color+",size:"+this.size;
}
}
自动装配autowire
根据set方法注入依赖,用在调用其他类的bean情况下。
1.byName,根据Bean名称
2.byType,根据Bean类型
3.constructor,构造函数,根据类型
byName示例:
<!--自动装配-->
<bean id="screwDriver" class="com.test1.ScrewDriver" autowire="byName"></bean>
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="IntroduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<property name="name" value="章"/>
<property name="age" value="23"/>
</bean>-->
<bean id="header" class="com.test1.StraightHeader">
<!--通过配置文件headerProperties来注入依赖,通过set方法注入依赖-->
<property name="color" value="${color}"></property>
<property name="size" value="${size}"></property>
</bean>
<!--通过构造注入依赖,并调用其他bean-->
<!-- <bean id="ScrewDriver" class="com.test1.ScrewDriver" >
<constructor-arg>
<ref bean="Header"></ref>
</constructor-arg>
</bean>-->
<!--自动装配-->
<bean id="screwDriver" class="com.test1.ScrewDriver" autowire="byName"></bean>
<!--配置properties文件的位置-->
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
ScrewDriver.java
package com.test1;
//定义一个调用Header类的类
public class ScrewDriver {
private Header header;
//自动装配
public void setHeader(Header header){
this.header = header;
}
//自定义方法,使用header
public void useHeader(){
System.out.println("headerInfo:"+header.getInfo());
header.doWork();
}
}
test文件
package com.test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHeader {
public static void main(String[] args){
//创建Spring上下文(加载bean.xml)
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取HelloWorld实例
Header header = ac.getBean("header",StraightHeader.class);
//header.doWork();
System.out.println(header.getInfo());
ScrewDriver screwDriver = ac.getBean("screwDriver",ScrewDriver.class);
screwDriver.useHeader();
//销毁实例对象
((ClassPathXmlApplicationContext) ac).close();
}
}
annotation
@Component:定义Bean
@Value:properties注入
@Autowired & @Resource:自动装配依赖
@PostConstruct & @PreDestroy:生命周期回调
使用annotation需要在bean.xml(application-context.xml)文件中添加如下配置:
<context:component-scan base-package="com.test1"/>
使用@Component和@Value
StraightHeader.java
package com.test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Properties;
//一字刀头,StraightHeader类
@Component("header")//如果不加别名,spring自动将此类的bean命名为straightHeader而不是header,后面程序调用header的地方将出错。
public class StraightHeader implements Header {
@Value("${color}")
private String color;
@Value("${size}")
private int size;
@Override
public void doWork() {
System.out.println("Do work with straight header!!");
}
@Override
public String getInfo() {
return "This is straight header color :"+this.color+",size:"+this.size;
}
}
bean.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://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用注解annotation,base-package中填要扫描的包,会扫描该包下所有文件中的annotation-->
<context:component-scan base-package="com.test1"/>
<!--自动装配-->
<bean id="screwDriver" class="com.test1.ScrewDriver" autowire="byName"></bean>
<!--配置properties文件的位置-->
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
使用@autowired:
<!--使用annotation中的autowired-->
<bean id="screwDriver" class="com.test1.ScrewDriver" ></bean>
@Autowired
private Header header;
bean.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://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用注解annotation-->
<context:component-scan base-package="com.test1"/>
<!--自动装配-->
<!--<bean id="screwDriver" class="com.test1.ScrewDriver" autowire="byName"></bean>-->
<!--使用annotation中的autowired-->
<bean id="screwDriver" class="com.test1.ScrewDriver" ></bean>
<!--配置properties文件的位置-->
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
ScrewDriver.java
package com.test1;
import org.springframework.beans.factory.annotation.Autowired;
//定义一个调用Header类的类
public class ScrewDriver {
@Autowired//或者@Resource//更加标准位于javax.annottion包中
private Header header;
//自定义方法,使用header
public void useHeader(){
System.out.println("headerInfo:"+header.getInfo());
header.doWork();
}
}
使用@PostConstruct & @PreDestroy
使用注释完成初始化和销毁方法的ioc
@Value("${name}")
private String name;
@Value("${age}")
private int age;
//bean初始化时调用的方法
@PostConstruct
public void init(){
System.out.println("Bean初始化");
}
//bean销毁时调用的方法
@PreDestroy
public void destroy(){
System.out.println("Bean销毁");
}
bean.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://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用注解annotation-->
<context:component-scan base-package="com.test1"/>
<!-- <bean id="IntroduceDemo" class="com.test1.IntroduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
<!--自动装配-->
<!--<bean id="screwDriver" class="com.test1.ScrewDriver" autowire="byName"></bean>-->
<!--使用annotation中的autowired-->
<bean id="screwDriver" class="com.test1.ScrewDriver" ></bean>
<!--配置properties文件的位置-->
<bean id="headerProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:header.properties"/>
</bean>
</beans>
IntroduceDemo.java
package com.test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//自我介绍类
@Component
public class IntroduceDemo {
@Value("${name}")
private String name;
@Value("${age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//bean初始化时调用的方法
@PostConstruct
public void init(){
System.out.println("Bean初始化");
}
//bean销毁时调用的方法
@PreDestroy
public void destroy(){
System.out.println("Bean销毁");
}
public void introduceMyself(){
System.out.println("1:你好我叫"+getName()+"我的年龄是:"+getAge());
System.out.println("2:姓名"+this.name+"年龄"+this.age);
}
}
spring容器中配置带泛型的bean
http://blog.icoolxue.com/spring-configuration-with-generic-bean/
希望在Spring容器中配置一个带泛型的Bean,直接配置如下:
<bean id="list" class="java.util.ArrayList<java.lang.String>"/>
启动容器时,将报如下所示:
Caused by: org.xml.sax.SAXParseException: The value of attribute "class" associated with an element type "null" must not contain the '<' character. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1427) at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:945)
这是因为“<”或“>”的字符是XML的特殊字符,它会破坏Spring XML配置文件的格式,因此产生了错误
由于Spring 3.0引入了JavaConfig,以代码的方式定义Bean,因此我们可以使用如下方式配置之:
Java
package com.ioctest; /**
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class AppConfig {
@Bean(name = "listStr")
public List<String> listStrBean(){
return new ArrayList<String>();
}
}
在XML配置文件中引用这个JavaConfig:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- <bean id="list" class="java.util.ArrayList<java.lang.String>"/>-->
<context:component-scan base-package="com.ioctest"/>
<context:annotation-config/>
</beans>
这个就可以正确启动了:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Tester {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test.xml");
List<String> listStr = (List<String>) applicationContext.getBean("listStr");
listStr.add("ddd");
}
}
使用
项目taotao-parent中使用:JavaConfig,完成spring容器配置SolrCloudClient.Builder的注入
要生成SolrCloudClient对象,必须通过Builder来生成,而Builder的无参构造在solrj8.0.0中已被弃用,所以使用Builder的第三种构造,Builder(List zkHosts,Optional zkChroot)。尝试直接使用最基本的注入方式,通过构造方法注入,配置一个list,和一个空的Optional对象,前面一个可用容易配置,但是Optional的注入无法直接通过:
<bean id = "optional" class = "java.util.Optional">
</bean>
因为Optional是带泛型的,且构造方法为私有,所以采用JavaConfig的方法注入Optional的Bean。由于不清楚zkChroot的具体意义,暂时以一个空意义的Optional来代替。
api中的说明;
zkChroot - the path to the root ZooKeeper node containing Solr data. Provide java.util.Optional.empty() if no ZK chroot is used.
OptionalConfig.java
package com.taotao.taotaosearch.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Optional;
@Configuration
public class OptionalConfig {
@Bean(name = "zkChroot")
public Optional<String> zkChrootBean(){
return Optional.empty();
}
}
spring-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"
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置solrBuilder单机版-->
<bean id="httpSolrClientBuilder" class="org.apache.solr.client.solrj.impl.HttpSolrClient.Builder">
<constructor-arg name="baseSolrUrl" value="http://192.168.137.129:8080/solr"/>
</bean>
<!--配置cloudSolr-->
<bean id="cloudSolrClientBuilder" class="org.apache.solr.client.solrj.impl.CloudSolrClient.Builder">
<constructor-arg>
<list>
<value>192.168.137.17:2181</value>
<value>192.168.137.17:2182</value>
<value>192.168.137.17:2183</value>
</list>
</constructor-arg>
<constructor-arg ref="zkChroot"/>
</bean>
</beans>
解决完这个配置问题后,突然发现使用Builder的第二种构造方法更加方便简单,直接传入solr集群中各个solr实例的url。不用想办法配置使用zookeeper集群的构造方法。