0

I use weld+paxcdi on java se. I can get the reference to beanManager using

@Inject
BeanManager beanManager;

However, it's clear that I can use this way only in objects that are managed by CDI container. How can I get reference to beanmanager in object that was created by new?

  • Does CDI.current().getBeanManager() not work? Cf. https://blogs.oracle.com/arungupta/entry/beanmanager_obtain_contextual_reference_to – Hein Blöd May 29 '15 at 16:42
  • @Hein Blöd It throws Caused by: java.lang.IllegalStateException: Unable to access CDI at javax.enterprise.inject.spi.CDI.current(CDI.java:65) –  May 29 '15 at 16:57
  • I overlooked that you're on Java SE; did you follow the instructions in the [reference manual](https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_java_se) then? – Hein Blöd May 29 '15 at 17:03

1 Answers1

1

I assume the question refers to OSGi, as it wouldn't make sense to use Pax CDI otherwise.

Pax CDI creates a separate CDI container and thus a separate bean manager per bean bundle. For this reason, it is not quite obvious what the current bean manager should be.

As of Pax CDI 1.0.0.RC1, CDI.current() returns a meaningful value when called from an OsgiServiceProvider method, if the method target is an injection point in the calling class. CDI.current() is undefined otherwise.

Pax CDI 0.x is based on CDI 1.0 and thus does not support CDI.current().

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • You are right - this is osgi+cdi. Could you explain in details what to do in order to use CDI.current()? –  May 30 '15 at 16:05
  • Please, take a look at this question http://stackoverflow.com/questions/38871453/pax-cdi-how-to-get-reference-to-beanmanager-in-bundle-without-using-cdi – Pavel_K Aug 10 '16 at 10:58