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

dev.alangomes.springspigot.ScopePostProcessor Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
package dev.alangomes.springspigot;

import dev.alangomes.springspigot.scope.SenderContextScope;
import lombok.val;
import org.bukkit.event.Listener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import java.util.Arrays;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON;

class ScopePostProcessor implements BeanFactoryPostProcessor {

    private SenderContextScope senderContextScope;

    ScopePostProcessor(SenderContextScope senderContextScope) {
        this.senderContextScope = senderContextScope;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
        factory.registerScope("sender", senderContextScope);
        Arrays.stream(factory.getBeanDefinitionNames()).forEach(beanName -> {
            val beanDef = factory.getBeanDefinition(beanName);
            val beanType = factory.getType(beanName);
            if (beanType != null && beanType.isAssignableFrom(Listener.class)) {
                beanDef.setScope(SCOPE_SINGLETON);
            }
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy