Commit da970e933179bcc5bedeb1144587188601631b9f
1 parent
4cef0dd9
Exists in
master
Ajuste na Serialização do CustomBean
Showing
1 changed file
with
33 additions
and
11 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java
| @@ -36,6 +36,7 @@ | @@ -36,6 +36,7 @@ | ||
| 36 | */ | 36 | */ |
| 37 | package br.gov.frameworkdemoiselle.internal.bootstrap; | 37 | package br.gov.frameworkdemoiselle.internal.bootstrap; |
| 38 | 38 | ||
| 39 | +import java.io.Serializable; | ||
| 39 | import java.lang.annotation.Annotation; | 40 | import java.lang.annotation.Annotation; |
| 40 | import java.lang.reflect.Type; | 41 | import java.lang.reflect.Type; |
| 41 | import java.util.HashSet; | 42 | import java.util.HashSet; |
| @@ -58,33 +59,54 @@ import javax.inject.Named; | @@ -58,33 +59,54 @@ import javax.inject.Named; | ||
| 58 | import javax.inject.Qualifier; | 59 | import javax.inject.Qualifier; |
| 59 | import javax.inject.Scope; | 60 | import javax.inject.Scope; |
| 60 | 61 | ||
| 62 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 63 | + | ||
| 61 | /** | 64 | /** |
| 62 | * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 | 65 | * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 |
| 63 | */ | 66 | */ |
| 64 | -public class CustomBean implements Bean<Object> { | 67 | +public class CustomBean implements Bean<Object>, Serializable { |
| 68 | + | ||
| 69 | + private static final long serialVersionUID = 1L; | ||
| 65 | 70 | ||
| 66 | private Class<Object> beanClass; | 71 | private Class<Object> beanClass; |
| 67 | 72 | ||
| 68 | - private InjectionTarget<Object> injectionTarget; | 73 | + private transient InjectionTarget<Object> injectionTarget; |
| 69 | 74 | ||
| 70 | - public CustomBean(Class<Object> beanClass, BeanManager beanManager) { | ||
| 71 | - AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(beanClass); | 75 | + private transient BeanManager beanManager; |
| 76 | + | ||
| 77 | + private InjectionTarget<Object> getInjectionTarget() { | ||
| 78 | + if (this.injectionTarget == null) { | ||
| 79 | + AnnotatedType<Object> annotatedType = getBeanManager().createAnnotatedType(beanClass); | ||
| 80 | + this.injectionTarget = getBeanManager().createInjectionTarget(annotatedType); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + return this.injectionTarget; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + private BeanManager getBeanManager() { | ||
| 87 | + if (this.beanManager == null) { | ||
| 88 | + this.beanManager = Beans.getBeanManager(); | ||
| 89 | + } | ||
| 72 | 90 | ||
| 73 | - this.injectionTarget = beanManager.createInjectionTarget(annotatedType); | 91 | + return this.beanManager; |
| 92 | + } | ||
| 93 | + | ||
| 94 | + public CustomBean(Class<Object> beanClass, BeanManager beanManager) { | ||
| 74 | this.beanClass = beanClass; | 95 | this.beanClass = beanClass; |
| 96 | + this.beanManager = beanManager; | ||
| 75 | } | 97 | } |
| 76 | 98 | ||
| 77 | public Object create(CreationalContext<Object> creationalContext) { | 99 | public Object create(CreationalContext<Object> creationalContext) { |
| 78 | - Object instance = injectionTarget.produce(creationalContext); | ||
| 79 | - injectionTarget.inject(instance, creationalContext); | ||
| 80 | - injectionTarget.postConstruct(instance); | 100 | + Object instance = getInjectionTarget().produce(creationalContext); |
| 101 | + getInjectionTarget().inject(instance, creationalContext); | ||
| 102 | + getInjectionTarget().postConstruct(instance); | ||
| 81 | 103 | ||
| 82 | return instance; | 104 | return instance; |
| 83 | } | 105 | } |
| 84 | 106 | ||
| 85 | public void destroy(Object instance, CreationalContext<Object> creationalContext) { | 107 | public void destroy(Object instance, CreationalContext<Object> creationalContext) { |
| 86 | - injectionTarget.preDestroy(instance); | ||
| 87 | - injectionTarget.dispose(instance); | 108 | + getInjectionTarget().preDestroy(instance); |
| 109 | + getInjectionTarget().dispose(instance); | ||
| 88 | creationalContext.release(); | 110 | creationalContext.release(); |
| 89 | } | 111 | } |
| 90 | 112 | ||
| @@ -166,6 +188,6 @@ public class CustomBean implements Bean<Object> { | @@ -166,6 +188,6 @@ public class CustomBean implements Bean<Object> { | ||
| 166 | } | 188 | } |
| 167 | 189 | ||
| 168 | public Set<InjectionPoint> getInjectionPoints() { | 190 | public Set<InjectionPoint> getInjectionPoints() { |
| 169 | - return injectionTarget.getInjectionPoints(); | 191 | + return getInjectionTarget().getInjectionPoints(); |
| 170 | } | 192 | } |
| 171 | } | 193 | } |