You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by ra...@apache.org on 2014/08/18 22:48:04 UTC
svn commit: r1618726 - /deltaspike/site/trunk/content/core.mdtext
Author: rafabene
Date: Mon Aug 18 20:48:03 2014
New Revision: 1618726
URL: http://svn.apache.org/r1618726
Log:
DELTASPIKE-640 - give some hints how to implement custom scopes
Modified:
deltaspike/site/trunk/content/core.mdtext
Modified: deltaspike/site/trunk/content/core.mdtext
URL: http://svn.apache.org/viewvc/deltaspike/site/trunk/content/core.mdtext?rev=1618726&r1=1618725&r2=1618726&view=diff
==============================================================================
--- deltaspike/site/trunk/content/core.mdtext (original)
+++ deltaspike/site/trunk/content/core.mdtext Mon Aug 18 20:48:03 2014
@@ -806,8 +806,7 @@ that exception chain, unless it's explic
## Scopes
-DeltaSpike Core provides the API and SPI for several scopes.
-Currently all scopes are only implemented in the JSF module.
+DeltaSpike Core provides the API and SPI for several scopes. Currently all scopes are only implemented in the [JSF module](jsf.html#scopes).
### @WindowScoped
@@ -815,6 +814,74 @@ Currently all scopes are only implemente
### @GroupedConversationScoped
+## Creating a custom CDI Scope
+
+If you want to create a custom CDI scope to match your needs, you will need to follow these steps:
+
+1. First, create an Annotation with annotated with @javax.inject.Scope; Example:
+
+ :::java
+ @Scope
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
+ public @interface ACustomScope {}
+
+2. Second, create an Extension to add the scope and a context for it. Example:
+
+ :::java
+ public class ACustomScopeExtension implements Extension, Serializable {
+
+ public void addACustomScope(@Observes final BeforeBeanDiscovery event) {
+ event.addScope(ACustomScope.class, true, false);
+ }
+
+ public void registerACustomScopeContext(@Observes final AfterBeanDiscovery event) {
+ event.addContext(new ACustomScopeContext());
+ }
+ }
+
+3. Implement a javax.enterprise.context.spi.Context interface to hold the javax.enterprise.inject.spi.Bean instances according to your needs
+
+ :::java
+ public class ACustomScopeContext implements Context, Serializable {
+
+ // Get the scope type of the context object.
+ public Class<? extends Annotation> getScope() {
+ return ACustomScope.class;
+ }
+
+ // Return an existing instance of certain contextual type or create a new instance by calling
+ // javax.enterprise.context.spi.Contextual.create(CreationalContext) and return the new instance.
+ public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
+ Bean bean = (Bean) contextual;
+ // you can store the bean somewhere
+ if (somewhere.containsKey(bean.getName())) {
+ return (T) somewhere.get(bean.getName());
+ } else {
+ T t = (T) bean.create(creationalContext);
+ somewhere.put(bean.getName(), t);
+ return t;
+ }
+ }
+
+ // Return an existing instance of a certain contextual type or a null value.
+ public <T> T get(Contextual<T> contextual) {
+ Bean bean = (Bean) contextual;
+ // you can store the bean somewhere
+ if (somewhere.containsKey(bean.getName())) {
+ return (T) somewhere.get(bean.getName());
+ } else {
+ return null;
+ }
+ }
+
+ // Determines if the context object is active.
+ public boolean isActive() {
+ return true;
+ }
+
+ }
+
## Deactivatable
DeltaSpike allows you to deactivate its own Extensions. You just need to implement your [ClassDeactivator](spi.html#classdeactivator).