ClassLoader.getResources在Weblogic / Spring应用程序上返回空的枚举 [英] ClassLoader.getResources returning an empty Enumeration on a Weblogic/Spring application

查看:96
本文介绍了ClassLoader.getResources在Weblogic / Spring应用程序上返回空的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Spring 4.0.4在Weblogic 12.2上部署了EAR应用程序,并且面临着奇怪的行为:

I've an EAR application deployed on Weblogic 12.2 using Spring 4.0.4 and facing a strange behaviour:

由于Weblogic的原因,Spring无法找到基本软件包。返回 Spring的代码 cl.getResources( my / base / pkg)

Spring can´t find the base package due to Weblogic's classloader which return an empty Enumeration for Spring's code: cl.getResources("my/base/pkg")

它是在上下文中向我的MDB注入一些@Autowired bean。 (因此,由于此原因,没有注入bean)

It's under a context which injects some @Autowired beans on my MDB. (so, due to it, the beans aren´t injected)

但是,如果我将以下代码放入示例MDB中,并且在同一应用程序中,则可以正常工作:

but if I put the below code into a sample MDB into the same application, it works:

Thread.currentThread().getContextClassLoader().getResources("my/base/pkg");

似乎Spring使用相同的代码创建其 cl 变量。

and seems that Spring use the same code to create its cl variable.

我注意到的另一件事是:如果我将Spring配置为将基本程序包作为一个具体的基本程序包(包含类),则它可以工作。

Another thing I noticed is: if I configure Spring to have a base package as a concrete one (with classes into it) it works.

那会是什么?

推荐答案

将使用Spring 2.5.6的Spring 2.5.6在Weblogic 12.1中运行在Java 7上的应用程序升级到Java时,我遇到了类似的问题使用Spring 4.1.9在Weblogic 12.2中使用8(完整的Java 8支持)。从技术上讲,Spring 4.1.9包含与2.5.6相同的所有类(尽管已弃用)。

I had a similar issue when upgrading an application that ran on java 7 in Weblogic 12.1 using Spring 2.5.6 over to java 8 in Weblogic 12.2 using Spring 4.1.9 (for full java 8 support). Technically, Spring 4.1.9 contains all of the same classes as 2.5.6 (albeit deprecated).

突然,Spring不会通过上下文文件在外部加载Bean。 CLASSPATH上指定的文件系统上的目录。我们在扩展 SpringBeanAutowiringInterceptor 的拦截器中使用以下代码:

Suddenly, Spring would not load the beans via context files in an external directory on the filesystem that was given on the CLASSPATH. We were using the following in an interceptor that extends SpringBeanAutowiringInterceptor:

private static final String[] CONTEXT_FILES = {"classpath*:context*.xml"};

此操作停止了。为了使其再次正常工作,我必须显式传递父文件夹名称(我们将其称为 spring_context),然后将CLASSPATH修改为一个文件夹。像这样:

This stopped working. To get it working again, I had to pass in the parent folder name explicitly (we call it "spring_context") and modify our CLASSPATH to be one folder up. Like this:

private static final String[] CONTEXT_FILES = {"classpath*:spring_context/context*.xml"}

有关此更改的某些内容允许spring通过WLS 12.2中的ClassLoader获取外部上下文文件列表。我认为这与Spring文档中的注释有关:

Something about that change allowed spring to get the list of external context files via the ClassLoader in WLS 12.2. I assume this is related to a note found in the Spring documentation:

https://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc -api / org / springframework / core / io / support / PathMatchingResourcePatternResolver.html


警告:请注意, classpath *: ,当与Ant样式的模式结合使用时,
只能在
模式启动之前与至少一个根目录可靠地一起工作,除非实际的目标文件位于文件
系统中。这意味着类似 classpath *:*。xml的模式将不会
从jar文件的根目录中检索文件,而只会从扩展目录的
根目录中检索文件。这源于JDK
的ClassLoader.getResources()方法中的限制,该方法仅为传入的空字符串返回文件系统
的位置(表明可能的根源为
搜索)。

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的经验表明,在文件系统上使用扩展目录时,它可能也会遇到相同的问题。

This seems to say that the issue only applies to loading spring context files from a jar file. But my experience with Weblogic Server 12.2 suggests that it might have the same issue as well when using an expanded directory on the filesystem.

这篇关于ClassLoader.getResources在Weblogic / Spring应用程序上返回空的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