Search in sources :

Example 96 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-cloud-sleuth by spring-cloud.

the class ExecutorMethodInterceptor method createThreadPoolTaskExecutorProxy.

Object createThreadPoolTaskExecutorProxy(Object bean, boolean cglibProxy, ThreadPoolTaskExecutor executor) {
    ProxyFactoryBean factory = new ProxyFactoryBean();
    factory.setProxyTargetClass(cglibProxy);
    factory.addAdvice(new ExecutorMethodInterceptor<ThreadPoolTaskExecutor>(executor, this.beanFactory) {

        @Override
        Executor executor(BeanFactory beanFactory, ThreadPoolTaskExecutor executor) {
            return new LazyTraceThreadPoolTaskExecutor(beanFactory, executor);
        }
    });
    factory.setTarget(bean);
    return factory.getObject();
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Executor(java.util.concurrent.Executor) ProxyFactoryBean(org.springframework.aop.framework.ProxyFactoryBean) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 97 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project bucket4j-spring-boot-starter by MarcGiffing.

the class Bucket4JBaseConfiguration method buildFilterConfig.

public FilterConfiguration buildFilterConfig(Bucket4JConfiguration config, CacheManager cacheManager, ExpressionParser expressionParser, BeanFactory beanFactory) {
    FilterConfiguration filterConfig = new FilterConfiguration();
    filterConfig.setUrl(config.getUrl());
    filterConfig.setOrder(config.getFilterOrder());
    filterConfig.setStrategy(config.getStrategy());
    filterConfig.setHttpResponseBody(config.getHttpResponseBody());
    ProxyManager<String> buckets = Bucket4j.extension(JCache.class).proxyManagerForCache(jCache(config.getCacheName(), cacheManager));
    config.getRateLimits().forEach(rl -> {
        ConfigurationBuilder<?> configBuilder = Bucket4j.configurationBuilder();
        for (BandWidthConfig bandWidth : rl.getBandwidths()) {
            configBuilder = configBuilder.addLimit(Bandwidth.simple(bandWidth.getCapacity(), Duration.of(bandWidth.getTime(), bandWidth.getUnit())));
        }
        ;
        final ConfigurationBuilder<?> configBuilderToUse = configBuilder;
        RateLimitCheck rlc = (servletRequest) -> {
            boolean skipRateLimit = false;
            if (rl.getSkipCondition() != null) {
                skipRateLimit = skipCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
            }
            if (rl.getExecuteCondition() != null && !skipRateLimit) {
                skipRateLimit = !executeCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
            }
            if (!skipRateLimit) {
                String key = getKeyFilter(filterConfig.getUrl(), rl, expressionParser, beanFactory).key(servletRequest);
                Bucket bucket = buckets.getProxy(key, () -> configBuilderToUse.buildConfiguration());
                ConsumptionProbe probe = bucket.tryConsumeAndReturnRemaining(1);
                return probe;
            }
            return null;
        };
        filterConfig.getRateLimitChecks().add(rlc);
    });
    return filterConfig;
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) KeyFilter(com.giffing.bucket4j.spring.boot.starter.context.KeyFilter) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) RateLimit(com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) ProxyManager(io.github.bucket4j.grid.ProxyManager) Bucket4JAutoConfigurationZuul(com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul) Condition(com.giffing.bucket4j.spring.boot.starter.context.Condition) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Duration(java.time.Duration) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Cache(javax.cache.Cache) Bucket4JAutoConfigurationServletFilter(com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter) GridBucketState(io.github.bucket4j.grid.GridBucketState) ConfigurationBuilder(io.github.bucket4j.ConfigurationBuilder) Bandwidth(io.github.bucket4j.Bandwidth) Bucket(io.github.bucket4j.Bucket) Bucket4j(io.github.bucket4j.Bucket4j) ExpressionParser(org.springframework.expression.ExpressionParser) JCache(io.github.bucket4j.grid.jcache.JCache) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) CacheManager(javax.cache.CacheManager) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) StringUtils(org.springframework.util.StringUtils) Bucket(io.github.bucket4j.Bucket) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) JCache(io.github.bucket4j.grid.jcache.JCache)

Example 98 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project bucket4j-spring-boot-starter by MarcGiffing.

the class Bucket4JBaseConfiguration method getKeyFilter.

/**
 * Creates the key filter lambda which is responsible to decide how the rate limit will be performed. The key
 * is the unique identifier like an IP address or a username.
 *
 * @param url is used to generated a unique cache key
 * @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
 * @param expressionParser is used to evaluate the expression if the filter key type is EXPRESSION.
 * @param beanFactory used to get full access to all java beans in the SpEl
 * @return should not been null. If no filter key type is matching a plain 1 is returned so that all requests uses the same key.
 */
