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

shz.spring.SpringAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 2023.2.5
Show newest version
package shz.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import shz.core.NullHelp;
import shz.core.ToList;
import shz.core.cl.ClassLoaderHelp;
import shz.core.msg.ServerFailureMsg;
import shz.core.translate.SchemeHandler;
import shz.core.translate.TranslateHandler;
import shz.spring.cron.CronProperties;
import shz.spring.cron.DefaultCronDelayQueue;
import shz.spring.filter.FilterHelp;
import shz.spring.lock.LockAdvisor;
import shz.spring.session.SessionAcceptor;
import shz.spring.session.SessionFilter;
import shz.spring.session.SessionListener;
import shz.spring.translate.TranslateAdvisor;

import java.lang.annotation.Annotation;
import java.util.List;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CronProperties.class)
class SpringAutoConfiguration {
    @Bean
    LockAdvisor lockAdvisor() {
        return new LockAdvisor();
    }

    @Bean
    @ConditionalOnBean(SchemeHandler.class)
    @ConditionalOnMissingBean(TranslateHandler.class)
     TranslateHandler translateHandler(SchemeHandler schemeHandler) {
        return new TranslateHandler<>(schemeHandler);
    }

    @Bean
    @ConditionalOnBean(TranslateHandler.class)
    TranslateAdvisor translateAdvisor() {
        return new TranslateAdvisor();
    }

    ////////////////////////////cron
    @Autowired(required = false)
    CronProperties cronProperties;

    @Bean
    @ConditionalOnProperty(name = "cron.enabled", havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean(DefaultCronDelayQueue.class)
    DefaultCronDelayQueue defaultCronDelayQueue() {
        DefaultCronDelayQueue queue = new DefaultCronDelayQueue();

        if (cronProperties != null) {
            List configs = cronProperties.getConfigs();
            if (NullHelp.nonEmpty(configs)) {
                List cronEntries = ToList.explicitCollect(configs.stream().map(config -> {
                    ServerFailureMsg.requireNonBlank(config.getExpression(), "cron.configs.expression不能为空");
                    ServerFailureMsg.requireNonBlank(config.getClassName(), "cron.configs.class-name不能为空");
                    ServerFailureMsg.requireNonBlank(config.getMethodName(), "cron.configs.method-name不能为空");

                    Class cls = ClassLoaderHelp.load(config.getClassName());
                    ServerFailureMsg.requireNonNull(cls, "cron.configs.class-name:%s 不存在", config.getClassName());

                    return new DefaultCronDelayQueue.DefaultCronEntry(config.getExpression(), config.getId(), cls, config.getMethodName());
                }), configs.size());

                //启动任务
                queue.push(cronEntries);
            }
        }

        return queue;
    }

    ////////////////////////////session
    @Bean
    @ConditionalOnBean(SessionAcceptor.class)
    @ConditionalOnMissingBean(SessionListener.class)
    SessionListener sessionListener(@Value("${spring.application.name}") String appName, SessionAcceptor sessionAcceptor) {
        return new SessionListener(appName, sessionAcceptor);
    }

    @Bean
    @ConditionalOnBean(SessionListener.class)
    ServletListenerRegistrationBean sessionListenerRegistrationBean(SessionListener listener) {
        ServletListenerRegistrationBean slrb = new ServletListenerRegistrationBean<>();
        slrb.setOrder(Ordered.HIGHEST_PRECEDENCE);
        slrb.setListener(listener);
        return slrb;
    }

    @Bean
    @ConditionalOnBean(SessionListener.class)
    @ConditionalOnMissingBean(SessionFilter.class)
    SessionFilter sessionFilter() {
        return new SessionFilter();
    }

    @Bean
    @ConditionalOnBean(SessionFilter.class)
    FilterRegistrationBean sessionFilterRegistrationBean(SessionFilter filter) {
        return FilterHelp.registrationBean(filter);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy