简体   繁体   English

在jsf中使用不同bean的策略

[英]Strategy using different beans in jsf

i want to use 2 different beans (Spring) for one JSF-Page. 我想对一个JSF页使用2个不同的bean(Spring)。 I do not like to write every method into one bean, so i tried to separate into two beans like JobEditDataBean and JobEditActionBean . 我不喜欢将每种方法都写到一个bean中,因此我尝试将其分成两个类,例如JobEditDataBeanJobEditActionBean I want to use the JobEdiDataBean just as "Container" for my data objects and move the actionstuff (like saving, update etc.) to the action bean. 我想将JobEdiDataBean用作数据对象的“容器”,然后将actiontuff(例如保存,更新等)移动到action bean。 What i did by now (and what seems to work, but feels wrong) is the following: 我现在所做的(和似乎可行,但感觉不对劲)如下:

public class JobEditDataBean{
    @Autowired
    JobEditActionBean actionBean;
    // some objects...
    @PostConstruct
    public void init() {
       actionBean.setJobEditDataBean(this);
       // do something ... 
    }
}

public class JobEditActionBean{
    JobEditDataBean dataBean;
    // some objects...
}

Do you have any hints or tipps how this can be done better, nicer? 您是否有任何提示或技巧可以更好,更好地做到这一点?

Indeed, you don't need to have one bean per each page. 确实,您不必在每个页面上都有一个bean。 You can use as much beans you want for any page, it is fine, as whenever an expression like #{someMB} is found in your XHTML, JSF will find a bean with that name and create a new instance if necessary. 您可以在任何页面上使用任意数量的bean,这很好,因为只要在XHTML中找到#{someMB}类的表达式,JSF就会找到具有该名称的bean,并在必要时创建一个新实例。

If you need to inject one bean to another, just use @Autowired already: 如果需要将一个bean注入另一个bean,只需使用@Autowired

@Component
@Scope("request")
public class JobEditActionBean {
    @Autowired
    private JobEditDataBean dataBean;
    @PostConstruct
    public void init() {
        // dataBean.youCanUseDataBeanMethodsHereAlready()
    }
}

You just have to make sure both beans are in the Spring container (annotating them with @Component will do), and choosing the right scope for each one. 您只需要确保两个bean都在Spring容器中即可(使用@Component注释即可),并为每个bean选择正确的范围。 Beware of the scopes of the beans which you are injecting, cause it usually only makes sense to inject beans of broader scope to beans of the same or more restrict scope. 当心要注入的bean的作用域,导致通常只将范围更广的bean注入相同或更多限制作用域的bean才有意义。

Having said that, I recommend reading following thread about choosing the right scopes: 话虽如此,我建议阅读以下有关选择正确范围的主题:

How to choose the right bean scope? 如何选择合适的bean范围?

One more thing: this is only valid if your JSF beans are really being managed by the Spring container (that was my assumption after you used @Autowired ). 还有一件事:仅当您的JSF bean确实由Spring容器管理时才有效(这是我在使用@Autowired之后的假设)。 If you are letting JSF container manage the beans (using @ManagedBean with @RequestScoped or @ViewScoped , for example), the way you inject them is with a @ManagedProperty annotation: 如果让JSF容器管理Bean(例如,将@ManagedBean@RequestScoped@ViewScoped一起使用),则注入它们的方式是使用@ManagedProperty批注:

    ...
    @ManagedProperty("#{jobEditDataBean}")
    private JobEditDataBean dataBean;

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

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