What is an EJB, and what does it do?

前端 未结 4 1928
夕颜
夕颜 2021-01-29 17:25

Been trying to learn what EJB beans are, what does it mean their instances are managed in a pool, blah blah. Really can\'t get a good grip of them.

Can you

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-29 17:40

    The question that interests me most is how and where can I use them. To understand this, we need first to see what types of EJBs exist. There are 2 big categories:

    1. Session beans
    2. Message driven beans

    Let's consider Session Beans. They are of 3 kinds:

    1. Stateful - these components maintain state and are specific for a client across multiple requests. See it as a session. The immediate use these could be put to is shop carts or other type of sessions (login session and so on)
    2. Stateless - these are self contained components that don't persist information between requests, but they are unique to the user. Immediate use that comes to mind - Service classes in the service layer. Imagine OrderService. Another big use for these is to expose web services. Again, this be in the Service layer or totally separate.
    3. Singleton - these are the beans that exist per application and are created once and can be reused / accessed multiple times. Immediately the Configuration component comes to mind - where you can store application level configs and access them when you need them from anywhere.

    Now the rest of the capabilities or features can be used across layers in any such situations:

    • Security - you can check for permissions with an annotation on the method that is called. This can happen in the Service layer as well as in Controller if you wish so.
    • Transaction management - this is the obvious candidate in the Service layer or Persistence layer
    • Dependency Injection - again will be used everywhere

    One big use in modern times are the so called Microservices and Service Oriented Architectures. You can package some business logic components as EJBs and distribute them across the organization, to be used by multiple clients (by client here I mean other back-end applications).

    And so on. Now the big drawback is that you become very dependent on the EJB container and although you could switch between 2 reference implementations, you will not be able to switch to something lighter - Tomcat for example. But why would you want to sacrifice all the benefits?

提交回复
热议问题