我有一个使用 Spring 4.0.4 部署在 Weblogic 12.2 上的 EAR 应用程序并面临一个奇怪的行为:
由于 Weblogic 的类加载器为 Spring's code 返回一个空枚举,Spring 无法找到基础包: cl.getResources("my/base/pkg")
它在将一些@Autowired bean 注入(inject)我的 MDB 的上下文中。 (因此,由于它,bean 没有被注入(inject))
但是如果我将下面的代码放入同一个应用程序的示例 MDB 中,它就可以工作:
Thread.currentThread().getContextClassLoader().getResources("my/base/pkg");
似乎 Spring 使用相同的代码来创建其 cl
变量。
我注意到的另一件事是:如果我将 Spring 配置为将基础包作为具体包(其中包含类),它就可以工作。
它会是什么?
最佳答案
在使用 Spring 2.5.6 将在 Weblogic 12.1 中的 java 7 上运行的应用程序升级到使用 Spring 4.1.9 的 Weblogic 12.2 中的 java 8 时,我遇到了类似的问题(以获得完整的 java 8 支持)。从技术上讲,Spring 4.1.9 包含与 2.5.6 相同的所有类(尽管已弃用)。
突然间,Spring 不会通过 CLASSPATH 上给定的文件系统上的外部目录中的上下文文件加载 bean。我们在扩展 SpringBeanAutowiringInterceptor
的拦截器中使用了以下代码:
private static final String[] CONTEXT_FILES = {"classpath*:context*.xml"};
这停止了工作。为了让它再次工作,我必须明确传递父文件夹名称(我们称之为“spring_context”)并将我们的 CLASSPATH 修改为一个文件夹。像这样:
private static final String[] CONTEXT_FILES = {"classpath*:spring_context/context*.xml"}
关于该更改的某些内容允许 spring 通过 WLS 12.2 中的 ClassLoader 获取外部上下文文件列表。我认为这与 Spring 文档中的注释有关:
WARNING: Note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like "classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK's ClassLoader.getResources() method which only returns file system locations for a passed-in empty String (indicating potential roots to search).
这似乎表明该问题仅适用于从 jar 文件加载 spring 上下文文件。但我使用 Weblogic Server 12.2 的经验表明,在文件系统上使用扩展目录时,它也可能会遇到同样的问题。
关于java - ClassLoader.getResources 在 Weblogic/Spring 应用程序上返回一个空枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751297/