如何将 Spring 3 Bean 注入 JSF 2 转换器
我正在尝试将 @javax.naming.Inject
一个名为 WtvrBean
的 Spring 3 Bean 注入 JSF 2 @FacesConverter
中。
Bean 和转换器都在同一个包中。并且,在我的 spring 的 applicationContext.xml 中,我正在扫描此包:
<context:component-scan base-package="my-package" />
但这不起作用。当然,使用转换器的 JSF 2 内部类是 绝对不在 my-package
中。
例如,如果我从 JSF 2 ManagedBean 中删除 @ManagedBean
,并将其替换为 @org.springframework.stereotype.Component
或 @Controller
,可以使用 Spring WebFlow 在此 ManagedBean 上对 WtvrBean
进行 @Inject
处理。
嗯,据我所知,Spring 中不存在 @Converter
构造型。
我知道我可以使用
FacesContextUtils.getWebApplicationContext(context).getBean("WtvrBean")
但是,通过这种方法,Web 应用程序和 Spring 之间的耦合变得更加紧密。 (注释是元数据,一些作者甚至不认为依赖)。
到目前为止,我正在使用 FacesContextUtils,如果没有更好的解决方案。
有什么想法吗?
I am trying to @javax.naming.Inject
a Spring 3 Bean called WtvrBean
into a JSF 2 @FacesConverter
.
Both the Bean and the Converter are on the same package. And, in my spring's applicationContext.xml, I am scanning this package:
<context:component-scan base-package="my-package" />
But this is not working. For sure, the JSF 2 internal class that uses the converter is
definitely not in my-package
.
For instance, if I remove the @ManagedBean
from a JSF 2 ManagedBean, and replace it to @org.springframework.stereotype.Component
or @Controller
, the WtvrBean
can be @Inject
ed on this ManagedBean, by using Spring WebFlow.
Well, as far as I know, there is no such thing as a @Converter
stereotype in Spring.
I know I can use
FacesContextUtils.getWebApplicationContext(context).getBean("WtvrBean")
But, with that approach, the coupling between the web app and the spring is getting more tight. (annotations are metadata, and are not even considered dependency by some authors).
I am using FacesContextUtils
so far, if there is no better solution.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要将 bean 注入到类的实例中,这些实例必须由 spring 管理。即 spring 必须实例化它们。但这并没有发生,所以 - 不,你不能在那里注射。
但这是因为您在 jsf 中注册了转换器。您可以跳过它:
然后,如果您使用的是 spring
:这样就可以了。它对我有用。
(值得一提的解决方法是,您可以使用 aspectj 编织和
@Configurable
,但我更喜欢你的FacesContextUtils
方法,编织会修改类,以便它们甚至成为 spring 管理的。如果它们没有被 spring 实例化。)If you want to inject beans into instances of a class, these instances have to be spring-managed. I.e. spring has to instantiate them. And this is not happening, so - no, you can't inject there.
But this is because you register the converter within jsf. You can skip that:
And then, if you are using the spring
<el-resolver>
:So that will do it. It has worked for me.
(A workaround worth mentioning is that you can do it by using aspectj weaving and
@Configurable
, but I'd prefer yourFacesContextUtils
approach. The weaving modifies classes so that they become spring managed even if they are not instantiated by spring.)简单的回答。
Simple answer.
嘿,我面临着同样的问题,Spring bean 没有被注入到 JSF 转换器中。
然后通过谷歌搜索我找到了答案,在 JSF 2.2 之后我们可以将转换器作为 jsf 托管 bean。然后我们就可以注入spring依赖了。
它解决了我的问题。
并在您的 jsf 页面中像托管 bean 一样使用它
hey i was facing the same problem that spring beans are not getting injected in the JSF Converter.
then by googling about it i found the answers that after JSF 2.2 we can make converters as jsf managed bean. and then we can inject the spring dependency.
it solved my problem.
and in your jsf page use it like a managed bean
将@Scope 注释(例如,使用“request”参数)添加到您的托管bean 中。
Add @Scope annotation (eg with "request" parameter) to your managed bean.