spring 使用util:list节点 提取公共的bean

本文详细介绍了一个使用Spring框架进行配置的示例,包括如何定义bean、构造注入、属性注入及集合类型的注入方式,并展示了如何配置数据源及其属性。

1. 配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- beans节点中的红色部分是我加的,否则报出“The matching wildcard is strict, but no declaration can be found for element 'util:list"错误-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">






<bean id="car" class="com.hxzy.model.Car">
<constructor-arg type="java.lang.String">
<value>宝马</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>天津</value>
</constructor-arg>
<constructor-arg type="int">
<value>200</value>
</constructor-arg>
</bean>


<bean id="car2" class="com.hxzy.model.Car">
<constructor-arg type="java.lang.String">
<value>奔驰</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>北京</value>
</constructor-arg>
<constructor-arg type="int">
<value>220</value>
</constructor-arg>
</bean>


<bean id="person2" class="com.hxzy.model.Person">
<property name="age" value="58">
</property>
<property name="name" value="老刘"></property>

以下红色部分可以使用这句话替换

<property name="cars" ref="cars">


<property name="cars">
<list>
<ref bean="car" />
<ref bean="car2" />
</list>
</property>

</bean>

<bean id="person4" class="com.hxzy.model.Person">
   <property name="age" value="48"></property>
   <property name="name" value="夏洛"></property>

<!--此处调用了外部的cars bean-->
   <property name="cars" ref="cars"></property> 
</bean> 

<!--此处即为抽出的cars集合bean-->
<util:list id="cars">
   <ref bean="car" />
   <ref bean="car2" />
</util:list>



<bean id="dataSource" class="com.hxzy.model.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>


</bean>






</beans>

