1

I'm using Websphere Application Server 8.0.0.5 and I've observed the following issues while using CDI.

  1. Classes which have @Producer methods cannot have any scope other than @Dependent
  2. Any bean whose scope is other than @Dependent cannot be injected and used in classes with @Producer methods.

On doing any of the above task, I get:

[10/8/15 14:50:30:764 GMT+05:30] 00000019 InjectInjecti E   CWOWB0102E: A JCDI error has occurred: WebBeans context with scope type annotation @ApplicationScoped does not exist within current thread
[10/8/15 14:50:30:766 GMT+05:30] 00000019 BusinessExcep E   CNTR0019E: EJB threw an unexpected (non-declared) exception during invocation of method "doSomething". Exception data: javax.ejb.EJBException: Injection failure; nested exception is: javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @ApplicationScoped does not exist within current thread
javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @ApplicationScoped does not exist within current thread
        at org.apache.webbeans.container.BeanManagerImpl.getContext(BeanManagerImpl.java:321)
        at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:124)
        at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.invoke(NormalScopedBeanInterceptorHandler.java:95)

......

Sample code to replicate the issue:

public class Producer{

    @Inject
    @MyQualifier
    private Properties properties;

    @Produces
    public Logger getLogger(InjectionPoint ip){
        System.out.println(properties);
        System.out.println("Injection point is: "+ip);
        return Logger.getLogger(ip.getMember().getDeclaringClass().getName());
    }


}





public class AnotherProducer{

    @Produces
    @MyQualifier
    @ApplicationScoped
    public Properties getProperties(){

        Popperties p = new Properties();
        p.put("key","value");

        return p;
    }   
}

My question here is - Am I violating the CDI spec or there's something wrong with my Container?

ares
  • 4,283
  • 6
  • 32
  • 63

1 Answers1

1

According to CDI specification, scope of type @ApplicationScoped should be always available - usually it means that objects in this scope exist for the lifetime of your application.

But it seems that you came across a bug in Websphere, as discussed in here. If you want to use @ApplicationScoped beans, you have following options:

  • switch to a different Application server or upgrade Websphere, or at least upgrade WebBeans library in WebSphere (maybe not an option for you)
  • create a CDI extension to add your custom @ApplicationScoped scope (possible too much coding for something, which should be supported by the app server already)
  • do not use @ApplicationScoped on your producers, but instead cache created beans in a Singleton bean if they were created previously
Community
  • 1
  • 1
OndroMih
  • 7,280
  • 1
  • 26
  • 44