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:52:36 UTC
svn commit: r1618729 - /deltaspike/site/trunk/content/core.mdtext
Author: rafabene
Date: Mon Aug 18 20:52:35 2014
New Revision: 1618729
URL: http://svn.apache.org/r1618729
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=1618729&r1=1618728&r2=1618729&view=diff
==============================================================================
--- deltaspike/site/trunk/content/core.mdtext (original)
+++ deltaspike/site/trunk/content/core.mdtext Mon Aug 18 20:52:35 2014
@@ -820,67 +820,67 @@ If you want to create a custom CDI scope
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 {}
+ :::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 {
+ :::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());
- }
- }
+ 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 {
+ :::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;
- }
- }
+ // 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;
- }
+ // Determines if the context object is active.
+ public boolean isActive() {
+ return true;
+ }
- }
+ }
## Deactivatable