All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.cloud.idempotent.enhance.redis.config.IdempotentAdapterRedisAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 2024.09
Show newest version
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.cloud.idempotent.enhance.redis.config;

import com.taotao.cloud.idempotent.enhance.core.config.properties.IdempotentCoreProperties;
import com.taotao.cloud.idempotent.enhance.core.handler.IdempotentExceptionEventHandler;
import com.taotao.cloud.idempotent.enhance.core.helper.IdempotentHelper;
import com.taotao.cloud.idempotent.enhance.core.repository.IdempotentRepository;
import com.taotao.cloud.idempotent.enhance.redis.RedisIdempotentRepositoryImpl;
import com.taotao.cloud.idempotent.enhance.redis.config.properties.IdempotentRedisAdapterProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.NonNull;

/** 基于redis做幂等自动配置 */
@Configuration
@EnableConfigurationProperties({IdempotentRedisAdapterProperties.class})
// @ConditionalOnProperties(prefix = "enhance.idempotent",
//		properties = {"enable", "adapter.redis.enable"}, values = {"true", "true"})
public class IdempotentAdapterRedisAutoConfiguration {

    /**
     * 当容器中没有名称为 idempotentStringRedisTemplate 的bean时,往容器里注入一个默认Redis数据源的StringRedisTemplate,
     * 当容器中有名称为 idempotentStringRedisTemplate 的StringRedisTemplate时便使用容器内的,方便使用者选择存放幂等数据的Redis数据源
     */
    @Bean(name = "idempotentStringRedisTemplate")
    @ConditionalOnMissingBean(name = "idempotentStringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean(name = "redisIdempotentRepository")
    @ConditionalOnMissingBean(name = "redisIdempotentRepository")
    public IdempotentRepository redisIdempotentRepository(
            @NonNull @Qualifier("idempotentStringRedisTemplate") StringRedisTemplate redisTemplate,
            @NonNull IdempotentRedisAdapterProperties properties) {
        return new RedisIdempotentRepositoryImpl(redisTemplate, properties);
    }

    @Bean
    @ConditionalOnMissingBean
    public IdempotentHelper idempotentHelper(
            @NonNull IdempotentRepository idempotentRepository,
            @NonNull IdempotentExceptionEventHandler exceptionEventHandler,
            @NonNull IdempotentCoreProperties idempotentCoreProperties) {
        return new IdempotentHelper(exceptionEventHandler, idempotentRepository, idempotentCoreProperties);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy