Spring 配置文件标签——util:list使用方法

首先要将spring的beans标签属性进行配置;

在下面中加粗的必须加入才能进行util类标签的使用,否则会报错

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task" xmlns="http://www.springframework.org/schema/beans"
    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.xsd
">

      <util:list id="abc">
        <value>a</value>
        <value>b</value>
        <value>c</value>
    </util:list>

    <bean id="FFFFFF" class="com.F" >
        <property name="abcs" ref="abc" />
    </bean>

</beans>

接着,在类中如何获取到util:list中的值

public class F {

    private List<String> abcs;

    public void exe(){
        System.out.println(abcs);
        //打印出来是a,b,c
    }
    
    public List<String> getAbcs() {
		return abcs;
	}

	public void setAbcs(List<String> abcs) {
		this.abcs= abcs;
	}

}

变量名字仅仅只是为了举例子,希望大家还是要遵循标准的类名、变量等名称的命名规范。

Spring Boot项目中配置主从模式的Redis,通常需要借助于Lettuce或Jedis作为客户端工具包,并通过编写自定义配置类来完成。下面详细介绍如何实现这一需求。 ### 步骤一:添加依赖 首先确保pom.xml里包含必要的starter组件: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 可选 --> <dependency> <groupId>io.lettuce.core</groupId> <artifactId>lettuce-core</artifactId> </dependency> ``` ### 步骤二:编辑application.yml文件 这里给出一个多节点Redis集群的例子,其中部分充当Master角色负责写操作而另一些则是Slave只读副本用于负载均衡查询压力分散。 ```yaml spring: redis: lettuce: pool: max-active: 8 # 控制活动连接的最大数量 max-idle: 8 # 设置空闲连接的数量上限 min-idle: 0 # 初始化时打开最少连接数 hosts: # 定义所有的 Redis 地址列表 - host: master1.example.com port: 6379 timeout: PT2S password: your_password database: 0 - host: slave1a.example.com port: 6380 readonly: true - host: slave1b.example.com port: 6381 readonly: true ``` 注释说明:上述例子使用Spring Data Redis支持的新特性——即允许直接声明多组Host信息而非单一URI形式。 ### 步骤三:创建Configuration Bean 由于目前官方提供的自动装配并不完全满足复杂的场景需求所以我们手动介入生成ConnectionFactory实例过程如下所示。 ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import java.time.Duration; import java.util.List; @Configuration public class RedisConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { List<String> nodes = Arrays.asList( "master1.example.com:6379", "slave1a.example.com:6380", "slave1b.example.com:6381"); // 构建 Cluster Configuration 对象 RedisClusterConfiguration clusterCfg = new RedisClusterConfiguration(nodes); // 配置额外选项如超时期限等等... LettuceClientConfiguration clientCfg = LettuceClientConfiguration.builder() .commandTimeout(Duration.ofSeconds(2)) .useSSL() // 如果启用了 SSL 加密传输则开启此开关 .build(); return new LettuceConnectionFactory(clusterCfg, clientCfg); } } ``` 最后重启应用程序即可生效!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ijiran

一杯咖啡太贵,一块糖就可以

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值