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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:05:33+00:00 2026-05-28T05:05:33+00:00

I have two implementations of interface IFoo : Foo1 and Foo2 . I need

  • 0

I have two implementations of interface IFoo: Foo1 and Foo2.

I need the context to be able to inject the correct one for any class that wants an IFoo without that class knowing what implementation it’s going to get (so no use of @Qualifier).

Example of usage:

class Bar { 
  @Autowired IFoo foo;
}

I have a FactoryBean<IFoo> class that has the logic for which one to return.

The issue is that I also want to have those two impls go through the IOC because they have dependencies themselves.

public class FooFactory implements FactoryBean<Foo> {
  @Autowired Foo1 foo1;
  @Autowired Foo2 foo2;

  @Override
  public IFoo getObject() throws Exception {
    if(someLogic()){
        return foo1;
    }
    return foo2;
   }
} 

If I autowire them into the factory I get an exception of

"No unique bean of type IFoo is defined: expected single matching bean but found 3: [fooFactory, foo1, foo2]"

Any way to tell spring to just use the default of fooFactory for everyone else but use the two implementations inside the factory?

  • 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-28T05:05:34+00:00Added an answer on May 28, 2026 at 5:05 am

    You could use the notion of the “primary” bean. Assuming that you’re using XML config, then:

    <bean id="fooFactory" class="x.y.FooFactory" primary="true"/>
    

    Now, @Autowired IFoo foo should select the result of the factory, in preference to the foo1 and foo2 beans.

    I would also recommend using @Resource inside the factory itself, rather than @Autowired, i.e.

    public class FooFactory implements FactoryBean<Foo> {
      @Resource Foo1 foo1;
      @Resource Foo2 foo2;
    

    This reduces the chance of the autowiring chasing its own tail, since @Resource will inject a specific, named bean (i.e. foo1 and foo2). You’d still use @Autowired in Bar.

    If you use @Resource like that, then an alternative to use primary="true" is to exclude the foo1 and foo2 beans from autowiring, e.g.

    <bean id="foo1" class="x.y.Foo1" autowire-candidate="false"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two implementations of a method, one for value types and another for
I have an abstract base class which acts as an interface. I have two
I have an interface. public interface ISomeInterface {...} and two implementations (SomeImpl1 and SomeImpl2):
We have two different implementations of the same interface. Think of it as a
I have an interface, called IRepository, with two implementations: SqlRepository SqlDualWriterRepository The first implementation
Imagine you have two implementations of a @Local interface @Local public interface LocalInterface {
I have two properties declared for my class in its interface file Account.h @interface
in the implementations class charged of displaying PINS, i have reserved two variables (title
I have two identical tables and need to copy rows from table to another.
I have two basic interfaces as below, one inheriting the other. I am binding

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.