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

io.vanillabp.springboot.adapter.SpringBeanUtil Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package io.vanillabp.springboot.adapter;

import io.vanillabp.spi.service.WorkflowService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;

import java.util.Map;

public class SpringBeanUtil {

    private final ApplicationContext applicationContext;

    private Map workflowAnnotatedBeansCache;

    private boolean cached = true;

    public SpringBeanUtil(
            final ApplicationContext applicationContext) {

        this.applicationContext = applicationContext;

    }

    public Map getWorkflowAnnotatedBeans() {

        synchronized (this) {
            if (workflowAnnotatedBeansCache != null) {
                return workflowAnnotatedBeansCache;
            }
            final var beans = applicationContext
                    .getBeansWithAnnotation(WorkflowService.class);
            if (cached) {
                workflowAnnotatedBeansCache = beans;
            }
            return beans;
        }

    }

    @EventListener
    void onApplicationEvent(
            final ContextRefreshedEvent event) {

        synchronized (this) {
            cached = false; // only cache during startup
            workflowAnnotatedBeansCache = null;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy