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 6726151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:53:34+00:00 2026-05-26T09:53:34+00:00

From reading the Weld docs I don’t think this can be done, but I

  • 0

From reading the Weld docs I don’t think this can be done, but I could really do with it, so thought I’d ask – if there’s not a way with CDI, maybe there’s a pattern workaround…

I’ve created a custom qualifier with a member:

@Qualifier
@Target({TYPE, METHOD, FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface JobBinding {
    JobType value();
}

JobType is an enum:

public enum JobType {

    JOB_A,
    JOB_B,
// etc - there are quite a few

Say most jobs need building in a slightly different way, so I have builder classes related to the jobs. These are annotated with a JobBinding annotation given the relevant JobType:

@JobBinding(JobType.JOB_A)
public class JobABuilder implements JobBuilder {
....

When I need to build, I use programmatic lookup:

@Inject @Any
private Instance<JobBuilder> builderSource;
private JobType myJobType;
...
builderSource.select(new JobBindingQualifier(myJobType).get();

JobBindingQualifier is a custom class:

public class JobBindingQualifier extends AnnotationLiteral<JobBinding> implements JobBinding {

    private static final long serialVersionUID = -822150300665931157L;

    private JobType type;

    public JobBindingQualifier(JobType type) {
        this.type = type;
    }

    @Override
    public JobType value() {
        return type;
   }
}

So far, great – CDI working brilliantly. However, what if 2 of these jobs, JOB_X and JOB_Y, are built in exactly the same way? I only need one builder class, which I’d like to be instantiated for either of those options – new JobBindingQualifier(JobType.JOB_X) or new JobBindingQualifier(JobType.JOB_Y).

If I annotate JobXAndYBuilder with both @JobBinding(JOB_X) and @JobBinding(JOB_Y), I get a compiler error about the duplicated annotation. To get around this I could change the value of the annotation to an array of JobTypes, and you would annotate the builder like

@JobBinding(JobType.JOB_X, JobType.JOB_Y)

with the constructor called there using an ellipsis to produce the array. However, if I did that, how could I look that up programmatically using either of the jobTypes? Weld docs suggest that you would have to have both; I’d need to provide the exact arguments:

builderSource.select(new JobBindingQualifier(JobType.JOB_X, JobType.JOB_Y).get();

when I want either to be sufficient to lookup the class:

builderSource.select(new JobBindingQualifier(JobType.JOB_X).get();
//or
builderSource.select(new JobBindingQualifier(JobType.JOB_Y).get();

Using the array really just changes the value that you have to match when looking up. I really need a way of annotating a class twice with the same qualifier annotation, and then being able to look it up with any combination of them. Otherwise I’ll have to provide a builder class each for X and Y, when one would suffice. Any ideas? Thanks in advance!

  • 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-26T09:53:35+00:00Added an answer on May 26, 2026 at 9:53 am

    As I said in my comment there is no straight way to have an OR relation when you have two or more qualifiers on a bean class.

    So the solution to have the same Bean with another qualifier is to use the Producer mechanism. In your example you can create you first class as usual :

    @JobBinding(JobType.JOB_X)
    public class JobABuilder implements JobBuilder {
        ....
    }
    

    and after that create a producer method in either in the first class or in a dedicated producers class like that

    public class MoreJobsProducer {
        @Produces
        @JobBinding(JobType.JOB_Y)
        protected JobBuilder prodJobYBuilder(@New @JobBinding(JobType.JOB_X)
                                             JobBuilder theJob) {
            return theJob;
        }    
    }
    

    In the paramaters of the producer method you Inject your former bean with its own qualifier and the @Newqualifier which create a new instance of the bean to be sure you avoid some scoping issues (read the Weld doc for more info).

    It should do the “Job”.

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

Sidebar

Related Questions

Can anyone point me in the right direction on this. From reading the FAQs
Seems like this might be easy, but from reading other SO answers there are
From what I can tell from reading everything I can find here, this doesn't
From reading the docs I can tell that allowForceBuild will turn off the force
Ok, I can't figure this out from reading Perl's documentation. I'm looking at the
I know SL 3 didn't have this but from reading about SL 4, it
From reading the Apple Docs on Core Data, I've learned that you should not
From reading the RFC it appears that CID can/must only contain characters from the
I've tried everything from reading the Netbeans help to browsing Google. This code works
I am starting to learn AspectJ. From reading tutorials, I know that I can

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.