简体   繁体   English

我可以将一个POJO注入BackingBean吗? 还有@Entity到POJO? 它是如何工作的?

[英]Can I inject a POJO to a BackingBean ? And a @Entity to a POJO? How does it work?

So this is my code: 所以这是我的代码:

<h:form>
    <p:dataTable var="customer" value="#{customersTableBackingBean.allCustomers}">
        <p:column headerText="First Name">
            <h:outputText value="#{customer.contactFirstName}" />
        </p:column>

        <p:column headerText="City">
            <h:outputText value="#{customer.city}" />
        </p:column>
    </p:dataTable>
</h:form>

BackingBean: BackingBean:

package com.tugay.maythirty.model;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;

@Named
public class CustomersTableBackingBean implements Serializable {

    @Inject
    CustomersService customersService;

    public List<Customers> getAllCustomers(){
        return customersService.getAllCustomers();
    }

    public String sayHello(){
        return "Hello from a managed bean!";
    }
}

Service: 服务:

import javax.inject.Inject;
import java.io.Serializable;
import java.util.List;

public class CustomersService implements Serializable {

    @Inject
    private CustomersDao customersDao;

    public List<Customers> getAllCustomers(){
        boolean userLoggedIn = true;
        if(userLoggedIn){
            return customersDao.getAllCustomers();
        }
        else{
            return null;
        }
    }
}

Dao: 道:

import javax.persistence.*;
import java.util.List;

public class CustomersDao {

    @PersistenceContext(unitName = "Persistence")
    private EntityManager em;

    public List<Customers> getAllCustomers() {
        TypedQuery<Customers> query = em.createQuery("SELECT c FROM Customers c", Customers.class);
        List<Customers> allCustomers = query.getResultList();
        return allCustomers;
    }

    public void addCustomer(String customerName, String city){
        Customers customer = new Customers();
        customer.setCustomerName(customerName);
        customer.setCity(city);
        em.persist(customer);
    }
}

Entity: 实体:

@Entity
public class Customers {
    //relevant code here...

So this runs on Glassfish server, and runs just fine.. I can fetch the data from the db and see it on index.xhtml.. 所以这在Glassfish服务器上运行,运行得很好..我可以从数据库中获取数据并在index.xhtml上查看它。

My backing mean is a @Named bean. 我的支持意思是@Named bean。 So @Named makes this be accessible from the EL. 因此@Named可以从EL访问它。

However, it does not have any other annotations, such as Statless or Stateful.. So what is the "state" of this backing bean? 但是,它没有任何其他注释,例如Statless或Stateful .. 那么这个支持bean的“状态”是什么? Is the lifetime of this bean not managed by the container? 这个bean的生命周期是不是由容器管理的吗?

Also, as you can see I am injecting a Service to my BackingBean with @Inject,and a Dao to my Service, and an Entity to my Dao... Are these EJB 's? 另外,正如你所看到的那样,我正在使用@Inject向我的BackingBean注入一个服务,为我的服务注入一个Dao,为我的Dao注入一个实体......这些是EJB吗? Why would I want to make these @Stateless or @Stateful if they work just fine like this? 如果他们像这样工作得很好,我为什么要制作这些@Stateless或@Stateful?

Why would/should I make any of these beans @Stateful or @Statless? 为什么我应该/应该制作这些bean @Stateful或@Statless?

CustomersService is just a POJO. CustomersService只是一个POJO。 If you annotate it with @Stateful then it will be a statefull EJB (with @Statless - a stateless EJB). 如果使用@Stateful对其进行注释,那么它将是一个有状态的EJB(使用@Statless - 无状态EJB)。 Unlike a POJO (which is not an EJB), an EJB is managed by the EJB container, and can benefit with the facilities (eg container managed transactions, security, ...) that the EJB container has to offer. 与POJO(不是EJB)不同,EJB由EJB容器管理,并且可以受益于EJB容器必须提供的工具(例如,容器管理的事务,安全性......)。

But if you don't require the services provided by EJB container then I think there is no point in making them EJBs. 但是,如果您不需要EJB容器提供的服务,那么我认为将它们变成EJB是没有意义的。

So what is the "state" of this backing bean? 那么这个支持豆的“状态”是什么?

IMO, that doesn't quite make sense. IMO,这没有多大意义。 I think you mean the scope (eg request, session, application, ...) of the bean. 我认为你的意思是bean的范围(例如请求,会话,应用程序......)。 If so, since you haven't specified any scope for it, I think it won't be put in any of the scopes. 如果是这样,既然你没有为它指定任何范围,我认为它不会放在任何范围内。 It will be instantiated whenever needed (eg in this case it should be instantiated every time the page is requested). 它将在需要时进行实例化(例如,在这种情况下,应在每次请求页面时对其进行实例化)。 But if it was scoped then an instance of the bean would live as long as the scope itself would live. 但是如果它是作用域,那么只要范围本身存在,bean的实例就会存在。 eg a request scoped bean is instantiated for each new request. 例如,为每个新请求实例化请求范围的bean。

Here is the tutorial for EJB . 这是EJB的教程

Above tutorial link is for Java EE 6. Thanks to @Luiggi for the Java EE 7 tutorial link . 上面的教程链接适用于Java EE 6.感谢@Luiggi的Java EE 7教程链接

Per the CDI spec (sorry on my phone, can't look up the section easily) without any context / scope annotations the bean is DependantScoped, which means the container creates a new instance every time it's accessed (or created and set on the containing bean, similar to doing new yourself). 根据CDI规范(抱歉在我的手机上,无法轻松查找该部分)没有任何上下文/范围注释,bean是DependantScoped,这意味着容器每次访问时都会创建一个新实例(或者在包含时创建和设置bean,类似于自己做new )。

The other question about Entities, you should create a producer for each entity you want to directly access on the front end. 关于实体的另一个问题,您应该为要在前端直接访问的每个实体创建一个生产者。 If you don't, it may not save correctly on persist. 如果不这样做,可能无法正确保存。

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

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