我有一个使用weld的第三方库,特别是weld-se-2.4.5。我想在使用 eclipse 插件框架的不同第三方应用程序的插件中使用这个库。我一生都无法让 Weld 成功初始化。
我无法分享任何一个真实的应用程序,但我制作了一个最小的示例插件,但由于我希望是同样的原因,我也无法使用该插件。
Activator.java:
package minimal_cdi;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "minimal-cdi"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
private static WeldContainer container;
/**
* The constructor
*/
public Activator()
{
}
@Override
public void start(BundleContext context) throws Exception
{
super.start(context);
plugin = this;
try
{
Weld weld = new Weld().property("org.jboss.weld.se.shutdownHook", false);
container = weld.initialize();
container.select(MainClass.class).get().start();
Runtime.getRuntime().addShutdownHook( new Thread( () -> {
container.select(MainClass.class).get().shutdown();
container.shutdown();
}));
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
}
主要课程:
package minimal_cdi;
import javax.inject.Inject;
public class MainClass
{
@Inject
ThingDoer doer;
MainClass()
{
}
MainClass(ThingDoer doerIn)
{
doer = doerIn;
}
void start()
{
doer.doThing();
}
void shutdown()
{
doer.stop();
}
}
注入接口:
package minimal_cdi;
public interface ThingDoer
{
abstract public void doThing();
abstract public void stop();
}
接口实现:
package minimal_cdi;
public class BeanieBoo implements ThingDoer
{
@Override
public void doThing()
{
System.out.println("Hello World");
}
public void stop()
{
System.out.println("Goodbye");
}
}
beans.xml
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
我导出插件并通过 OSGI 控制台运行它并获得以下输出:
Sept 24, 2024 10:57:47 AM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.4.5 (Final)
Sept 24, 2024 10:57:47 AM org.jboss.weld.environment.deployment.discovery.DefaultBeanArchiveScanner getBeanArchiveReference
WARN: Unable to adapt URL: bundleresource://892.fwk1603300106:1/META-INF/beans.xml, using its external form instead
Sept 24, 2024 10:57:47 AM org.jboss.weld.environment.deployment.discovery.AbstractDiscoveryStrategy performDiscovery
WARN: WELD-ENV-000031: The bean archive reference bundleresource://892.fwk1603300106:1/META-INF/beans.xml cannot be handled by any BeanArchiveHandler: [org.jboss.weld.environment.deployment.discovery.FileSystemBeanArchiveHandler@ea1ce9b]
java.lang.IllegalStateException: WELD-ENV-002009: Weld SE container cannot be initialized - no bean archives found
警告:WELD-ENV-000031:bean 归档参考包资源://892.fwk1603300106:1/META-INF/beans.xml 无法由任何 BeanArchiveHandler 处理:[org.jboss.weld.environment.deployment.discovery。 FileSystemBeanArchiveHandler@ea1ce9b]
BeanArchiveHandler
的实现,它知道如何处理您传递给它的资源。我认为 bundleresource://892.fwk1603300106:1/META-INF/beans.xml
格式是 Eclipse 特定的,因此 Weld 拒绝它,不知道如何解析其内容。