EJB(Enterprise Java Beans)

  • Abbreviation of Enterprise Java Beans.
  • An enterprise, Java bean is a server side component And capsulate the business logic of an application. The business logic is the code that fulfils the purpose of the application. It does not perform display of business data(Presentation) I perform operation operations directly on the database(Persistence).
  • EJB is a POJO That uses specific annotations at the class and method level to mark it as an EJB.
  • It is managed(Instantiated and destroyed) By An EJB container.
  • There are number of variants of Enterprise Java beans.
    • Stateless session
      • Keep state across method invocations.
      • Not used widely.
    • State-full session
      • Does not maintain state across method invocations.
      • Used widely
    • Singleton
      • Only a single instance is instantiated for every use.
    • Message Driven
      • Used to process messages sent to it an asynchronously.
    • Timer
      • Used to invoke business logic as per a desired schedule.
  • J2EE EJB server or container
    • An EJB container manage the enterprise beans contained within it.
    • For each enterprise bean, The container is responsible for registering the object, providing a remote interface for the object, creating and destroying object instances, checking security for the object, managing the active state for the object, and coordinating distributed transactions.
    • A Web container can be any web application server.
  • Inversion of control
    • Inversion of control(IOC) Is a programming technique.
    • Inversion of control is expressed in terms of object oriented programming, in which object coupling is bound at runtime Buy a dependent object That is typically not known at compile time using static analysis.
    • Use cases
      • Services such as logging, business services, data access services
      • Mock implementation for test driven development Of unit tests.
    • We code by contract.
      • We define an interface which is a blueprint of a class. It contains a list of methods and properties but no code is written yet.
      • We define blue point of an object called class where methods are coded. Variables do not have values assigned yet. A class that implements interface must implement all the methods in the interface.
    • Instance
      • Physical existence in memory variables are assigned, for example this.name=“Gaurav”.
    • For example, when we hit an API, we must have an object with the response status and message. This will be our contract. Next, we will define classes based on above contract which will provide data plus response information to contract That is status and message. Best on the API hit and type/status of response. We will bind the necessary class at runtime.
      • Interface BaseContract<X>
      • {
      • String status;
      • String message;
      • X data;
      • }
      • Class Success implements BaseContract<SuccessData>
      • {
      • List<String> metadata;
      • }
      • Class Error implements BaseContract<ErrorData>
      • {
      • List<String> metadata;
      • }
  • CDI(Contexts And Dependency injection)
    • Primarily used to support inversion of control(IOC)
    • Any Java class or EJB can be injected if it meets the following
      • It is not a non-static in inner class.
      • It is class or is annotated @Decorator
      • It is not with an EJB component-Defining annotation or declared as an EJB bean class In ejb-jar.xml.
      • It has an appropriate constructor, which has one of the following as the use case:
        • The class has a constructor with no parameters.
        • The class declares a constructor or annotated @inject.

No comments:

Post a Comment