Java 如何使用反射库(或其他)在SpringWeb应用程序中扫描注释
我有一个用Spring构建的web应用程序,在Jboss中运行。我试图在运行时扫描具有特定注释的类。现在我正在使用开源软件 这段代码在一个独立的Maven项目中工作,所以我知道它一般都能工作。但我认为问题在于代码是在servlet上下文中运行的。我该如何解决这个问题?如何在SpringWebApp中扫描带有注释的类?提前谢谢 开始玩游戏吧Java 如何使用反射库(或其他)在SpringWeb应用程序中扫描注释,java,spring,spring-mvc,annotations,reflections,Java,Spring,Spring Mvc,Annotations,Reflections,我有一个用Spring构建的web应用程序,在Jboss中运行。我试图在运行时扫描具有特定注释的类。现在我正在使用开源软件 这段代码在一个独立的Maven项目中工作,所以我知道它一般都能工作。但我认为问题在于代码是在servlet上下文中运行的。我该如何解决这个问题?如何在SpringWebApp中扫描带有注释的类?提前谢谢 开始玩游戏吧 public class Scanner { private static final Log log = LogFactory.getLog(Sc
public class Scanner {
private static final Log log = LogFactory.getLog(Scanner.class);
public static void main(String[] args) throws IOException {
SimpleMetadataReaderFactory metadataFactory = new SimpleMetadataReaderFactory();
ResourcePatternResolver scaner = new PathMatchingResourcePatternResolver();
Resource[] resources = scaner.getResources("classpath*:/base.package/**/*.class");
for (Resource r : resources) {
log.info("Scanning [" + r.getDescription() + "]");
MetadataReader metadataReader = metadataFactory.getMetadataReader(r);
if (metadataReader.getAnnotationMetadata().isAnnotated("org.example.Annotation"))
log.info("found annotation in [" + r.getDescription() + "]");
}
}
这很有效
public static List<Class> scanForClassesWithAnnotation2(Class<? extends Annotation> annotationClass)
{
List<Class> classesWithAnnotation = new ArrayList<>();
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(annotationClass));
for (BeanDefinition bd : scanner.findCandidateComponents("com.example"))
{
try
{
classesWithAnnotation.add(Class.forName(bd.getBeanClassName()));
} catch (ClassNotFoundException e)
{
LOGGER.error("Could not create class from class name");
}
}
return classesWithAnnotation;
}
公共静态列表扫描带有注释的类2(类
public class Scanner {
private static final Log log = LogFactory.getLog(Scanner.class);
public static void main(String[] args) throws IOException {
SimpleMetadataReaderFactory metadataFactory = new SimpleMetadataReaderFactory();
ResourcePatternResolver scaner = new PathMatchingResourcePatternResolver();
Resource[] resources = scaner.getResources("classpath*:/base.package/**/*.class");
for (Resource r : resources) {
log.info("Scanning [" + r.getDescription() + "]");
MetadataReader metadataReader = metadataFactory.getMetadataReader(r);
if (metadataReader.getAnnotationMetadata().isAnnotated("org.example.Annotation"))
log.info("found annotation in [" + r.getDescription() + "]");
}
}
public static List<Class> scanForClassesWithAnnotation2(Class<? extends Annotation> annotationClass)
{
List<Class> classesWithAnnotation = new ArrayList<>();
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(annotationClass));
for (BeanDefinition bd : scanner.findCandidateComponents("com.example"))
{
try
{
classesWithAnnotation.add(Class.forName(bd.getBeanClassName()));
} catch (ClassNotFoundException e)
{
LOGGER.error("Could not create class from class name");
}
}
return classesWithAnnotation;
}