简体   繁体   English

如何在我的Java EE应用程序中连接各个部分

[英]How to connect the various part in my Java EE Application

I am making a Java EE application just to use what I learn while reading in the books. 我开发Java EE应用程序只是为了利用我在阅读本书时所学的知识。 I made a simple Entity and a DAO object that do different actions on this Entity such as create, update etc. Then in my EJB I want to use this DAO object so that I later can expose it to JSF etc. 我制作了一个简单的Entity和一个DAO对象,对该实体执行不同的操作,例如创建,更新等。然后在我的EJB中,我想使用此DAO对象,以便以后可以将其公开给JSF等。

I am having a little problems of understanding how to use objects of different classes in different layers of my application. 我在理解如何在应用程序的不同层中使用不同类的对象时遇到一些问题。 How do I expose them to each other? 如何将它们彼此暴露?

Do I just do as in regular Java SE? 我是否像常规Java SE一样进行操作? Have properties for the different classes and instantiate them in the classes I want? 是否有不同类的属性,并在我想要的类中实例化它们? (No probably not, I have seen the use of @Inject and so on, but I do not understand one bit of it. And not when I can use it either) (可能不会,我已经看到了@Inject等的用法,但我一点也不了解。而且也不是我什么时候可以使用的)

I would apprciate some text on when/how we use the different annotations and how we connect different layers rather than just some code showing it. 我会讲一些关于何时/如何使用不同注释以及如何连接不同图层的文本,而不只是一些显示它的代码。

I am on the Java chat if anyone have some spare time =) 如果有人有空闲时间,我正在Java聊天中)

You're asking too general questions. 您问的问题太笼统了。 Grab some decent book on Java EE (there are tons of them). 拿一些关于Java EE的不错的书(有很多)。 For better understanding of dependency injection, I'd recommend official Weld documentation . 为了更好地理解依赖注入,我建议使用官方的Weld 文档 However here is a typical example aimed to show you how to connect different Java EE layers: 但是,以下是一个典型示例,旨在向您展示如何连接不同的Java EE层:

JPA Entity : JPA实体

@Entity
public class Employee {

  @Id
  private Long id;

  private String name;

  //getters and setters
}

EJB bean : EJB bean

@Stateless
public class EmployeeService {

  //The entity manager will be injected automatically  
  @PersistenceContext
  private EntityManager em;

  public Employee findEmployeeById(Long id) {
    return em.find(Employee.class, id);
  }

}

JSF controller (let's assume it's CDI-bean): JSF控制器 (假设它是CDI-bean):

@Named
@SessionScoped
public class EmployeeController implements Serializable {

  //using CDI @Inject annotation empService will be initialized automatically
  @Inject
  private EmployeeService empService;

  //this method can be called from .xhtml page
  public String obtainEmployeeName(Long id) {
    String empName = "";
    Employee emp = empService.findEmployeeById(id);
    if (emp != null) {
      empName = emp.getName();
    }
    return empName;
  }
}

xhtml page : xhtml页面

<h:outputText value="#{employeeController.obtainEmployeeName(3)}" />

Update Some books that might help: 更新一些可能有帮助的书:

  • Pro JPA 2: Mastering the Java Persistence API Pro JPA 2:掌握Java Persistence API
  • Core JavaServer Faces (3rd Edition) 核心JavaServer Faces(第3版)
  • Beginning Java EE 6 with GlassFish 3 从GlassFish 3开始Java EE 6
  • These are quite popular and cover a lot of ground. 这些非常受欢迎,涉及很多领域。

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

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