5 Şubat 2022 Cumartesi

SpringContext ClassPathScanningCandidateComponentProvider Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
Örnek
Şöyle yaparız
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(FooAnnotation.class));

Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents("com.foo");

if (!CollectionUtils.isEmpty(beanDefinitions)) {
  for (final BeanDefinition beanDefinition : beanDefinitions) {
    Class<?> fooClass = Class.forName(beanDefinition.getBeanClassName());
    ...
  }
}

Hiç yorum yok:

Yorum Gönder