这个简单的 Wicket 页面在 GlassFish 3.1.2.2 服务器中完美运行,但在 GlassFish 4.0 中部署时无法运行。非常重要的是要注意不包含 @Resource 注释的其他页面可以完美运行。
public class IndexPage extends WebPage {
// this EJB injection works as expected
@EJB(name = AnyManager.NAME)
AnyManager anyMgr;
// this JNDI Resource is defined as java.util.Properties at JNDI/Custom Resources of GlassFish admin console
// this resource can be looked up using normal InitialContext.doLookup() method
@Resource(name = "anyResource")
Properties config;
public IndexPage(final PageParameters p) {
super(p);
}
}
它给出了以下异常:
Last cause: org.apache.wicket.proxy.ILazyInitProxy not found by org.glassfish.hk2.external.cglib [27]
WicketMessage: Can't instantiate page using constructor 'public hu.app.IndexPage(org.apache.wicket.request.mapper.parameter.PageParameters)' and argument ''. An exception has been thrown during construction!
查看堆栈跟踪(从 IndexPage 构造函数的第一行开始):
Root cause:
java.lang.ClassNotFoundException: org.apache.wicket.proxy.ILazyInitProxy not found by org.glassfish.hk2.external.cglib [27]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.apache.wicket.proxy.LazyInitProxyFactory.createProxy(LazyInitProxyFactory.java:190)
at org.wicketstuff.javaee.injection.JavaEEProxyFieldValueFactory.getCachedProxy(JavaEEProxyFieldValueFactory.java:149)
at org.wicketstuff.javaee.injection.JavaEEProxyFieldValueFactory.getFieldValue(JavaEEProxyFieldValueFactory.java:79)
at org.apache.wicket.injection.Injector.inject(Injector.java:111)
at org.wicketstuff.javaee.injection.JavaEEComponentInjector.inject(JavaEEComponentInjector.java:68)
at org.wicketstuff.javaee.injection.JavaEEComponentInjector.onInstantiation(JavaEEComponentInjector.java:74)
at org.apache.wicket.application.ComponentInstantiationListenerCollection$1.notify(ComponentInstantiationListenerCollection.java:38)
at org.apache.wicket.application.ComponentInstantiationListenerCollection$1.notify(ComponentInstantiationListenerCollection.java:34)
at org.apache.wicket.util.listener.ListenerCollection.notify(ListenerCollection.java:80)
at org.apache.wicket.application.ComponentInstantiationListenerCollection.onInstantiation(ComponentInstantiationListenerCollection.java:33)
at org.apache.wicket.Component.(Component.java:683)
at org.apache.wicket.MarkupContainer.(MarkupContainer.java:121)
at org.apache.wicket.Page.(Page.java:168)
at org.apache.wicket.Page.(Page.java:132)
at org.apache.wicket.markup.html.WebPage.(WebPage.java:76)
at hu.app.IndexPage.(IndexPage.java:56)
在做了很多谷歌搜索之后,我有了一个想法,尝试在我的一个管理器 bean 中进行注入,并为它定义了一个简单的 getter。请参阅以下几行:
@Local
public interface AnyManager extends EntityManager {
public static final String NAME = "ejb/AnyManager";
Properties getConfig();
}
@Stateless(name = AnyManager.NAME)
public class AnyManagerImpl extends AbstractEntityManager implements AnyManager {
@Resource(name = "iOIRConfig")
Properties config;
@Override
public Properties getConfig() {
return config;
}
}
我已经像这样更改了我的 Wicket 页面:
public class IndexPage extends WebPage {
@EJB(name = AnyManager.NAME)
AnyManager anyMgr;
// so I have commented out
// @Resource(name = "anyResource")
Properties config;
public IndexPage(final PageParameters p) {
super(p);
// and get the value from one of my EJB manager class
config = anyMgr.getConfig();
}
}
这对我有用。我不喜欢这种类型的解决方案,我仍在寻找真正的解释和解决方案。GlassFish 4 中可能会发生什么变化而导致这种现象?我在 GlassFish 3 中没有这种奇怪的效果。
我对 GlassFish 3 和 GlassFish 4 都进行了全新安装。
我的应用程序是一个 EAR 应用程序,它包含一个 EJB.jar、WEBAPP.war 和一个带有共享公共 jar 的 lib/ 文件夹。也许这是某种类加载器问题,因为 EJB 和 WebApp 位于不同的文件夹中?但是为什么@EJB 有效而@Resource 无效呢?
可能是 Java EE 7 实现问题或 GlassFish 4 中的简单错误?
你能帮忙的话,我会很高兴。