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

com.circustar.mybatis_accessor.utils.ApplicationContextUtils Maven / Gradle / Ivy

The newest version!
package com.circustar.mybatis_accessor.utils;

import com.circustar.common_utils.reflection.ClassUtils;
import org.springframework.context.ApplicationContext;

import java.util.Collection;
import java.util.Optional;

public abstract class ApplicationContextUtils {
    public static  T getBeanOrCreate(ApplicationContext applicationContext, Class clazz) {
        final Collection beanList = applicationContext.getBeansOfType(clazz).values();
        Optional anyMatch = beanList.stream().filter(x -> x.getClass().equals(clazz)).findAny();
        if(!anyMatch.isPresent()) {
            anyMatch = beanList.stream().filter(x -> x.getClass().getName()
                    .startsWith(clazz.getName() + "$")).findAny();
        }
        if(anyMatch.isPresent()) {
            return anyMatch.get();
        }
        return ClassUtils.createInstance(clazz);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy