by the
container in order to realize common use cases
An interface becomes a vehicle for encapsulation
or abstraction,
as it was originally intended to be:
“Interfaces are used
to encode similarities which the classes of various types share, but do not
necessarily constitute a class relationship. For instance, a human and
a parrot can
both whistle; however, it would not make sense to
represent Humans and Parrots as subclasses of
a Whistler class. Rather they would most likely be subclasses
of an Animal class (likely with intermediate classes), but
both would implement the Whistler interface.” [http://en.wikipedia.org/wiki/Java_interface]
Extensive use of interfaces derives from a
belief that they might
be helpful in the future
Premature Extensibility
Is the Root of Some Evil
The number of artifacts will
double and you will have to introduce a configuration facility
history is persisted in the database
by means of a JPA entity bean and those objects are serialized back to the Flex client each time you enter a new name.
Using @DataSourceDefinition to configure a DataSource
This annotation requires that a data source implementation class (generally from a JDBC driver JAR) be present on the class path (either by including it in your application, or deploying it as a top-level JAR and referring to it via MANIFEST.MF's Class-Path attribute) and be named explicitly.
this annotation bypasses the management layer and as such it is recommended only for development and testing purposes
Defining a Managed DataSource
Installing a JDBC driver as a deployment
Installing the JDBC Driver
deployment or as a core module
managed by the application server (and thus take advantage of the management and connection pooling facilities it provides), you must perform two tasks. First, you must make the JDBC driver available to the application server; then you can configure the data source itself. Once you have performed these tasks you can use the data source via standard JNDI injection.
recommended way to install a JDBC driver into the application server is to simply deploy it as a regular JAR deployment. The reason for this is that when you run your application server in domain mode, deployments are automatically propagated to all servers to which the deployment applies; thus distribution of the driver JAR is one less thing for administrators to worry about.
Note on MySQL driver and JDBC Type 4 compliance: while the MySQL driver (at least up to 5.1.18) is designed to be a Type 4 driver, its jdbcCompliant() method always return false. The reason is that the driver does not pass SQL 92 full compliance tests, says MySQL. Thus, you will need to install the MySQL JDBC driver as a module (see below).
define your module with a module.xml file, and the actual jar file that contains your database driver
content of the module.xml file
Under the root directory of the application server, is a directory called modules
module name, which in this example is com.mysql
where the implementation is, which is the resource-root tag with the path element
define any dependencies you might have. In this case, as the case with all JDBC data sources, we would be dependent on the Java JDBC API's, which in this case in defined in another module called javax.api, which you can find under modules/javax/api/main as you would expect.
CDI managed beans. The @EJB annotation is removed and @Inject is used instead
Annotating the boundary (Cart) with the @Named annotation makes the Cart immediately visible for expression language (EL) expressions in JSP and JSF
@Named annotation takes the simple name of the annotated class, puts the first character in lowercase, and exposes it directly to the JSF pages (or JSP). The Cart bean can be accessed directly, without any backed or managed beans, by the JSF pages: <h:commandButton value="Check out!" action="#{cart.checkout}" />
If there is a need for abstraction, the class can be turned into an interface (or abstract class)
local implementation (with CDI events
@Inject
Event<String> event;
event.fire("Order proceeded!");
remote implementation:
javax.enterprise.event.Event belongs to the CDI-implementation
class Event can be considered to be a lightweight alternative to the java.beans.PropertyChangeSupport class
@Inject
Event<String> event;
event.fire("Order proceeded!");
event can be received by any managed bean and also by EJB beans
provide a method with a single @Observes annotated parameter
@Observes String event
there is no real event, just the payload:
The during attribute in the @Observes annotation allows you to select in which transactional phase the event gets delivered. The default setting is IN_PROGRESS, which causes an immediate event delivery regardless of the transaction outcome. The AFTER_SUCCESS configuration causes the delivery to occur only after successful transaction completion
Although CDI events work only inside a single process (in the default case, CDI is extensible), they are perfectly suitable for decoupling packages from modules
The method checkout() starts a transaction that gets "reused" by the OrderSystem and CustomerNotification session beans
differences between CDI beans and EJBs is that EJBs are :
Transactional
Remote or local
Able to passivate stateful beans freeing up resources
Able to make use of timers
Can be asynchronous
Stateless EJBs can be thought of as thread safe single-use beans that don’t maintain any state between two web requests
Stateful EJBs do hold state and can be created and sit around for as long as they are needed until they are disposed of
Stateless beans must have a dependent scope while a stateful session bean can have any scope. By default they are transactional, but you can use the transaction attribute annotation.
CDI beans can be injected into EJBs and EJBs can be injected into CDI beans
When to use which bean
How do you know when to use which bean? Simple.
In general, you should use CDI beans unless you need the advanced functionality available in the EJBs such as transactional functions. You can write your own interceptor to make CDI beans transactional, but for now, its simpler to use an EJB until CDI gets transactional CDI beans which is just around the corner
Comparing JSF Beans, CDI Beans and EJBs
JSF Managed Beans
In short, don’t use them if you are developing for Java EE 6 and using CDI. They provide a simple mechanism for dependency injection and defining backing beans for web pages, but they are far less powerful than CDI beans.
JSF beans cannot be mixed with other kinds of beans without some kind of manual coding.
CDI Beans
includes a complete, comprehensive managed bean facility
interceptors, conversation scope, Events, type safe injection, decorators, stereotypes and producer methods
JSF-like features, you can define the scope of the CDI bean using one of the scopes defined in the javax.enterprise.context package (namely, request, conversation, session and application scopes). If you want to use the CDI bean from a JSF page, you can give it a name using the javax.inject.Named annotation
boundary, which main responsibilities are:Providing coarser granularityEnsuring consistencyProviding a defined entry point which can be easily decorated with aspects / interceptorsExposure of components (what components are we will cover later) functionality to remote clients via IIOP, REST, SOAP, JMS, Hessian etc...
ServiceFacade - the facade to Services. The Services just rely on a certain amount of cross cutting aspects and concentrate on the realization of business logic
only invokes Services in consistent way, mostly using transactions
Configuration for EJB 3
In order to initialize GDS/Tide for EJB 3 and Hibernate, you must add granite.jar and granite-hibernate.jar to your WEB-INF/lib