ClassLoader.getResources returning an empty Enumeration on a Weblogic/Spring application

可紊 提交于 2020-01-13 05:57:11

问题


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

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")

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

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

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

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

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

What could it be?


回答1:


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).

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"};

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"}

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

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).

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.



来源:https://stackoverflow.com/questions/36751297/classloader-getresources-returning-an-empty-enumeration-on-a-weblogic-spring-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!