public KeyFilter getKeyFilter(String url, RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
    switch(rateLimit.getFilterKeyType()) {
        case IP:
            return (request) -> url + "-" + request.getRemoteAddr();
        case EXPRESSION:
            String expression = rateLimit.getExpression();
            if (StringUtils.isEmpty(expression)) {
                throw new MissingKeyFilterExpressionException();
            }
            StandardEvaluationContext context = new StandardEvaluationContext();
            context.setBeanResolver(new BeanFactoryResolver(beanFactory));
            return (request) -> {
                // TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
                Expression expr = expressionParser.parseExpression(rateLimit.getExpression());
                final String value = expr.getValue(context, request, String.class);
                return url + "-" + value;
            };
    }
    return (request) -> url + "-" + "1";
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) KeyFilter(com.giffing.bucket4j.spring.boot.starter.context.KeyFilter) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) RateLimit(com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) ProxyManager(io.github.bucket4j.grid.ProxyManager) Bucket4JAutoConfigurationZuul(com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul) Condition(com.giffing.bucket4j.spring.boot.starter.context.Condition) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Duration(java.time.Duration) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Cache(javax.cache.Cache) Bucket4JAutoConfigurationServletFilter(com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter) GridBucketState(io.github.bucket4j.grid.GridBucketState) ConfigurationBuilder(io.github.bucket4j.ConfigurationBuilder) Bandwidth(io.github.bucket4j.Bandwidth) Bucket(io.github.bucket4j.Bucket) Bucket4j(io.github.bucket4j.Bucket4j) ExpressionParser(org.springframework.expression.ExpressionParser) JCache(io.github.bucket4j.grid.jcache.JCache) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) CacheManager(javax.cache.CacheManager) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) StringUtils(org.springframework.util.StringUtils) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Expression(org.springframework.expression.Expression)

Example 99 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project mule by mulesoft.

the class DefaultListableBeanFactory method setAutowireCandidateResolver.

/**
 * Set a custom autowire candidate resolver for this BeanFactory to use when deciding whether a bean definition should be
 * considered as a candidate for autowiring.
 */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
    Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
    if (autowireCandidateResolver instanceof BeanFactoryAware) {
        if (System.getSecurityManager() != null) {
            final BeanFactory target = this;
            AccessController.doPrivileged(new PrivilegedAction<Object>() {

                @Override
                public Object run() {
                    ((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(target);
                    return null;
                }
            }, getAccessControlContext());
        } else {
            ((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
        }
    }
    this.autowireCandidateResolver = autowireCandidateResolver;
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory)

Example 100 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project grails-core by grails.

the class ClosureClassIgnoringComponentScanBeanDefinitionParser method createScanner.

@Override
protected ClassPathBeanDefinitionScanner createScanner(XmlReaderContext readerContext, boolean useDefaultFilters) {
    final ClassPathBeanDefinitionScanner scanner = super.createScanner(readerContext, useDefaultFilters);
    BeanDefinitionRegistry beanDefinitionRegistry = readerContext.getRegistry();
    GrailsPluginManager pluginManager = null;
    if (beanDefinitionRegistry instanceof HierarchicalBeanFactory) {
        HierarchicalBeanFactory beanFactory = (HierarchicalBeanFactory) beanDefinitionRegistry;
        BeanFactory parent = beanFactory.getParentBeanFactory();
        if (parent != null && parent.containsBean(GrailsPluginManager.BEAN_NAME)) {
            pluginManager = parent.getBean(GrailsPluginManager.BEAN_NAME, GrailsPluginManager.class);
        }
    }
    if (pluginManager != null) {
        List<TypeFilter> typeFilters = pluginManager.getTypeFilters();
        for (TypeFilter typeFilter : typeFilters) {
            scanner.addIncludeFilter(typeFilter);
        }
    }
    return scanner;
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) GrailsPluginManager(grails.plugins.GrailsPluginManager) HierarchicalBeanFactory(org.springframework.beans.factory.HierarchicalBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) TypeFilter(org.springframework.core.type.filter.TypeFilter) HierarchicalBeanFactory(org.springframework.beans.factory.HierarchicalBeanFactory)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)121 Test (org.junit.jupiter.api.Test)30 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)25 Test (org.junit.Test)20 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)16 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)15 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutorService (java.util.concurrent.ExecutorService)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)4 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)4