JEE Core Concepts - Resources, Naming and Injection

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Changing Application Behavior without Changing Code

The possibility to change an application's behavior without changing code is a core concern in JEE. The entire Chapter 5 in the JEE specifications is dedicated to it.

The mechanism than makes this possible is the application component environment, also known as "enterprise naming context" or "ENC".

The application code relies on ENC named entries to refer to configuration values and external resources.

The names of those elements do not change in the code, and those names are mapped to deployment environment specific values and resources during deployment. T

he most portable way of mapping the environment specific values and resources is to specify them in deployment descriptors. The deployment descriptors act as a layer of indirection between code and the deployment environment. The code does not change, but the deployment descriptors do. The developers have an option to also do that mapping in the code, using "lookup" attributes associated with various annotations, but that results in code less portable between environments.

Application Component Environment

The application component environment is also known as enterprise naming context or ENC.

Environment Entry

Applications use named environment entries to declare a dependency on an external configuration parameter or a resource.

The declaration can be made in the code, using an annotation like @Resource, or in the component's deployment descriptor. If annotations are used, the container will inject the appropriate value, if found in the component naming context. If deployment descriptor declarations are used, the component will have to look the entry up in the context using JNDI API calls.

An example of such dependency declaration using annotations is:

@Resource
private int configurationParameterA;

Assuming that the above declaration was made in a io.example.ServletA class, the declaration creates a "io.example.ServletA/configurationParameterA" simple environment entry in the component naming context "java:comp/env" and expresses the request of the component of being provided a value for "configurationParameterA" when the component is deployed. The value is specific to the deployment environment in which the component was deployed.




. Examples of such dependencies are:


Simple Environment Entry

Applications use simple environment entries do declare a dependency on an external configuration parameter. Examples of such dependencies are:

@Resource
private int configurationParameterA;


For more details on @Resource semantics and defaults, see @javax.annotation.Resource.

TODO