3

我有一个结构如下的 EJB 应用程序。所有依赖项都保存在/lib目录中并在所有 Web 模块之间共享。

├── app1_war
│   ├── index.jsp
│   ├── META-INF
│   └── WEB-INF
|
├── core.jar
├── app2_war
│   ├── META-INF
│   └── WEB-INF
|
├── app3_war
│   ├── META-INF
│   └── WEB-INF
|
├── lib
│   ├── struts2-core-2.3.8.jar
│   ├── webwork-2.2.7.jar
│   ├── xwork-1.2.3.jar
│   └── xwork-core-2.3.8.jar
|
├── META-INF
│   ├── application.xml
│   ├── MANIFEST.MF
|
├── app4_war
    ├── index.jsp
    ├── META-INF
    └── WEB-INF

所有模块都使用struts2-core-2.3.8.jar依赖项。当我在 Glassfish 上部署它时,我最终会遇到以下异常:

Exception starting filter struts2
java.lang.InstantiationException
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:124)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4685)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:5377)
        at com.sun.enterprise.web.WebModule.start(WebModule.java:498)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
        at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2019)
        at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669)
[...]

Caused by: Unable to load configuration. - bean - jar:file:/opt/glassfish3/nodes/apps/fish1/applications/legacy/lib/struts2-core-2.3.8.jar!/struts-default.xml:29:72
        at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:483)
        at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
        at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:264)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:120)
        ... 41 more
Caused by: Unable to load configuration. - bean - jar:file:/opt/glassfish3/nodes/apps/fish1/applications/legacy/lib/struts2-core-2.3.8.jar!/struts-default.xml:29:72
        at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)
        at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:429)
        at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:471)
        ... 45 more
Caused by: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/opt/glassfish3/nodes/apps/fish1/applications/legacy/lib/struts2-core-2.3.8.jar!/struts-default.xml:29:72
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:245)
        at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:102)
        at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:215)
        at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
        ... 47 more
Caused by: Bean type class com.opensymphony.xwork2.ObjectFactory with the name xwork has already been loaded by bean - jar:file:/opt/glassfish3/nodes/apps/fish1/applications/legacy/lib/struts2-core-2.3.8.jar!/struts-default.xml:29:72 - bean - jar:file:/opt/glassfish3/nodes/apps/fish1/applications/legacy/lib/struts2-core-2.3.8.jar!/struts-default.xml:29:72
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:229)

我的猜测是,这是因为所有模块都共享相同的依赖项,因此每个模块都struts-default.xml在其上下文中加载相同的文件。有谁知道如何解决这个问题?

4

1 回答 1

0

这是一个迟到的回应,但无论如何,这是我对这个问题的看法:

我在 EAR 文件中“共享”一些依赖项时遇到了类似的问题。我可以告诉你的是,当加载上下文时,Struts 会在第一个 WAR 类路径中初始化(依次使用 objectfactory,通常是 Spring)。然后,当加载其他战争时,它们具有不同的上下文但它们包含在同一个类路径层次结构中,Struts 再次初始化,但它发现它已经被初始化。

因此,您不能共享这些类型的依赖项,因为它们需要存在于自己的 Servlet 上下文中。因此,您应该将它们包含在每个 WAR 中。

如果您找到了更好的解决方案,请分享。但如果没有,我希望上面的评论对你有帮助。

于 2013-11-13T22:15:08.010 回答