简体   繁体   English

使用CDI(Seam 3)视图范围的JSF转换器范围

[英]JSF converter scope when using CDI (Seam 3) view-scoped

I'm currently reviewing code and found CDI converters like: 我目前正在审查代码,发现CDI转换器如下:

@Named
@RequestScoped
public class BankConverter implements Converter, Serializable
{
    @EJB
    private BankService bankService;

    @Override
    public Object getAsObject( FacesContext ctx, UIComponent comp, String identifier )
    {
        if ( identifier == null || identifier.trim().isEmpty() )
        {
            return null;
        }

        Bank bank = null;

        try
        {       
            bank = this.bankService.findByPrimaryKey( Long.valueOf( identifier ) );
        }
        catch( Exception e )
        {
            // omitted
        }

        return bank;
    }

    @Override
    public String getAsString( FacesContext ctx, UIComponent comp, Object obj )
    {
        if ( obj == null || ( ( Bank ) obj ).getId() == null )
        {
            return null;
        }

        return ( ( Bank ) obj ).getId().toString();
    }

}

The converters are basically always used like this (note the converter="#{bankConverter}" ): 转换器基本上总是这样使用(请注意converter="#{bankConverter}" ):

<p:autoComplete id="bank"
                value="#{employeeDepotManager.selectedBank}"
                var="bnk"
                converter="#{bankConverter}"
                completeMethod="#{autoCompleter.completeBankSearch}"
                itemLabel="#{bnk.name}"
                itemValue="#{bnk}"
                forceSelection="false"
                minQueryLength="3"
                global="true"
                validator="#{employeeDepotManager.validateBank}"
                scrollHeight="200">
    <p:ajax event="itemSelect" update="bank-code bank-name" />
    <p:column>#{bnk.code}</p:column>
    <p:column>#{bnk.name}</p:column>
</p:autoComplete>                    

I am currently discussing with a colleague about which scope would be the best for the converters... 我目前正在与一位同事讨论哪种范围最适合转炉...

95% of the manager beans referenced from the JSF pages are @ViewScoped and so I thought it would be best for the converters to be @ViewScoped as well (instead of @RequestScoped, which as far as I understand would recreate a converter instance per AJAX request). 从JSF页面引用的管理器bean中有95%是@ViewScoped ,因此我认为也最好将转换器也使用@ViewScoped (而不是@RequestScoped,据我所知, @ViewScoped会根据AJAX重新创建一个转换器实例)请求)。

Then my colleague added, that the converter should probably be @Dependent as this would automatically put the converters into the scope the the surrounding beans are in. My feeling said, this wouldn't work. 然后我的同事补充说,转换器可能应该是@Dependent因为这会自动将转换器放入周围bean所在的范围内。我的感觉是,这行不通。 However, I couldn't really disagree as my knowledge pretty much ends here. 但是,我不能完全不同意,因为我的知识到此为止。

So, what would probably be the best scope for converters when almost all beans referenced from JSF are @ViewScoped ? 那么,什么将可能是转换器的最佳范围的时候,几乎从JSF引用的所有豆@ViewScoped

PS: Note that we are using Seam 3 to mix @Named and @ViewScoped PS:请注意,我们正在使用Seam 3来混合@Named@ViewScoped

As most part of the converters are actually stateless, they could easily be @ApplicationScoped , which is, in my opinion, the most natural scope for them. 由于大多数转换器实际上是无状态的,因此它们很容易成为@ApplicationScoped ,在我看来,这是它们最自然的作用域。 Nonetheless, some converters actually aren't. 但是,某些转换器实际上不是。 For example, DateTimeConverter behind <f:convertDateTime> tag does hold some state. 例如, <f:convertDateTime>标记后面的DateTimeConverter确实具有某些状态。 Moreover, the default implementation of @FacesConverter uses Application#createConverter(String converterId) that creates a new converter instance when it is needed, so it can be created more than once per request. 此外, @FacesConverter的默认实现使用Application#createConverter(String converterId)在需要时创建一个新的转换器实例,因此每个请求可以创建一个以上的转换器实例。

Also, as far as I'm concerned custom converters don't have any intersection with the referenced backing beans in terms of scope, so it doesn't matter whether they are ViewScoped or not. 另外,就范围而言,自定义转换器与引用的后备bean没有任何交集,因此无论它们是否为ViewScopedViewScoped What actually matters when choosing scope of a converter is the scope of the state held in the converter instance, as it was rightly spotted by BalusC. 选择转换器范围时,真正重要的是转换器实例中所保存状态的范围,这是BalusC正确发现的。

As far as the converter in your question in fact is stateless , it can safely be @ApplicationScoped . 只要您所质疑的转换器实际上是无状态的 ,就可以安全地使用@ApplicationScoped

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

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