<think>我们被要求使用XML配置Redisson的Spring集成方案。根据引用内容,我们通常看到的是基于Java配置类(@Configuration)的方式。但用户明确要求XML配置方式。 在Spring中,XML配置通常涉及定义Bean,我们可以通过构造Redisson的Config对象,然后创建RedissonClient Bean。 参考引用[3]中提到的程序化配置方式,我们可以将类似的配置转换为Spring XML配置。基本思路: 1. 创建一个Config对象(属于org.redisson.config.Config类)。 2. 设置Config对象的属性(例如,使用单服务器模式、集群模式等)。 3. 使用Config对象创建RedissonClient实例。 在XML中,我们可以通过<bean>标签来定义Config对象,并设置其属性。然后定义RedissonClient的bean,通过调用Redisson.create(Config)方法。 但是,注意:Redisson.create(Config)是一个静态方法,在Spring XML中可以使用工厂方法(factory-method)来创建。 示例步骤: 1. 定义Config bean。 2. 设置Config的相应属性(例如,使用单服务器模式,设置地址、密码等)。 3. 定义RedissonClient bean使用工厂方法,调用Redisson的create方法,并传入Config bean。 由于Config对象的配置较为复杂,我们可以通过嵌套<bean>标签来配置具体的服务器模式(如单服务器、集群等)。 参考引用[2]中的单服务器配置,我们可以在XML中这样配置: 假设我们配置单服务器模式: - 地址:redis://127.0.0.1:6379 - 密码:123456 XML配置可能如下: ```xml <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"> <!-- 配置Redisson的Config bean --> <bean id="redissonConfig" class="org.redisson.config.Config"> <!-- 设置单服务器模式 --> <property name="singleServerConfig"> <bean class="org.redisson.config.SingleServerConfig"> <property name="address" value="redis://127.0.0.1:6379" /> <property name="password" value="123456" /> <!-- 可以设置其他属性,如超时等 --> <property name="timeout" value="3000" /> <property name="retryAttempts" value="3" /> <property name="retryInterval" value="1500" /> </bean> </property> <!-- 还可以设置其他全局配置,如传输模式、线程数等 --> <property name="transportMode" value="NIO" /> </bean> <!-- 通过工厂方法创建RedissonClient --> <bean id="redissonClient" class="org.redisson.Redisson" factory-method="create" destroy-method="shutdown"> <constructor-arg ref="redissonConfig" /> </bean> </beans> ``` 注意:以上配置中,我们使用了Redisson类的create静态方法,并传递了redissonConfig作为参数。同时,我们指定了销毁方法为shutdown(与引用[3]中的@Bean(destroyMethod="shutdown")对应)。 如果使用集群模式,可以配置ClusterServersConfig,例如(参考引用[3]中的集群配置片段): ```xml <bean id="redissonConfig" class="org.redisson.config.Config"> <property name="transportMode" value="EPOLL" /> <property name="clusterServersConfig"> <bean class="org.redisson.config.ClusterServersConfig"> <property name="nodeAddresses"> <list> <value>redis://127.0.0.1:7181</value> <!-- 可以添加更多节点 --> </list> </property> </bean> </property> </bean> ``` 但是,请注意,引用[3]中使用了useClusterServers()方法,在Config对象中,我们可以通过设置clusterServersConfig属性来实现。 另外,引用[4]展示了通过配置文件(如YAML)来配置,但在XML中我们无法直接使用YAML,所以需要将配置转化为Bean的属性设置。 因此,我们通过XML配置RedissonClient的关键是构建一个Config Bean,然后通过Redisson的静态工厂方法创建。 如果配置非常复杂,也可以考虑使用Spring的<util:properties>和属性占位符,但这里我们直接使用硬编码值演示。 最后,确保在项目中包含Redisson的依赖。 注意:上面的XML配置中,我们使用了destroy-method="shutdown",这是因为RedissonClient需要关闭以释放资源。 总结:Spring使用XML配置Redisson的方法主要是定义Config Bean和RedissonClient Bean,其中Config Bean根据具体模式(单机、集群等)进行配置,然后使用Redisson的create方法创建客户端。 由于Redisson的配置选项非常多,这里只给出了基本示例,实际使用中请根据需求配置更多参数。 </think> ### Spring使用XML配置Redisson的方法 在Spring框架中,可以通过XML配置方式集成Redisson客户端。以下是完整的配置方案及说明: #### 核心XML配置示例 ```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"> <!-- 1. 配置Redisson的Config对象 --> <bean id="redissonConfig" class="org.redisson.config.Config"> <property name="transportMode" value="NIO"/> <!-- 传输模式:NIO/EPOLL --> <!-- 单服务器模式配置 --> <property name="singleServerConfig"> <bean class="org.redisson.config.SingleServerConfig"> <property name="address" value="redis://127.0.0.1:6379"/> <property name="password" value="your_password"/> <property name="timeout" value="3000"/> <!-- 超时时间(ms) --> <property name="retryAttempts" value="3"/> <!-- 重试次数 --> <property name="retryInterval" value="1500"/> <!-- 重试间隔 --> </bean> </property> <!-- 集群模式示例 (与单服务器模式二选一) <property name="clusterServersConfig"> <bean class="org.redisson.config.ClusterServersConfig"> <property name="nodeAddresses"> <list> <value>redis://192.168.1.101:6379</value> <value>redis://192.168.1.102:6379</value> </list> </property> </bean> </property> --> </bean> <!-- 2. 创建RedissonClient实例 --> <bean id="redissonClient" class="org.redisson.Redisson" factory-method="create" destroy-method="shutdown"> <constructor-arg ref="redissonConfig"/> </bean> </beans> ``` #### 关键配置说明 1. **传输模式** - `TransportMode`: 指定网络传输层实现,可选`NIO`(默认)或`EPOLL`(需Linux环境)[^3] 2. **服务器模式** - **单机模式**:`SingleServerConfig` - `address`: Redis连接地址,格式:`redis://host:port` - `password`: 认证密码(若无密码可省略) - **集群模式**:`ClusterServersConfig` - `nodeAddresses`: 集群节点地址列表 3. **连接参数** - `timeout`: 命令等待超时(毫秒) - `retryAttempts`: 命令重试次数 - `retryInterval`: 重试间隔时间(毫秒) 4. **生命周期管理** - `destroy-method="shutdown"`: 确保Spring容器关闭时释放Redisson资源[^3] #### 使用外部属性文件 可通过`PropertyPlaceholderConfigurer`结合外部配置: ```xml <!-- 加载properties文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:redis.properties"/> </bean> <!-- 在配置中引用 --> <property name="address" value="${redis.host}:${redis.port}"/> <property name="password" value="${redis.password}"/> ``` #### 在代码中注入使用 ```java public class RedisService { private RedissonClient redissonClient; // 通过setter注入 public void setRedissonClient(RedissonClient redissonClient) { this.redissonClient = redissonClient; } public void demoMethod() { RBucket<String> bucket = redissonClient.getBucket("testKey"); bucket.set("Hello Redisson"); } } ``` #### 注意事项 1. **依赖要求**:确保项目包含Redisson依赖 ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.17.7</version> </dependency> ``` 2. **模式选择**:单机/集群/哨兵等模式需根据实际部署调整 3. **SSL支持**:启用SSL连接时使用`rediss://`协议前缀 4. **高级配置**:线程池、编解码器等可通过`Config`对象扩展[^4]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值