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

com.spring.boxes.gateway.GatewayAutoConfiguration Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.gateway;

import com.spring.boxes.gateway.configure.GatewayProperties;
import com.spring.boxes.gateway.exception.GatewayExceptionHandler;
import com.spring.boxes.gateway.filter.GatewayGrayGlobalFilter;
import com.spring.boxes.gateway.locator.RedisKvRouteDefinitionLocator;
import com.spring.boxes.gateway.locator.MemoryRouteDefinitionLocator;
import com.spring.boxes.gateway.refresh.GatewayRoutesRefresher;
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.cloud.gateway.route.InMemoryRouteDefinitionRepository;
import org.springframework.cloud.gateway.route.RedisRouteDefinitionRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
// @ComponentScan(value = {"org.springframework.cloud.gateway.route"})
@EnableConfigurationProperties(GatewayProperties.class)
public class GatewayAutoConfiguration {

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Bean
    @ConditionalOnMissingBean
    public GatewayRoutesRefresher gatewayRoutesRefresher() {
        return new GatewayRoutesRefresher(applicationEventPublisher);
    }

    @Bean
    @ConditionalOnMissingBean
    public GatewayExceptionHandler gatewayExceptionHandler() {
        return new GatewayExceptionHandler();
    }

    @Bean
    @ConditionalOnBean(RedisRouteDefinitionRepository.class)
    @ConditionalOnProperty(prefix = "spring.boxes.gateway.locator", value = {"redis"}, havingValue = "false")
    public RedisKvRouteDefinitionLocator cacheRouteDefinitionLocator(RedisRouteDefinitionRepository redisRouteDefinitionRepository) {
        return new RedisKvRouteDefinitionLocator(gatewayRoutesRefresher(), redisRouteDefinitionRepository);
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.boxes.gateway.locator", value = {"memory"}, havingValue = "true")
    public InMemoryRouteDefinitionRepository inMemoryRouteDefinitionRepository(){
        return new InMemoryRouteDefinitionRepository();
    }

    @Bean
    @ConditionalOnBean(InMemoryRouteDefinitionRepository.class)
    public MemoryRouteDefinitionLocator memoryRouteDefinitionLocator() {
        return new MemoryRouteDefinitionLocator(gatewayRoutesRefresher(), inMemoryRouteDefinitionRepository());
    }

    @Bean
    @ConditionalOnBean(InMemoryRouteDefinitionRepository.class)
    public GatewayGrayGlobalFilter gatewayGrayGlobalFilter(){
        return new GatewayGrayGlobalFilter(inMemoryRouteDefinitionRepository());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy