openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1529111 - /openwebbeans/cms-site/trunk/content/cdi_explained.mdtext
Date Fri, 04 Oct 2013 09:42:47 GMT
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
+        }
+    }
 
 



Mime
View raw message