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

发布于 2024-12-10 01:20:14 字数 384 浏览 2 评论 0原文

我正在制作一个 Java EE 应用程序,只是为了使用我在书中学到的知识。我创建了一个简单的实体和一个 DAO 对象,该对象对该实体执行不同的操作,例如创建、更新等。然后在我的 EJB 中,我想使用这个 DAO 对象,以便稍后可以将其公开给 JSF 等。

我有一点理解如何在应用程序的不同层中使用不同类的对象的问题。我如何将它们暴露给彼此?

我是否只是像常规 Java SE 中那样做?拥有不同类的属性并在我想要的类中实例化它们? (不,可能不是,我已经看到了 @Inject 等的使用,但我一点也不理解。而且当我可以使用它时也不理解)

我会欣赏一些关于何时/如何使用不同注释的文本以及我们如何连接不同的层,而不仅仅是一些显示它的代码。

如果有人有空闲时间的话我会参加 Java 聊天 =)

I am making a Java EE application just to use what I learn while reading in the books. 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.

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? 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)

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 =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2024-12-17 01:20:14

你问的问题太笼统了。找一些关于 Java EE 的不错的书(有很多)。为了更好地理解依赖注入,我推荐官方Weld

JPA Entity

@Entity
public class Employee {

  @Id
  private Long id;

  private String name;

  //getters and setters
}

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 控制器(我们假设它是 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 页面

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

更新
一些可能有帮助的书:

  • Pro JPA 2:掌握 Java 持久性 API
  • 核心 JavaServer Faces (第 3 版)
  • 使用 GlassFish 3 开始 Java EE 6
  • 这些非常受欢迎并且涵盖了很多领域。

    You're asking too general questions. Grab some decent book on Java EE (there are tons of them). For better understanding of dependency injection, I'd recommend official Weld documentation. However here is a typical example aimed to show you how to connect different Java EE layers:

    JPA Entity:

    @Entity
    public class Employee {
    
      @Id
      private Long id;
    
      private String name;
    
      //getters and setters
    }
    

    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):

    @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:

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

    Update
    Some books that might help:

  • Pro JPA 2: Mastering the Java Persistence API
  • Core JavaServer Faces (3rd Edition)
  • Beginning Java EE 6 with GlassFish 3
  • These are quite popular and cover a lot of ground.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文