@Override
@SuppressWarnings("unchecked")
public <T> List<T> getBeansOfType( ResolvableType resolvableType, boolean includeModuleInternals ) {
Set<T> beans = new LinkedHashSet<>();
ModuleBeanOrderComparator comparator = new ModuleBeanOrderComparator();
DependencyDescriptor dd = new ResolvableTypeDescriptor( resolvableType );
ResolvableTypeAutowireCandidateResolver resolver = new ResolvableTypeAutowireCandidateResolver();
AcrossListableBeanFactory beanFactory = beanFactory( contextInfo.getApplicationContext() );
resolver.setBeanFactory( beanFactory );
for ( String beanName : BeanFactoryUtils.beansOfTypeIncludingAncestors( beanFactory,
resolvableType.getRawClass() )
.keySet() ) {
if ( beanFactory.isAutowireCandidate( beanName, dd, resolver ) ) {
Object bean = beanFactory.getBean( beanName );
comparator.register( bean, Ordered.HIGHEST_PRECEDENCE );
beans.add( (T) bean );
}
}
if ( includeModuleInternals ) {
for ( AcrossModuleInfo module : contextInfo.getModules() ) {
beanFactory = beanFactory( module.getApplicationContext() );
if ( beanFactory != null ) {
resolver.setBeanFactory( beanFactory );
for ( String beanName : beanFactory.getBeansOfType( resolvableType.getRawClass() ).keySet() ) {
if ( beanFactory.isAutowireCandidate( beanName, dd, resolver ) ) {
Object bean = beanFactory.getBean( beanName );
comparator.register( bean, module.getIndex() );
beans.add( (T) bean );
}
}
}
}
}
List<T> beanList = new ArrayList<>( beans );
comparator.sort( beanList );
return beanList;
}