Code to read classes that use a certain annotation:
Happy coding 👨💻
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.core.type.filter.AnnotationTypeFilter;ClassPathScanningCandidateComponentProvider classPathScanningCandidateComponentProvider = new ClassPathScanningCandidateComponentProvider(false); classPathScanningCandidateComponentProvider.addIncludeFilter(new AnnotationTypeFilter(CustomAnnotation.class)); Set < BeanDefinition > beanDefinitions = classPathScanningCandidateComponentProvider.findCandidateComponents("com.rake.demo.beans"); for (BeanDefinition beanDefinition: beanDefinitions) { System.out.println(beanDefinition.getBeanClassName()); }
Annotation:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @interface CustomAnnotation { }
Usage:
@CustomAnnotation public class MyCustomClass { }
Happy coding 👨💻
Comments
Post a Comment