在 Glassfish 中使用急切获取的 JSF 托管 bean 中的 spring bean 时出现问题

发布于 2024-11-15 11:27:41 字数 3196 浏览 1 评论 0原文

我正在开发一个使用 JSF 2.0 和 Spring 3.0.5 的项目,该项目在 Glassfish 3.1 开源版本上运行。然后我有一个用'eager'声明的ApplicationScoped托管bean:

@ManagedBean(eager = true)
@ApplicationScoped
public class CommonMB

它有一个使用EL注入的托管属性:

@ManagedProperty(#{foo})
private Foo foo;

然后在Spring上下文文件中配置foo:

<bean id="foo" class="Foo" />

最后在faces-config.xml中配置Spring EL解析器:

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

毕竟当我启动Glassfish时,抛出异常表明托管属性未正确注入:

Caused by: com.sun.faces.mgbean.ManagedBeanCreationException: unable to set property 'foo' on managed bean 'commonMB'
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:615)
at com.sun.faces.mgbean.ManagedBeanBuilder.buildBean(ManagedBeanBuilder.java:133)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:104)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:256)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:255)
... 76 more
Caused by: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicationContext(FacesContextUtils.java:83)
at org.springframework.web.jsf.DelegatingVariableResolver.getWebApplicationContext(DelegatingVariableResolver.java:167)
at org.springframework.web.jsf.DelegatingVariableResolver.getBeanFactory(DelegatingVariableResolver.java:156)
at org.springframework.web.jsf.DelegatingVariableResolver.resolveSpringBean(DelegatingVariableResolver.java:134)
at org.springframework.web.jsf.DelegatingVariableResolver.resolveVariable(DelegatingVariableResolver.java:112)
at com.sun.faces.el.VariableResolverChainWrapper.getValue(VariableResolverChainWrapper.java:115)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at de.odysseus.el.tree.impl.ast.AstIdentifier.eval(AstIdentifier.java:84)
at de.odysseus.el.tree.impl.ast.AstEval.eval(AstEval.java:51)
at de.odysseus.el.tree.impl.ast.AstNode.getValue(AstNode.java:30)
at de.odysseus.el.TreeValueExpression.getValue(TreeValueExpression.java:122)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
at com.sun.faces.mgbean.BeanBuilder$Expression.evaluate(BeanBuilder.java:591)
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:606)
... 82 more

由于我使用 ContextLoaderListener 来初始化 Spring beans,并且从堆栈跟踪看来,当 JSF 开始初始化 JSF beans 时com.sun.faces.config.ConfigureListener,Spring上下文还没有设置好,所以才会出现这个异常。

所以我想知道ConfigureListener(JSF)和ContextLoaderListener(Spring)之间的初始化顺序是否有问题,Spring监听器应该出现在JSF监听器之前,但事实并非如此。此外,JSF 监听器是在 JSF impl 中隐式配置的,我不知道如何更改它们的顺序。

顺便说一句,在 Tomcat 7 上运行时一切正常,但在 Glassfish 3.1 上运行时就出错了。也许与调用侦听器顺序的不同容器实现有关?

对这个问题有什么想法吗?提前致谢!

I'm working on a project using JSF 2.0 and Spring 3.0.5, which is running on Glassfish 3.1 open source edition. Then I have a ApplicationScoped managed bean declared with 'eager':

@ManagedBean(eager = true)
@ApplicationScoped
public class CommonMB

which have a managed property injected using EL:

@ManagedProperty(#{foo})
private Foo foo;

and then configure foo in Spring context file:

<bean id="foo" class="Foo" />

lastly configure Spring EL resolver in faces-config.xml:

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

After all when I started up Glassfish, exceptions are thrown indicating the managed propertiy was not injected correctly:

Caused by: com.sun.faces.mgbean.ManagedBeanCreationException: unable to set property 'foo' on managed bean 'commonMB'
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:615)
at com.sun.faces.mgbean.ManagedBeanBuilder.buildBean(ManagedBeanBuilder.java:133)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:104)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:256)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:255)
... 76 more
Caused by: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicationContext(FacesContextUtils.java:83)
at org.springframework.web.jsf.DelegatingVariableResolver.getWebApplicationContext(DelegatingVariableResolver.java:167)
at org.springframework.web.jsf.DelegatingVariableResolver.getBeanFactory(DelegatingVariableResolver.java:156)
at org.springframework.web.jsf.DelegatingVariableResolver.resolveSpringBean(DelegatingVariableResolver.java:134)
at org.springframework.web.jsf.DelegatingVariableResolver.resolveVariable(DelegatingVariableResolver.java:112)
at com.sun.faces.el.VariableResolverChainWrapper.getValue(VariableResolverChainWrapper.java:115)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at de.odysseus.el.tree.impl.ast.AstIdentifier.eval(AstIdentifier.java:84)
at de.odysseus.el.tree.impl.ast.AstEval.eval(AstEval.java:51)
at de.odysseus.el.tree.impl.ast.AstNode.getValue(AstNode.java:30)
at de.odysseus.el.TreeValueExpression.getValue(TreeValueExpression.java:122)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
at com.sun.faces.mgbean.BeanBuilder$Expression.evaluate(BeanBuilder.java:591)
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:606)
... 82 more

Since I'm using ContextLoaderListener to initialize Spring beans, and from the stacktrace it seems when JSF begin to initialize JSF beans in com.sun.faces.config.ConfigureListener, Spring context has not been set up yet, and that's why this exception happens.

So I'm wondering if there is something wrong with the initialization order between ConfigureListener(JSF) and ContextLoaderListener(Spring), Spring listener should come before JSF listener, but it didn't. Furthermore, JSF listener is configured in JSF impl implicitly and I don't know how can I change their order.

BTW, everything is OK when it's running on Tomcat 7, but on Glassfish 3.1 it messed up. Maybe there's something to do with different container implementation on invoking order of listeners?

Any idea about this problem? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-11-22 11:27:41

在 Tomcat 7 上运行时一切正常,但在 Glassfish 3.1 上则出现问题

您是否检查过项目的依赖项? afaik tomcat 7 不提供开箱即用的 jsf 2.0 支持

以进行正确的配置,这个 spring 论坛线程可能有用 http://forum.springsource.org/showthread.php?86577-Spring-3-JSF-2&p=290645#post290645

everything is OK when it's running on Tomcat 7, but on Glassfish 3.1 it messed up

did you check the dependencies of your project ? afaik tomcat 7 does not provide jsf 2.0 support out of the box

for the proper configuration this spring forum thread might be useful http://forum.springsource.org/showthread.php?86577-Spring-3-JSF-2&p=290645#post290645

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文