我对Eclipse中的一个eclipse OSGi容器中的类加载行为感到有些困惑(这是我试图让它运行的TIBCO ActiveMatrix运行时的基础),并将其归结为下面的简单示例。
在其中(TestComponent对象是在OSGi包的Activator类中创建和调用的,但我不认为这在这里很重要)我首先直接创建一个对象,只是为了仔细检查,它的类是否存在,可以实例化。这当然可以像人们期望的那样工作...... 其次,我想使用Spring(就像我原先的意图)创建同一个类的第二个对象,但是这会因ClassNotFoundException而失败。
Spring声称,它找不到那个类(即使包和类名肯定匹配),所以我甚至添加了一个.setClassLoader(...)调用来传递它刚刚成功加载的同一个类加载器同一个类,但Spring仍然无法找到该类。 任何线索,为什么会这样?我已经没想完了。我错过了什么?
后来编辑:我只是意识到,它不是... getBean(...) - 崩溃的方法调用,而是已经是ClassPathXmlApplicationContext() - 构造函数。即该对象已经在该构造函数中创建,而不仅仅是在后续的getBean(...) - 方法调用中。因此,我尝试传入类加载器是徒劳的,因为它已经到了很晚。因此,问题是:如何将上下文的类加载器传递给该构造函数(或者工厂或Spring在内部使用的任何内容来创建ClassPathXmlApplicationContext对象)?
我的例子:
我首先为通过Spring创建的类定义了一个接口:
package com.example.some_package_0;
public interface SomeInterface
{
public String getSomeString();
}
...和一个实现此接口的类:
package com.example.some_package_1;
import com.example.some_package_0.SomeInterface;
public class SomeClassA implements SomeInterface
{
private String someProperty;
public void setSomeProperty(String someProperty) {
this.someProperty = someProperty;
}
public String getSomeString() {
return this.someProperty;
}
}
我的测试程序读取
public class TestComponent
{
import com.example.some_package_0.SomeInterface;
import com.example.some_package_1.SomeClassA;
public void test() {
SomeClassA obj1 = new SomeClassA();
obj1.setSomeProperty("SomeClassA-object (directly created)");
System.out.println("@@ message=\"" + obj1.getSomeString() + "\"");
ClassPathXmlApplicationContext applicationContext;
applicationContext = new ClassPathXmlApplicationContext("/META-INF/package1_beans.xml");
applicationContext.setClassLoader(Thread.currentThread().getContextClassLoader());
SomeInterface obj2 = (SomeInterface) applicationContext.getBean("bean1");
System.out.println("@@ message=\"" + obj2.getSomeString() + "\"");
}
}
正在使用的/META-INF/package1_beans.xml读取:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:/org/springframework/beans/factory/xml/spring-beans-2.5.xsd">
<bean id="bean1" class="com.example.some_package_1.SomeClassA">
<property name="someProperty"><value>SomeClassA-object (created via Spring)</value></property>
</bean>
</beans>
例外情况如下:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.example.some_package_1.SomeClassA] for bean with name 'bean1' defined in class path resource [META-INF/package1_beans.xml]; nested exception is java.lang.ClassNotFoundException: com.example.some_package_1.SomeClassA
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1141)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:758)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:422)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.example.test_spring_example.TestComponent.testOperation(TestComponent.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.tibco.amf.platform.runtime.componentframework.internal.proxies.operation.OperationHandler.invokeMethodWithThreadContext(OperationHandler.java:667)
at com.tibco.amf.platform.runtime.componentframework.internal.proxies.operation.AsyncToSyncOperationHandler.invoke(AsyncToSyncOperationHandler.java:98)
at com.tibco.amf.platform.runtime.componentframework.internal.proxies.ProxyInvocationHandlerRegistry$ProxyInvocationContext.invoke(ProxyInvocationHandlerRegistry.java:411)
at $Proxy67.invoke(Unknown Source)
at com.tibco.amf.binding.soap.runtime.transport.http.SoapHttpInboundEndpoint.processHttpPost(SoapHttpInboundEndpoint.java:565)
at com.tibco.amf.binding.soap.runtime.transport.http.SoapHttpServer.doPost(SoapHttpServer.java:195)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)
at org.mortbay.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1213)
at com.tibco.governance.pa.amxcomponent.pep.http.HttpPepFilter.doFilter(HttpPepFilter.java:126)
at org.mortbay.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1205)
at com.tibco.amf.implementation.common.httpfilter.GenericComponentFilter.doFilter(GenericComponentFilter.java:65)
at org.mortbay.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1205)
at com.tibco.amf.hpa.tibcohost.jetty.internal.ConnectorFilter.doFilter(ConnectorFilter.java:49)
at org.mortbay.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1205)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:536)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:747)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:405)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.example.some_package_1.SomeClassA
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:211)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:385)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
... 46 more
答案 0 :(得分:3)
实际上,解决方案几乎是微不足道的! 正如我在@Robin对OSGi中的评论中已经说过的那样,当前线程的类加载器和当前上下文或类的类加载器是不一样的!
所以,我最后要做的就是将当前线程的context-classloader设置为调用对象的类加载器,就是这样!即一个人甚至不需要深入了解OSGi来获取bundle的类加载器或者使用清单或POM或其他类似的东西 - 只需告诉Spring使用“我的”自己的类'classloader然后离开你!
...
// need to set the context class loader to "my" class loader to make Spring work:
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
ClassPathXmlApplicationContext applicationContext;
applicationContext =
new ClassPathXmlApplicationContext("/META-INF/package1_beans.xml");
SomeInterface obj2 = (SomeInterface) applicationContext.getBean("bean1");
System.out.println("@@ message=\"" + obj2.getSomeString() + "\"");
...
并且 - 是的 - 之后可能应该将线程的类加载器设置回原始值,以防万一 - 所以另一个好用于try {...} finally {...}子句。 : - )
感谢大家的回复! 微米。
答案 1 :(得分:0)
我有另一个想法。
而不是手动初始化Spring上下文。尝试将其添加到您的pom.xml中(如果您使用的是Maven):
<configuration>
<instructions>
<Spring-Context>spring/*.xml</Spring-Context>
指出applContext的位置。这似乎是一个更简洁的方式来连接它。如果你没有使用Maven,你需要手动添加到Manifest,我不确定那个正确的语法