1

我想在 Apache Karaf 上运行的 OSGi 包中使用 xbean 配置创建嵌入式 ActiveMQ 代理。这段代码:

brokerService = BrokerFactory.createBroker("xbean:file:etc/config.xml");

失败,但有以下异常:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load type: org.apache.activemq.xbean.XBeanBrokerService. Reason: java.lang.ClassNotFoundException: org.apache.activemq.xbean.XBeanBrokerService not found by org.apache.xbean.spring [81]; nested exception is java.lang.ClassNotFoundException: org.apache.activemq.xbean.XBeanBrokerService not found by org.apache.xbean.spring [81]
        at org.apache.xbean.spring.context.v2c.XBeanQNameHelper.getBeanInfo(XBeanQNameHelper.java:75)
        at org.apache.xbean.spring.context.v2c.XBeanQNameHelper.coerceNamespaceAwarePropertyValues(XBeanQNameHelper.java:54)
        at org.apache.xbean.spring.context.v2c.XBeanBeanDefinitionParserDelegate.parseBeanDefinitionElement(XBeanBeanDefinitionParserDelegate.java:59)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:396)
        at org.apache.xbean.spring.context.v2c.XBeanNamespaceHandler.parseBeanFromExtensionElement(XBeanNamespaceHandler.java:224)
        at org.apache.xbean.spring.context.v2c.XBeanNamespaceHandler.parseBeanFromExtensionElement(XBeanNamespaceHandler.java:276)
        at org.apache.xbean.spring.context.v2c.XBeanNamespaceHandler.parse(XBeanNamespaceHandler.java:156)
        at org.apache.xbean.spring.context.v2.XBeanNamespaceHandler.parse(XBeanNamespaceHandler.java:49)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1338)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1328)
        at org.apache.xbean.spring.context.v2.XBeanBeanDefinitionDocumentReader.parseBeanDefinitions(XBeanBeanDefinitionDocumentReader.java:84)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
        at org.apache.xbean.spring.context.v2.XBeanXmlBeanDefinitionReader.registerBeanDefinitions(XBeanXmlBeanDefinitionReader.java:79)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
        at org.apache.xbean.spring.context.ResourceXmlApplicationContext.loadBeanDefinitions(ResourceXmlApplicationContext.java:111)
        at org.apache.xbean.spring.context.ResourceXmlApplicationContext.loadBeanDefinitions(ResourceXmlApplicationContext.java:104)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
        at org.apache.xbean.spring.context.ResourceXmlApplicationContext.<init>(ResourceXmlApplicationContext.java:64)
        at org.apache.xbean.spring.context.ResourceXmlApplicationContext.<init>(ResourceXmlApplicationContext.java:52)
        at org.apache.activemq.xbean.XBeanBrokerFactory$1.<init>(XBeanBrokerFactory.java:114)
        at org.apache.activemq.xbean.XBeanBrokerFactory.createApplicationContext(XBeanBrokerFactory.java:114)
        at org.apache.activemq.xbean.XBeanBrokerFactory.createBroker(XBeanBrokerFactory.java:70)
        at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:71)
        at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:54)
        at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:85)
        at jmstest.bundle.TestActivator.start(TestActivator.java:18)
        at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:629)
        at org.apache.felix.framework.Felix.activateBundle(Felix.java:1842)
        at org.apache.felix.framework.Felix.startBundle(Felix.java:1759)
        at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1163)
        at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:264)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: org.apache.activemq.xbean.XBeanBrokerService not found by org.apache.xbean.spring [81]
        at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:787)
        at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:71)
        at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1768)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at org.apache.xbean.spring.context.v2c.XBeanQNameHelper.loadClass(XBeanQNameHelper.java:107)
        at org.apache.xbean.spring.context.v2c.XBeanQNameHelper.getBeanInfo(XBeanQNameHelper.java:72)
        ... 36 more

所有需要的包(activemq、activemq-spring、xbean-spring)都已加载并运行,我的包将它们全部放在Import-Package.

从独立 ActiveMQ 代理加载的相同配置文件可以正常工作。

我相信这是因为 xbean 无法从它不导入的包中访问类。有没有解决的办法?

4

1 回答 1

0

自己解决了这个问题,但它看起来像一个黑客。感谢有人告诉如果可能的话如何使它正确。

这个想法是用加载当前类的类替换默认类加载器(在我的例子中是捆绑激活器):

ClassLoader c = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

brokerService = BrokerFactory.createBroker("xbean:file:etc/config.xml");
brokerService.start();

Thread.currentThread().setContextClassLoader(c);

XBean 在加载一个类时会尝试几个类加载器,包括线程上下文类加载器。当然,getClass().getClassLoader()加载器知道我的包的导入,因此 XBean 设法加载所有需要的类(假设它们在Import-Package标题中)。也许这对其他人也有帮助。

于 2012-05-12T05:48:49.963 回答