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

com.taotao.boot.sms.qiniu.QiNiuAutoConfiguration Maven / Gradle / Ivy

The 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.boot.sms.qiniu;

import com.taotao.boot.common.constant.StarterName;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.sms.common.condition.ConditionalOnSmsEnabled;
import com.taotao.boot.sms.common.configuration.SmsAutoConfiguration;
import com.taotao.boot.sms.common.loadbalancer.SmsSenderLoadBalancer;
import com.taotao.boot.sms.common.properties.SmsProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 七牛云发送端点自动配置
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:52:05
 */
@AutoConfiguration(after = SmsAutoConfiguration.class)
@ConditionalOnSmsEnabled
@ConditionalOnProperty(prefix = SmsProperties.PREFIX, name = "type", havingValue = "qiniu")
@EnableConfigurationProperties(QiNiuProperties.class)
public class QiNiuAutoConfiguration implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        LogUtils.started(QiNiuAutoConfiguration.class, StarterName.SMS_QINIU_STARTER);
    }

    /**
     * 构造七牛云发送处理
     *
     * @param properties 配置对象
     * @param loadbalancer 负载均衡器
     * @param eventPublisher spring应用事件发布器
     * @return 七牛云发送处理
     */
    @Bean
    @ConditionalOnBean(SmsSenderLoadBalancer.class)
    public QiNiuSendHandler qiNiuSendHandler(
            QiNiuProperties properties, SmsSenderLoadBalancer loadbalancer, ApplicationEventPublisher eventPublisher) {
        QiNiuSendHandler handler = new QiNiuSendHandler(properties, eventPublisher);
        loadbalancer.addTarget(handler, true);
        loadbalancer.setWeight(handler, properties.getWeight());
        return handler;
    }

    public static class QiNiuSendHandlerCondition implements Condition {

        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            Boolean enable = context.getEnvironment().getProperty(QiNiuProperties.PREFIX + ".enable", Boolean.class);
            return enable == null || enable;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy