Ce contenu n'est pas disponible dans la langue sélectionnée.

Chapter 3. Building Services


Services are pieces of code which perform work needed by multiple clients. For our purposes, we will put some additional constraints on the definition of a service. Services should have unique names which can be referenced, or called, by clients. The internals of a service should be invisible and unimportant to clients. This is the "black box" concept of object-oriented programming (OOP). In OOP, each object is independent, and no other object needs to know how it does its job.
In the context of the Microcontainer, services are built from POJOs. A POJO is nearly a service in its own right, but it can't be accessed by a unique name, and it must be created by the client that needs it.
Although a POJO must be created at run-time by the client, it does not need to be implemented by a separate class in order to provide a well-defined interface. As long as fields and methods are not removed, and access to them is not restricted, there is no need to recompile clients to use a newly-created POJO.

Note

Implementing an interface is only necessary in order to allow a client to choose between alternative implementations. If the client is compiled against an interface, many different implementations of the interface can be provided without having to recompile the client. The interface ensures that the method signatures do not change.
The remainder of this guide consists of creating a Human Resources service, using the Microcontainer to capture and modularize the business logic of the application. After the Microcontainer is installed, the example code is located in examples/User_Guide/gettingStarted/humanResourcesService.

3.1. Introduction to the Human Resources Example

As you familiarize yourself with the directory structure of the files in the example, note that it uses the Maven Standard Directory Layout.
The Java source files are located in packages beneath the examples/User_Guide/gettingStarted/humanResourcesService/src/main/java/org/jboss/example/service directory, after you have extracted the ZIP file. Each of these classes represents a simple POJO that doesn't implement any special interfaces. The most important class is HRManager, which represents the service entry point providing all of the public methods that clients will call.

Methods Provided by the HRManager Class

  • addEmployee(Employee employee)
  • removeEmployee(Employee employee)
  • getEmployee(String firstName, String lastName)
  • getEmployees()
  • getSalary(Employee employee)
  • setSalary(Employee employee, Integer newSalary)
  • isHiringFreeze()
  • setHiringFreeze(boolean hiringFreeze)
  • getSalaryStrategy()
  • setSalaryStrategy(SalaryStrategy strategy)
The Human Resources Service is composed of a handful of classes which maintain a list of employees and their details (addresses and salaries, in this case). Using the SalaryStrategy interface it is possible to configure the HRManager so that different salary strategy implementations are available to place minimum and maximum limits on the salaries for different employee roles.
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat