What are the best debugging tricks with Weld/CDI?

Thorbjørn Ravn Andersen picture Thorbjørn Ravn Andersen · Jan 31, 2011 · Viewed 8k times · Source

One of the beauties with Java EE 6 is the new dependency injection framework - CDI with the Weld reference implementation - which has prompted us to start migrating internally to JSR-330 in an implementation agnostic manner, with the explicit target of being able to have a core jar which is frozen, and then being able to add extra jars providing new modules replacing functionality in the core jar.

I am now in the process of making the above work with Weld, and to be frank there is simply too much magic going on behind the covers. Either it works or it doesn't, and it doesn't provide very much help by default on what happens so you can investigate what is wrong and fix it.

I would expect that there are switches to switch which can easily enable things like:

  • What classpath entries are scanned and where? What was the result?
  • What beans are available for injection for which class?
  • What caused a given bean not to be considered for later? A given jar?

In other words, I need to see the decision process in much more detail. For some reason this is not as needed with Guice, perhaps because there is much less magic, and perhaps because the error messages are very good.

What do you do to debug your Weld applications, and how much does it help?

Answer

jan groth picture jan groth · May 7, 2011

Short answer: there is no dedicated debug option for CDI (as no such thing is required by the spec), and no dedicated debug option for Weld.

Long Answer: There is a lot you can do on your own. Familiarise yourself with the extension mechanism of CDI, and you'll discover that you can easily (really!) write your own extension that debugs your required information

What classpath entries are scanned and where? What was the result?

Listen to the ProcessAnnotatedType-Event

What beans are available for injection for which class?

Query the BeanManager for that.

What caused a given bean not to be considered for later? A given jar?

Listen to the AfterBeanDiscovery-Event and see what you've got in the BeanManager. Basically, the following scenarios make a ManageBean ineligible for injection: