com.circustar.mybatis_accessor.utils.ApplicationContextUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-accessor Show documentation
Show all versions of mybatis-accessor Show documentation
enhancement based on mybatis plus
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