Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:54:59+00:00 2026-05-23T16:54:59+00:00

I have a CDI producer method which – depending on some conditions not relevant

  • 0

I have a CDI producer method which – depending on some conditions not relevant to this example – creates objects of different types:

public class TestProducer {

  @Produces @TestQualifier
  public Object create(InjectionPoint ip) {
    if(something) {
      return "a String";
    } else {
      return Integer.valueOf(42);
    }
  }

but when using this producer, I always get an error in the followin situation:

@Named("test")
public class TestComponent {
   ...
   @Inject public void setA(@TestQualifier String stringValue) {
   ...
   @Inject public void setB(@TestQualifier Integer integerValue) {

It only works when the create method of the producer has the expected type in the method signature:

public class TestProducer {

  @Produces @SpringBean
  public String create(InjectionPoint ip) {

Now the String get’s injected correctly, but I have no way to also generate an integer from the producer method. But this is exactly what I want to avoid, since the producer itself should be completely generic.

Am I doing something wrong or is there no way to achieve the behaviour I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:54:59+00:00Added an answer on May 23, 2026 at 4:54 pm

    All CDI documentation makes it clear that CDI does typesafe dependency injection – and it is an exalted property of CDI. IMHO, what you are trying to do is just what CDI tries to avoid. You want the container to cast Object to each type and CDI does not work that way.

    The injections points stringValue and integerValue can only receive a bean which has java.lang.String and java.lang.Integer in its list of bean types respectively. java.lang.Object does not satisfy this criterion.

    I have two suggestions. First, since you have two or more injection points of different types, create two or more producer methods for that types:

    public class TestProducer {
    
      @Produces @TestQualifier
      public String createString(InjectionPoint ip) {
        if(something) {
          return "a String";
        } else {
          // Some other value
        }
      }
    
      @Produces @TestQualifier
      public int createInt(InjectionPoint ip) {
        if(something) {
          return 42;
        } else {
          // Some other value
        }
      }
    // ...
    

    It works if the something condition is just to check the type of the injection point (what I am betting is the case).

    However, if the something condition does decide the type using other criteria than the type of the injection point, I’d suggestion to do the “dirty job” yourself: inject the returned value in an Object-typed injection point and does the cast manually:

    @Named("test")
    public class TestComponent {
       ...
       @Inject public void setA(@TestQualifier Object value) {
           String stringValue = (String) value;
    
       ...
       @Inject public void setB(@TestQualifier Object value) {
           int intValue = (Integer) value;
    

    The main point is that, unlike some other DI frameworks, CDI does not work against the Java type system – on the contrary, it heavily uses it. Do not try to fight against it but use this aspect of CDI in your favor 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some beans for which, in specific injections, I want to add a
I have several components which are application scoped. Depending on which environment I am
I would like to have my own contexts for some CDI-based projects. I need
Have not done this before, so obviously I suck at it. Here 64 pixels
Have some dates in my local Oracle 11g database that are in this format:
In CDI, how do I configure my beans? Let's say I have this code:
I have a Stateless Session Bean (java ee 6, cdi) which throw events @Stateless
In my application, I have an @ApplicationScoped CDI bean to store some information from
I have already one session scoped CDI bean, which keeps currently logged in user
Following this tutorial http://netbeans.org/kb/docs/javaee/maven-osgiservice-cdi.html I have managed to create a simple OSGI bundle and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.