JBoss.org Community Documentation

3.1. Creating POJOs

The example that relates to this section can be found in the examples/User_Guide/gettingStarted/humanResourcesService directory. The directory layout, as with all of the examples for the User Guide, follows the Maven Standard Directory Layout.

humanResourcesService/pom.xml
                     /src/main/java
                         /test/java
                         /test/resources

Java source files are located in packages beneath the src/main/java directory:

org/jboss/example/service/Address.java
                         /Employee.java
                         /HRManager.java

org/jboss/example/service/util/SalaryStrategy.java
                              /AgeBasedSalaryStrategy.java
                              /LocationBasedSalaryStrategy.java

Each of these classes represents a simple POJO that doesn't implement any special interfaces. The most important class is HRManager as this represents the service entry point providing all of the public methods that clients will call.

The Human Resources Service is therefore composed of a handful of classes which work together to allow a list of employees, together with details of their addresses and salaries, to be maintained by an HRManger. Thanks to the SalaryStrategy interface it is possible to configure the HRManager so that different salary strategy implementations are used. These place minimum and maximum limits on the salaries that can be awarded to employees depending on various rules.

To compile the source code you simply need to enter mvn compile from the humanResourcesService/ directory. This will create a new directory called target/classes containing the compiled code. To clean up the project and remove the target directory simply enter mvn clean.

Now that we have compiled our classes we need to create instances of them. This is done by creating an XML deployment descriptor that contains a list of beans representing individual instances. Each bean is given a name so that the instance can be looked up at runtime by clients.


<?xml version="1.0" encoding="UTF-8"?>

<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:jboss:bean-deployer:2.0 bean-deployer_2_0.xsd"
            xmlns="urn:jboss:bean-deployer:2.0">

   <bean name="HRService" class="org.jboss.example.service.HRManager"/>
      
</deployment>

Here we have declared that we want to create an instance of the HRManager class and register it with the name HRService. This file is passed to an XML deployer associated with the microcontainer at runtime to perform the actual deployment and instantiate the beans.