如何在不使用Spring IOC的情况下在JSF托管bean中注入服务 [英] How to inject service in JSF managed bean without using Spring IOC

查看:90
本文介绍了如何在不使用Spring IOC的情况下在JSF托管bean中注入服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果我必须在春季注入服务,我会使用

Typically if I have to inject a service in Spring I use

 <bean id="mycontroller" class="com.MyController">
    <property name="myService" ref="myService" />

<bean id="myService" class="com.MyService"></bean>

使用JSF时该如何做?我不想为bean使用两个IOC容器,而是将其保留在faces上下文本身中。我见过

How to do the same when using JSF? I dont want to use two IOC containers for the beans and rather keep it in faces context itself. I have seen links such as

JSF 2使用@ManagedProperty且不使用xml注入Spring bean /服务

将spring bean注入jsf bean的问题。他们谈论将Spring托管bean注入JSF上下文。
我想做的事情必须非常简单,但无法找到任何相关信息。我是一个新手,将不胜感激。

and A problem about injecting spring bean into jsf bean . They talk about injecting Spring managed bean into JSF context. What I am trying to do must be really simple but am not able to find any relevant info. Am a newbie and will appreciate any help.

推荐答案

我认为您可能会对 bean一词感到困惑。
事实是,您正在谈论的服务也是Spring bean,对吧?

I think you may be confused by the word "bean". The thing is, the "service" you are talking about is also a Spring bean, right?

您可能将其作为服务使用,因为它有一些Spring根据您的配置添加了其他功能(可能是事务管理)。

You probably have it as a service cause it has some additional features (probably transaction management) added by Spring, according to your configuration.

JSF IoC容器非常简单,它不允许您配置其生命周期以包括交易管理,AOP之类的东西。那些与Spring(或EJB,在Java EE环境中)有关的事情。

The JSF IoC container is very simplistic, it does not allow you to configure its lifecycle to include transaction management, AOP and things like that. Those things you have to do with Spring (or EJB, in a Java EE environment).

因此,在将JSF与Spring结合使用时,通常有两种选择:

So, when using JSF with Spring, you usually have two choices:


  1. 您可以将JSF页面的支持bean放在JSF容器中,并用 @ManagedBean @RequestScoped @ViewScoped 等;并在属性中注入任何必需的Spring bean并在属性中添加 @ManagedProperty (需要使用setter)

  2. 或跳过JSF容器,然后将在Spring容器中将bean与所有其他bean一起备份,并使用Spring的请求/会话作用域,并用Spring的注释 @Component @Scope进行注释(请求) @Scope(会话)并注入 @Autowired @Qualifier 之类。

  1. Either you put the backing beans for the JSF pages in the JSF container, annotating them with @ManagedBean, @RequestScoped, @ViewScoped, etc; and injecting any necessary Spring bean with @ManagedProperty in a property (a setter is required)
  2. Or skip the JSF container and put the backing beans along with all others in the Spring container, and use the Spring scopes of request/session, annotating them with Spring's annotations @Component, @Scope("request"), @Scope("session") and injecting with @Autowired, @Qualifier and the like.

个人面对这种选择我会选择第一个,因为它会给您 @ViewScoped 和其他一些优点。确实可以使用两个IoC容器,但是,哪个Java EE应用程序不使用?

Personally, faced with that choice I'd go with the first choice, cause it gives you @ViewScoped and some other niceties. It's true it makes use of two IoC containers but then, which Java EE app does not?

如果您仍然想走第二条路线,也可以添加由JSF viewMap支持的Spring bean的视图范围

If you want to go the second route anyway, you may also add a view scope for Spring beans, backed by JSF viewMap.

这篇关于如何在不使用Spring IOC的情况下在JSF托管bean中注入服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