简体   繁体   English

Wildfly 8.0.2中的反射库

[英]reflections library inside Wildfly 8.0.2

I am trying to scan classes with Reflections library, if I add the Dynamic Web project to another project (plain Java one), I get the classes I want, but if run inside a @Startup bean, it is empty. 我正在尝试使用Reflections库扫描类,如果将Dynamic Web项目添加到另一个项目(纯Java项目)中,则会得到所需的类,但是如果在@Startup bean中运行,则为空。 Here is the code: 这是代码:

Reflections reflections = new Reflections(
    new ConfigurationBuilder().filterInputsBy(
        new FilterBuilder.Include(
            FilterBuilder.prefix("my.package")
        )
    ).setUrls(
        ClasspathHelper.forJavaClassPath()
    ).setScanners(
        new SubTypesScanner(false)
    )
);

Set<Class<? extends Object>> testClasses = reflections.getSubTypesOf(Object.class);

The tv,goopi should be changed to whatever package prefix used. tv,goopi应该更改为使用的任何软件包前缀。 the testClasses Set is empty. testClasses Set为空。

If the same code is running in a different project referencing this one, no other change, then the Set is populated with all classes inside the package. 如果相同的代码正在引用该代码的另一个项目中运行,而没有其他更改,则将用包内的所有类填充Set

The Maven dependency for Reflections is: 对反射的Maven依赖是:

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.9-RC1</version>
</dependency>

Wildfly 8.2.0. Wildfly 8.2.0。 For now, I can save the file extracted in the external project and use the load function, but this will not be dynamic as it should be. 现在,我可以将提取的文件保存在外部项目中,并使用加载功能,但这不会是应有的动态。

I struggled with this one for quite a while, it looks like due to the way it works, without getting the full java class path (makes it very heavy) you should load reflect a little bit later. 我花了相当长的时间苦苦挣扎,由于它的工作方式,它看起来像没有完整的Java类路径(使它变得很沉重),您应该稍后再加载反射。 This is mostly due to dynamic class creation during the EJB initialization phase, Startup beans included. 这主要是由于在EJB初始化阶段创建了动态类,其中包括Startup bean。

To load the full class path (from a startup bean) urls.addAll(ClasspathHelper.forJavaClassPath()); 要加载完整的类路径(从启动Bean),请执行urls.addAll(ClasspathHelper.forJavaClassPath());

To make it cross JEE friendly, eg wild-fly, you need to reflect from a Servlet Context listener. 为了使其跨JEE友好,例如狂放,您需要从Servlet Context侦听器进行反映。 For me the right place was in the constructor but a static field may work. 对我而言,正确的位置在构造函数中,但静态字段可能有效。

public class GuiceContext implements ServletContextListener
{

@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
    ClasspathHelper.forWebInfLib(servletContextEvent.getServletContext());
  ClasspathHelper.forWebInfClasses(servletContextEvent.getServletContext());
}

Try and get your beans to initialize stand-alone without dependencies. 尝试让您的bean初始化独立的无依赖项。 You can also use a custom injector like Guice to push your beans. 您也可以使用Guice这样的自定义注入器来推动咖啡豆。 In this case you would use the GuiceServletContextListener class. 在这种情况下,您将使用GuiceServletContextListener类。

You are excluding direct exclusions of Object.class, this startup bean in this instance may not be loading due to this. 您不包括Object.class的直接排除项,因此,此实例中的该启动bean可能因此无法加载。 new SubTypesScanner(false);

A complete library with org.reflections and guice directly implemented can be found at https://github.com/GedMarc/GuiceInjection 可以在https://github.com/GedMarc/GuiceInjection中找到具有直接实现的org.reflections和guice的完整库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM