简体   繁体   English

在Glassfish中使用从预先获取的JSF管理的bean中使用spring bean的问题

[英]Problem with using spring bean from eagerly-fetched JSF managed bean in Glassfish

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. 我正在使用JSF 2.0和Spring 3.0.5进行项目,该项目在Glassfish 3.1开源版本上运行。 Then I have a ApplicationScoped managed bean declared with 'eager': 然后我有一个用'eager'声明的ApplicationScoped托管bean:

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

which have a managed property injected using EL: 具有使用EL注入的托管属性:

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

and then configure foo in Spring context file: 然后在Spring上下文文件中配置foo:

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

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

<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: 毕竟,当我启动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

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. 由于我使用ContextLoaderListener初始化Spring Bean,并且从堆栈跟踪中可以看出,当JSF开始在com.sun.faces.config.ConfigureListener中初始化JSF Bean时,尚未设置Spring上下文,这就是为什么发生此异常的原因。

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. 因此,我想知道ConfigureListener(JSF)和ContextLoaderListener(Spring)之间的初始化顺序是否有问题,Spring侦听器应该位于JSF侦听器之前,但事实并非如此。 Furthermore, JSF listener is configured in JSF impl implicitly and I don't know how can I change their order. 此外,JSF侦听器是隐式配置在JSF中的,我不知道如何更改其顺序。

BTW, everything is OK when it's running on Tomcat 7, but on Glassfish 3.1 it messed up. 顺便说一句,当它在Tomcat 7上运行时,一切正常,但是在Glassfish 3.1上,它搞砸了。 Maybe there's something to do with different container implementation on invoking order of listeners? 也许在调用侦听器顺序上与不同的容器实现有关?

Any idea about this problem? 关于这个问题有什么想法吗? Thanks in advance! 提前致谢!

everything is OK when it's running on Tomcat 7, but on Glassfish 3.1 it messed up 在Tomcat 7上运行时,一切正常,但在Glassfish 3.1上,一切混乱

did you check the dependencies of your project ? 您是否检查了项目的依赖关系? afaik tomcat 7 does not provide jsf 2.0 support out of the box afaik tomcat 7不提供jsf 2.0支持

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 对于正确的配置,这个spring论坛线程可能有用http://forum.springsource.org/showthread.php?86577-Spring-3-JSF-2&p=290645#post290645

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM