2

when I tried to inject request scoped bean in application scope bean I got the following error.

Method threw 'org.jboss.weld.contexts.ContextNotActiveException' exception. Cannot evaluate com.example.flow.txn.TxnMessageProcessor$Proxy$_$$_WeldClientProxy.toString()

Code Reference:

@ApplicationScoped
public class TxnMessageObserver {

    private static final Logger logger = LoggerFactory.getLogger(TxnMessageObserver.class);

    @Inject
    private TxnMessageProcessor processor;
//message is observed here

  public void postConstruct(@Observes @Initialized(ApplicationScoped.class) Object o) {
        logger.info("Subscribing to queue [{}] for msg.", queue);
        consumer.subscribe(queue);
    }



}


@RequestScoped
public class TxnMessageProcessor {

    private static final Logger logger = LoggerFactory.getLogger(TxnMessageProcessor.class);
//all processing happens here

}

I need to process every message in request scope.

  • Does this answer your question? [Bean instance of a shorter scope injected in a bean instance of a larger scope in CDI - how does it work?](https://stackoverflow.com/questions/6577054/bean-instance-of-a-shorter-scope-injected-in-a-bean-instance-of-a-larger-scope-i) – Kukeltje Jul 11 '20 at 17:14
  • Any feedback? I did not downvote (yet) but please read http://idownvotedbecau.se/beingunresponsive (I noticed you asked a related question, so feeback on the answers here (even after your 'chameleonization') is very much appreciated (and corteous) – Kukeltje Jul 15 '20 at 07:20

2 Answers2

1

I do not know what exactly the problem cause is. But I can confirm that you can inject @ReqeustScoped beant into @ApplicationScoped. I do that in many applications with hundreds of classes and it worked out of the box.

Stefan
  • 1,789
  • 1
  • 11
  • 16
1

If the applicationscoped bean is constructed eagerly when the servlet context is initialized (like is the case here), there is no request context and hence no requestscoped bean.

Since it is completely unclear what you try to achieve (your code is not a minimal reproducible example, let me point you to

(All found via this search)

Injecting a requestscoped bean is therefor dangerous and I strongly suggest to retrieve the required requestscoped bean in the specific methods or do it the other way around, inject the applicationscoped bean in the requestscoped one and call a method on it, passing in itself.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47