openwebbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Andraschko <andraschko.tho...@gmail.com>
Subject Re: Lookup bean using beanManager returning null
Date Thu, 12 Jun 2014 12:34:15 GMT
You need to a pass a AnnotationLiteral instance instead of
"type.getAnnotation(yourAnnotation.class)"
Here it's explained how to create one and use one:
http://deltaspike.apache.org/core.html#beanprovider



2014-06-12 14:26 GMT+02:00 Gerhard Petracek <gerhard.petracek@gmail.com>:

> @felipe:
> we won't respond something different (see [1]), if you keep the wrong code.
> the one case you mentioned just works, because #getAnnotation returns null.
>
> regards,
> gerhard
>
> [1] http://s.apache.org/oDb
>
>
>
> 2014-06-12 14:16 GMT+02:00 Thomas Andraschko <andraschko.thomas@gmail.com>
> :
>
> Have a look at the bean manager java doc ->
>>
>> *getBeans
>> <http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html#getBeans%28java.lang.reflect.Type,%20java.lang.annotation.Annotation...%29>*(java.lang.reflect.Type
beanType,
>> java.lang.annotation.Annotation... qualifiers)
>>
>> RequestScoped isn't a CDI qualifier! ;)
>>
>>
>> 2014-06-12 14:03 GMT+02:00 Felipe Pina <fop.net@gmail.com>:
>>
>> Questions
>>>
>>>
>>>    1. Is there any schema that uses in the beans.xml file that
>>>    configures the engine to evaluate candidates beans through a package path?
>>>    2. the fact that the bean be packaged inside a jar which in turn,
>>>    within a war, is impediment to the proper functioning of the injection of
>>>    bean?
>>>
>>>
>>>
>>> *Tests*
>>>
>>>    1. lookup bean from string value
>>>    1. lookup "teste" -> works fine
>>>       2. lookup "perm" -> *fail returning null*
>>>    2. lookup bean from type
>>>       1. lookup using Permissao.class -> * fail returning null*
>>>
>>>
>>>
>>> *Project Structure*
>>>
>>> *WAR*
>>> *  |- WebProject*
>>> *            |- com.company.bean.JSFBean  -> **@Named(value="teste")*
>>> *  |-  BusinessProject*
>>> *             |- com.company.bean.Permissao -> *
>>> *@Named(value="perm") @BusinessBean*
>>>
>>> *My Qualifier*
>>>
>>>  @Qualifier
>>> @Retention(RUNTIME)
>>> @Target({TYPE, METHOD, FIELD, PARAMETER})
>>> public @interface BusinessBean {
>>>
>>> }
>>>
>>> *My bean.*
>>>
>>> @javax.enterprise.context.RequestScoped
>>> @javax.enterprise.inject.Default
>>> public class Permissao extends NegocioBase<PermissaoDTO> {
>>> ....
>>> }
>>>
>>>
>>> *context.xml*
>>>  <Resource name="BeanManager"
>>>         auth="Container"
>>>         type="javax.enterprise.inject.spi.BeanManager"
>>>         factory="org.apache.webbeans.container.ManagerObjectFactory"/>
>>>
>>>
>>> *web.xml*
>>>
>>> <!-- I ran with and without this resource-env-ref section -->
>>>   <resource-env-ref>
>>>     <description>Object factory for the CDI Bean Manager</description>
>>>     <resource-env-ref-name>BeanManager</resource-env-ref-name>
>>>
>>> <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
>>>   </resource-env-ref>
>>>
>>> *BeanFactory*
>>>
>>> public class BeanFactory {
>>>  static BeanManager getBeanManager() {
>>>      InitialContext context;
>>>     Object result;
>>>     try {
>>>          context = new InitialContext();
>>>         result = context.lookup("java:comp/env/BeanManager"); //lookup
>>> in Tomcat
>>>      } catch (NamingException e) {
>>>         try {
>>>             context = new InitialContext();
>>>              result = context.lookup("java:comp/BeanManager"); //lookup
>>> in JBossAS
>>>         } catch (NamingException ex) {
>>>              throw new RuntimeException("BeanManager could not be found
>>> in JNDI", e);
>>>         }
>>>     }
>>>      return (BeanManager) result;
>>> }
>>>  @SuppressWarnings("unchecked")
>>>  public static <T> T getContextualInstance(final Class<T> type) {
>>> BeanManager manager = getBeanManager();
>>>      T result = null;
>>> *Set<Bean<?>> beans = manager.getBeans(type,
>>> type.getAnnotation(RequestScoped.class));*
>>> *---- >  why variable beans isEmpty !!!*
>>> Bean<T> bean = (Bean<T>) manager.resolve(beans);
>>>     if (bean != null) {
>>>          CreationalContext<T> context =
>>> manager.createCreationalContext(bean);
>>>         if (context != null) {
>>>              result = (T) manager.getReference(bean, type, context);
>>>         }
>>>     }
>>>      return result;
>>> }
>>> }
>>>
>>> --
>>> Atenciosamente,
>>> Felipe Pina
>>>
>>
>>
>

Mime
View raw message