在Glassfish中使用来自eager-fetched JSF托管bean的spring bean的问题

时间:2011-06-16 09:08:30

标签: java spring jsf java-ee glassfish

我正在使用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 bean,并且从堆栈跟踪看起来JSF开始在com.sun.faces.config.ConfigureListener中初始化JSF bean时,Spring上下文尚未设置,这就是为什么发生这种异常。

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

顺便说一下,当它在Tomcat 7上运行时一切正常,但在Glassfish 3.1上它搞砸了。也许在调用侦听器的顺序时有不同的容器实现?

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

1 个答案:

答案 0 :(得分:0)

  

当它在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