如何将 Spring 3 Bean 注入 JSF 2 转换器

发布于 2024-10-18 05:34:10 字数 910 浏览 1 评论 0原文

我正在尝试将 @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 @Injected 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 技术交流群。

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

发布评论

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

评论(4

埋葬我深情 2024-10-25 05:34:10

如果要将 bean 注入到类的实例中,这些实例必须由 spring 管理。即 spring 必须实例化它们。但这并没有发生,所以 - 不,你不能在那里注射。

但这是因为您在 jsf 中注册了转换器。您可以跳过它:

@Component("myConverter")
public class MyConverter implements Converter { .. }

然后,如果您使用的是 spring

converter="#{myConverter}"

这样就可以了。它对我有用。

(值得一提的解决方法是,您可以使用 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:

@Component("myConverter")
public class MyConverter implements Converter { .. }

And then, if you are using the spring <el-resolver>:

converter="#{myConverter}"

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 your FacesContextUtils approach. The weaving modifies classes so that they become spring managed even if they are not instantiated by spring.)

陪我终i 2024-10-25 05:34:10
@FacesConverter(value = "examTypeConverter")
@Named

简单的回答。

@FacesConverter(value = "examTypeConverter")
@Named

Simple answer.

勿忘初心 2024-10-25 05:34:10

嘿,我面临着同样的问题,Spring bean 没有被注入到 JSF 转换器中。

然后通过谷歌搜索我找到了答案,在 JSF 2.2 之后我们可以将转换器作为 jsf 托管 bean。然后我们就可以注入spring依赖了。
它解决了我的问题。

 @ManagedBean
 @RequestScoped
 public class CustomerByName implements Converter {

      @ManagedProperty(value = "#{customerDao}")
      private CustomerDao customerDao;

并在您的 jsf 页面中像托管 bean 一样使用它

 converter="#{customerByName}"

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.

 @ManagedBean
 @RequestScoped
 public class CustomerByName implements Converter {

      @ManagedProperty(value = "#{customerDao}")
      private CustomerDao customerDao;

and in your jsf page use it like a managed bean

 converter="#{customerByName}"
转角预定愛 2024-10-25 05:34:10

将@Scope 注释(例如,使用“request”参数)添加到您的托管bean 中。

@ManagedBean
@Scope("request")
public class MyBean {
....
}

Add @Scope annotation (eg with "request" parameter) to your managed bean.

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