45663

CDI ambiguous dependency when adding @Named qualifier to existing bean via @Produces

Question:

I have a CDI bean implementation in a dependency jar file:

@ApplicationScoped public class MyService { public String doSomething() {...} }

In my webapp, I want to access that service via EL Expression, therefore I have to give it a @Named annotation. But I cannot add the annotation on the MyService implementation because I don't have the rights to change that code.

Therefore I tried creating a producer like

public class MyServiceProducer { @Inject MyService myService; @Produces @Named("myService") public MyService produceNamedInstance() { return myService; } }

This results in a

<blockquote>

WELD-001409 - ambiguous dependency for type MyService with qualifiers @Default ... Possible dependencies: - Managed Bean [class ...MyService] with qualifiers [@Any @Default] - Producer Method [myService] with qualifiers [@Default @Named @Any] declared as [...]

</blockquote>

How can I add a @Named annotation without touching the original source code?

Answer1:

The error is referring to the @Inject MyService. You basically defined a second bean via @Produces MyService which is <em>also</em> injectable as MyService, but you didn't make clear which one exactly you meant to inject via @Inject. So CDI got confused and throws this ambiguous dependency error.

Instead of creating another producer for an already auto-produced bean, you should just extend the existing bean and then name it.

@Named("myService") public class MyNamedService extends MyService { // }

Noted should be that the <a href="http://docs.oracle.com/javaee/7/api/javax/enterprise/context/ApplicationScoped.html" rel="nofollow">scope</a> is already <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html" rel="nofollow">@Inherited</a>, so you don't need to re-define it.

Recommend

  • Efficiently add numbers divisible by 3 or 5
  • Export Sharepoint Excel Sheet to SQL using SSIS
  • What is the Response.Redirect equivalent in Silverlight?
  • PHP & MySQL: Why is this working without lock?
  • Can I have a model called “Transaction” in Rails?
  • What happens if I use extern “C++” with a C toolchain?
  • Entity Framework error when submitting empty fields
  • Using the mongo C# driver, how to serialize an array of custom object in order to store it?
  • “error MSB8020: The build tools for v141 (Platform Toolset = 'v141') cannot be found” when
  • search bar getting disappeared in ios UIsearchcontroller
  • Qt fresh Gui application crashes on startup, faulting at QtCore4.dll
  • How to filter out rows from spark dataframe containing unreadable characters
  • AppEngine Paypal integration giving SSLCertificateError on localhost, using Python
  • Creating and managing two independent random number sequences
  • Bulk loading into PostgreSQL from a remote client
  • On unchecking a checkbox trigger an event
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • How to move to lines with the same indentation in Visual Studio Code
  • Yii Bootstrap not loading JS files
  • How do I set the logging properties in a spring java configuration?
  • Showing image on a acro text field position
  • Unable to run testNG tests from maven
  • ASP.NET MVC 2 actions for the same route?
  • Possible to set default CloudKit container not based on application name?
  • concise way of flattening multiindex columns
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • Error handeling in antlr 3.0
  • Terminal run dalvikvm with am.jar
  • Change cell value based on cell color in google spreadsheet
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to handle div that is created dynamically in a table
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Make checkout phone field optional for specific countries in WooCommerce
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined