Annotations
and Deployment Descriptors
In programming paradigm, there are two
approaches: imperative programming and declarative programming. Imperative
programming specifies the algorithm to achieve a goal (what
has to be done), whereas declarative programming specifies how to achieve
this goal (how it has to be done). In Java EE,
declarative programming is done by using metadata, that is, annotations or/and
deployment descriptors.
As you’ve seen earlier, components run in a container
and this container gives the component a set of services. Metadata are used to
declare and customize these services and associates additional information
along with Java classes, interfaces, constructors, methods, fields or parameters.
Since Java EE 5, annotations have been proliferating in
the enterprise platform. They decorate your code (Java classes, interfaces,
fields, methods . . .) with metadata information. Listing 1-1 shows a POJO (Plain Old Java
Object) that declares certain behavior using annotations on the class and on an
attribute (more on EJBs, persistence context and annotations in the coming
posts).
@Stateless
@Remote(ItemRemote.class)
@Local(ItemLocal.class)
@LocalBean
public class ItemEJB implements ItemLocal, ItemRemote {
@PersistenceContext (unitName = "chapter01PU")
private EntityManager em;
public Book findBookById(Long id) {
return em.find(Book.class, id);
}
}
The other manner of declaring
metadata is by using deployment descriptors. A deployment descriptor (DD)
refers to an XML configuration file that is deployed with the component in the
container. Listing
1-2 shows an EJB deployment descriptor. Like most of the Java EE 7
deployment descriptors, it defines the http://xmlns.jcp.org/xml/ns/javaee
namespace and contains a version attribute with the version of the
specification.
<ejb-jar xmlns=" http://xmlns.jcp.org/xml/ns/javaee " →
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " →
xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee →
http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd " →
version="3.2" >
<enterprise-beans>
<session>
<ejb-name>ItemEJB</ejb-name>
<remote>org.agoncal.book.javaee7.ItemRemote</remote>
<local>org.agoncal.book.javaee7.ItemLocal</local>
<local-bean/>
<ejb-class>org.agoncal.book.javaee7.ItemEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Deployment
descriptors need to be packaged with the components in the special META-INF or WEB-INF directory to
be taken in account. Table 1-1 shows the list of the Java EE
deployment descriptors and the related specification (more on that in the
coming chapters).
File
|
Specification
|
Paths
|
application.xml
|
Java EE
|
META-INF
|
application-client.xml
|
Java EE
|
META-INF
|
beans.xml
|
CDI
|
META-INF or WEB-INF
|
ra.xml
|
JCA
|
META-INF
|
ejb-jar.xml
|
EJB
|
META-INF or WEB-INF
|
faces-config.xml
|
JSF
|
WEB -INF
|
persistence.xml
|
JPA
|
META-INF
|
validation.xml
|
Bean Validation
|
META-INF or WEB-INF
|
web.xml
|
Servlet
|
WEB-INF
|
web-fragment.xml
|
Servlet
|
WEB-INF
|
webservices.xml
|
SOAP Web Services
|
META-INF or WEB-INF
|
Since Java EE 5 most deployment
descriptors are optional and you can use annotations instead. But you can also
use the best of both for your application. The biggest advantage of annotations
is that they significantly reduce the amount of code a developer needs to
write, and by using annotations you can avoid the need for deployment
descriptors. On the other hand, deployment descriptors are external XML files
that can be changed without requiring modifications to source code and
recompilation. If you use both, then the metadata are overridden by the
deployment descriptor (i.e., XML takes precedence over annotations) when the
application or component is deployed.
Note In
today’s development annotations are preferred over deployment descriptors in
Java EE. That is because there is a trend to replace a dual language
programming (Java + XML) with only one (Java). This is also true because it's
easy to analyze and prototype an application, when everything (data, methods,
and metadata with annotations) is in one place.
Java
EE uses the notion of Programming by Exception (a.k.a. Convention over
Configuration) so that most of the common behavior doesn’t need to be declared
with metadata (“programming metadata is the exception, the container takes care
of the defaults”). Which means that with only a small amount of annotations or
XML the container can give you a default set of services with default behavior.
Reference:
Beginning Java EE 7