You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2013/10/04 11:42:47 UTC
svn commit: r1529111 -
/openwebbeans/cms-site/trunk/content/cdi_explained.mdtext
Author: struberg
Date: Fri Oct 4 09:42:47 2013
New Revision: 1529111
URL: http://svn.apache.org/r1529111
Log:
add code sample
Modified:
openwebbeans/cms-site/trunk/content/cdi_explained.mdtext
Modified: openwebbeans/cms-site/trunk/content/cdi_explained.mdtext
URL: http://svn.apache.org/viewvc/openwebbeans/cms-site/trunk/content/cdi_explained.mdtext?rev=1529111&r1=1529110&r2=1529111&view=diff
==============================================================================
--- openwebbeans/cms-site/trunk/content/cdi_explained.mdtext (original)
+++ openwebbeans/cms-site/trunk/content/cdi_explained.mdtext Fri Oct 4 09:42:47 2013
@@ -66,35 +66,65 @@ and thus nowadays an integral part of al
Before we go on to dive into some code, letâs take a quick look at some key CDI features:
-- Type Safety: Instead of injecting objects by a (string) name,
+- **Type Safety**: Instead of injecting objects by a (string) name,
CDI uses the Java type to resolve injections. When the type is not sufficient,
a Qualifier annotation can be used. This allows the compiler to easily detect errors,
and provides easy refactoring.
-- POJOs: Almost every Java object can be injected by CDI!
+- **POJOs**: Almost every Java object can be injected by CDI!
This includes EJBs, JNDI resources, Persistence Units and Persistence Contexts,
as well as any object which previously would have been created by a factory method.
-- Extensibility: Every CDI container can enhance its functionality
+- **Extensibility**: Every CDI container can enhance its functionality
by using portable âExtensionsâ. The attribute âportableâ means that
those CDI Extensions can run on every CDI container and Java EE 6 server,
no matter which vendor. This is accomplished by a well-specified
SPI (Service Provider Interface) which is part of the JSR-299 specification.
-- Interceptors: It has never been easier to write your own Interceptors.
+- **Interceptors**: It has never been easier to write your own Interceptors.
Because of the portable behaviour of JSR-299, they now also run on every
EE 6-certified server and on all standalone CDI containers.
-- Decorators: These allow to dynamically extend existing interface
+- **Decorators**: These allow to dynamically extend existing interface
implementations with business aspects.
-- Events: CDI specifies a type-safe mechanism to send
+- **Events**: CDI specifies a type-safe mechanism to send
and receive events with loose coupling.
-- Unified EL integration: EL-2.2 opens a new horizon
+- **Unified EL integration**: EL-2.2 opens a new horizon
in regard of flexibility and functionality.
CDI provides out-of-the-box support for it!
+## A small CDI Example
+
+The following small sample application allows you to send eMails via a web form.
+We will only show code fragments, please checkout our samples form our
+[Source Code](source.html) for more information.
+
+
+#### The 'Backend'
+
+For our mail application, we need an âapplication-scopedâ MailService.
+Application-scoped objects are essentially singletons â
+the container will ensure you always get the same single instance whenever
+you inject it into your application.
+
+The ``replyTo`` address is taken from the ConfigurationService
+which is also application-scoped. Here we see our first injection.
+The âconfigurationâ field is not set by the applicationâs code,
+but is injected by CDI.
+The ``@Inject`` annotation tells CDI to perform the injection.
+
+ :::java
+ @ApplicationScoped
+ public class MyMailService implements MailService {
+ private @Inject ConfigurationService configuration;
+
+ public send(String from, String to, String body) {
+ String replyTo = configuration.getReplyToAddress();
+ ... // send the email
+ }
+ }