Manual injection of producer data

Question

I am working with support of legacy data and some multi-functional implementations. I am able to easily inject fully managed bean instances using basic code such as this:

InitialContext initialContext = new InitialContext();
BeanManager beanManager = (BeanManager) initialContext.lookup(BEAN_MANAGER_JNDI_NAME);
Set<Bean<?>> beans = beanManager.getBeans(beanClass);
Bean bean = beanManager.getBeans(beanClass).iterator().next();
CreationalContext creationalContext = beanManager.createCreationalContext(bean);
T managedInstance = (T) beanManager.getReference(bean, beanClass, creationalContext);

However, in the case of wanting to inject data from @Producer implementations, it does not work.

Using the classic example:

@Produces
@LoggerType
public Logger getLogger (InjectionPoint) { ... }

In my class this does not get resolved:

@Inject
@LoggerType
Logger log;

My workaround is just getting a completely managed instance of the class I am working in and then copying things in the following manner:

MyType managedInstance = CdiUtilities.getManagedInstance(getClass());
this.log = managedInstance.log;

How would I accomplish this? Is there something I need to do to get the @Producer data registered?

Details

I'm deploying on Wildly using:

  • jboss-javaee-6.0-with-tools (1.0.0.M1)
  • javax.servlet-api (3.1.0)
  • javax cdi-api
  • jboss-annotations-api_1.1_spec
  • jboss-ejb-api_3.1_spec

Qualifier is pretty pathetically simple:

@Qualifier
@Retention (RetentionPolicy.RUNTIME)
@Target ({ElementType.FIELD, ElementType.METHOD})
public @interface LoggerType { }

Answers


Let's take things in the order.

You're deploying to WildFly, so your target is Java EE 7 and CDI 1.1 (WildFly 8.0.0 and 8.1.0) or CDI 1.2 (WildFly 8.2.0).

In all case you can simplify your lookup code like this:

    BeanManager beanManager = CDI.current().getBeanManager();
    Bean bean = beanManager.resolve(beanManager.getBeans(beanClass));
    CreationalContext creationalContext = beanManager.createCreationalContext(bean);
    T managedInstance = (T) beanManager.getReference(bean, beanClass, creationalContext);

You don't give a lot of information of how you use this code. Just keep in mind that there might be nicer way of integrating your legacy code than this one...

Regarding your producer issue. I see 4 possible reason for this problem.

  1. you are using @javax.ws.rs.Produces instead of @javax.enterprise.inject.Produces. Check the import section of the class file containing your producer.

  2. You are in an implicit bean archive or the bean discovery mode of your archive is annotated and the bean containing your producer don't have a bean defining annotation. Add @Dependent on the class that contains the producer.

  3. The class containing your producer doesn't meet the conditions to be a bean. Check that you can inject this bean.

  4. Your war contains Java EE jars provided by WildFly and this cause unpredictable behaviour. Check your WEB-INF/lib to see if one of the lib you mentioned above wasn't packaged with your app (they should be scoped as provided in Maven).

BTW you should use Java EE 7 dependency to compile your code since you're deploying to WildFly.


Need Your Help

How do I establish multiple random numbers in a single Arduino sketch?

for-loop random arduino

I have an Arduino sketch with which I'm planning to control 8 LEDs to blink or fade at different rates. I want to set up a random number from 1-8random for my output pin, a random number from 30-30...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.