我遇到Spring XD处理器模块类加载的问题,并想知道如何解决它:
我有一个Spring XD处理器模块,在Spring XD容器中运行时找不到某些类。由于类加载器无法找到转换xml的类,因此我遇到了jaxb xml转换和xstream xml转换的类加载问题。 jaxb类在另一个jar文件中。 xstream类在同一个(模块的)jar文件中。无需任何jaxb / xstream代码即可轻松复制该问题(见下文)。
只要下面的处理器模块代码在Spring XD容器中执行,类加载器就无法从模块的jar中找到任何类(我更改了这个帖子的包名.Foo在相同的包在同一个jar文件中。没有外部依赖。):
// This is a spring xd processor
public class TestTransformer {
private final Logger logger = LoggerFactory.getLogger(getClass());
public String process(String input) {
try {
Class clazz = Class.forName("some.package.Foo", false, Thread.currentThread().getContextClassLoader());
logger.info("Class: {}", clazz.toString()); // works in test
} catch (ClassNotFoundException e) {
// happens when deployed to spring-xd container
logger.error(e.toString(), e);
throw new RuntimeException(e.toString(), e);
}
return input;
}
}
此代码适用于测试(单元测试和Spring XD集成测试),但在作为模块部署到Spring XD(单节点)时失败。堆栈跟踪:
Caused by: java.lang.ClassNotFoundException: some.package.Foo
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:344)
at some.package.TestTransformer.process(TestTransformer.java:13)
... 88 more
我使用Spring XD 1.2.0和1.2.1验证了这种行为。值得注意的是,处理器模块在使用reactor流时没有任何类加载问题。
我做错了什么,我怎么能解决这个问题?
答案 0 :(得分:0)
It sounds like the Transformer class is referenced by a spring-integration-core class that is loaded by the main XD class loader (parent of the module class loader). So it doesn't see any classes in the module jar. I have created https://jira.spring.io/browse/XD-3472. Meanwhile, the work around is to put those classes in a separate jar and install the jar in xd/lib.