首页 > 其他分享 >SpringBoot配置Bean是否生效

SpringBoot配置Bean是否生效

时间:2023-06-01 19:05:20浏览次数:40  
标签:jedisConnectionFactory SpringBoot cluster Bean RedisTemplate 生效 new redisTemplat


@Configuration
@ConditionalOnProperty(prefix = "xxl.job", name = "enable", havingValue = "true", matchIfMissing = true)
public class XxlJobConfig {
    // ...
}


上述可直接读取配置文件中的,xxl.job.enable = true


@ConditionalOnProperty(prefix = "redis", name = "cluster", havingValue = "true")

ConditionalOnProperty注解中的 cluster  havingValue 指定的值一致时生效。

其中name="cluster"中的 cluster为当前对象中的属性

当然也可以不使用 @ConditionalOnProperty 中的name。

redis.cluster=false
private static boolean cluster;

	@Value("${redis.cluster}")
	public void setCluster(boolean c) {
		cluster = c;
	}

	@Bean
	@ConditionalOnProperty(prefix = "redis", name = "cluster", havingValue = "false")
	public RedisTemplate<?, ?> redisTemplateSingle() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(host);
		jedisConnectionFactory.setPort(port);
		jedisConnectionFactory.setDatabase(dbIndex);
		if (StringUtils.isNotBlank(password)) {
			jedisConnectionFactory.setPassword(password);
		}
		jedisConnectionFactory.setTimeout(TIMEOUT);
		jedisConnectionFactory.setUsePool(USE_POOL);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
		jedisConnectionFactory.afterPropertiesSet();

		RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
		return redisTemplate;
	}

	@Bean
	@ConditionalOnProperty(prefix = "redis", name = "cluster", havingValue = "true")
	public RedisTemplate<?, ?> redisTemplateCluster() {
		RedisNode clusterRedisNodes1 = new RedisNode(host, port);
		RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
		Set<RedisNode> clusterNodes = new HashSet<>();
		clusterNodes.add(clusterRedisNodes1);
		redisClusterConfiguration.setClusterNodes(clusterNodes);

		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration,
				jedisPoolConfig());
		jedisConnectionFactory.setDatabase(dbIndex);
		if (StringUtils.isNotBlank(password)) {
			jedisConnectionFactory.setPassword(password);
		}
		jedisConnectionFactory.afterPropertiesSet();

		RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
		return redisTemplate;
	}

标签:jedisConnectionFactory,SpringBoot,cluster,Bean,RedisTemplate,生效,new,redisTemplat
From: https://blog.51cto.com/u_14671216/6397682

相关文章

  • Springboot项目中如何使用线程池
    目录1.基于 ExecutorService自定义线程池(Java5中引入的)2.基于 ThreadPoolTaskExecutor线程池的使用(Spring提供,以及监听线程池)3.自定义 ThreadPoolTaskExecutor线程池用于大数据量的导出报表、远程请求处理数据量同步等等日常项目中可以定义多个线程池,如:报表导出使用的线......
  • Springboot整合EasyPOI操作Excel文件
    配合官网阅读本文源码POI官网文档效果图:本文只做一个案列的操作:pom依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--EasyPoi导入导......
  • springboot整合shiro实现认证授权源码
    shiro-admin介绍springboot整合shiro实现前后端分离架构(swagger文档协调前端开发)源码地址:https://gitee.com/liujinxin_ark/shiro-admin软件架构架构说明springboot+shiro+mysql+swagger使用说明运行项目后访问http://localhost:8080/doc.html即可进入swagger接口文档界......
  • springBoot+下载
    在springBoot我们上次文件和其他相比,就简单不少,现在我们在SpringBoot的基本框架基础上添加上传图片。一:没有新的jar包,但是在原有的jar包中必须需要有如下jar包。<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId......
  • SpringBoot中定时任务多线程任务
    SpringBoot使用Spring自带的Schedule来实现定时任务变得非常简单和方便。在这里个大家分享下。开启缓存注解@SpringBootApplication@EnableScheduling//开启定时任务publicclassApplication{publicstaticvoidmain(String[]args){SpringApplicat......
  • SpringBoot项目中实现读写分离
    背景介绍面对日益增加的系统访问量,数据库的吞吐量面临着巨大瓶颈。对于同一时刻有大量并发读操作和较少写操作类型的应用系统来说,将数据库拆分为主库和从库,主库负责处理事务性的增删改操作,从库负责处理查询操作,能够有效的避免由数据更新导致的行锁,使得整个系统的查询性能得到极......
  • 前后端分离的架构,前端使用Vue2.6.10,后端使用SpringBoot2.0.0的ERP实现
    技术架构技术框架:SpringBoot2.0.0+Mybatis1.3.2+SLF4J1.7+Vue2.6.10+Ant-Design-Vue1.5.2+Mysql5.7+Redis运行环境:jdk8+IntelliJIDEA+maven+宝塔面板本地部署:1.小皮面板创建一个数据库,导入jsh_erp.sql文件至数据库中,该文件在后端程序的docs文件夹下。2.使用......
  • Springboot实现ENC加密jasypt-spring-boot-starter
    依赖:<!--配置文件加密--><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.4</version>&l......
  • 还在用BeanUtils拷贝对象,MapStruct才是yyds | 附源码
    前几天,远在北京的小伙伴在群里抛出了“MapStruct”的概念。对于只闻其名,未见其人的我来说,决定对其研究一番。本文我们就从MapStruct的概念出发,通过具体的代码示例来研究它的使用情况,最后与“市面上”的其它工具来做个对比!官方介绍首先我们打开MapStruct的官网地址,映入眼帘的就......
  • [SprigMVC/SpringBoot] JSON序列化专题之日期序列化问题:接口报Jackson框架错误“Inva
    0序言今日工作中遇到的一个bug。各位看官且听我娓娓道来。1问题描述请求接口时,service层返回到controller层的数据结构为List<Map<Strig,Object>>,而Map中存在一个key=date,valuetype=java.time.LocalDate的Entry,且日志报如下错误:InvalidDefinitionException:Java8date......