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

  • Home
  • SEARCH
  • 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 8712817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:11:20+00:00 2026-06-13T05:11:20+00:00

Example interface IA { public void someFunction(); } @Resource(name=b) class B implements IA {

  • 0

Example

interface IA
{
  public void someFunction();
}

@Resource(name="b")
class B implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someBfunc()
  {
     //doing b things
  }
}

@Resource(name="c")
class C implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someCfunc()
  {
     //doing C things
  }
}

class MyRunner
{

  @Autowire
  @Qualifier("b") 
  IA worker;

  worker.someFunction();
}

Can someone explain this to me.

  • How does spring know which polymorphic type to use.
  • Do I need @Qualifier or @Resource?
  • Why do we autowire the interface and not the implemented class?
  • 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-06-13T05:11:21+00:00Added an answer on June 13, 2026 at 5:11 am

    How does spring know which polymorphic type to use.

    As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring’s component scan enabled, Spring framework can find out the (interface, implementation) pair. If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).

    Do I need @Qualifier or @Resource?

    Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation. If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.

    Why do we autowire the interface and not the implemented class?

    Firstly, it is always a good practice to code to interfaces in general. Secondly, in case of spring, you can inject any implementation at runtime. A typical use case is to inject mock implementation during testing stage.

    interface IA
    {
      public void someFunction();
    }
    
    
    class B implements IA
    {
      public void someFunction()
      {
        //busy code block
      }
      public void someBfunc()
      {
         //doing b things
      }
    }
    
    
    class C implements IA
    {
      public void someFunction()
      {
        //busy code block
      }
      public void someCfunc()
      {
         //doing C things
      }
    }
    
    class MyRunner
    {
         @Autowire
         @Qualifier("b") 
         IA worker;
    
         ....
         worker.someFunction();
    }
    

    Your bean configuration should look like this:

    <bean id="b" class="B" />
    <bean id="c" class="C" />
    <bean id="runner" class="MyRunner" />
    

    Alternatively, if you enabled component scan on the package where these are present, then you should qualify each class with @Component as follows:

    interface IA
    {
      public void someFunction();
    }
    
    @Component(value="b")
    class B implements IA
    {
      public void someFunction()
      {
        //busy code block
      }
      public void someBfunc()
      {
         //doing b things
      }
    }
    
    
    @Component(value="c")
    class C implements IA
    {
      public void someFunction()
      {
        //busy code block
      }
      public void someCfunc()
      {
         //doing C things
      }
    }
    
    @Component    
    class MyRunner
    {
         @Autowire
         @Qualifier("b") 
         IA worker;
    
         ....
         worker.someFunction();
    }
    

    Then worker in MyRunner will be injected with an instance of type B.

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

Sidebar

Related Questions

For example public interface X{ public void foo(X i); } public class Y implements
From code how can we know which interfaces one class implements? Example: interface IDrink
Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Lets look at the following example: public class BothPaintAndPrintable implements Paintable,Printable{ public void print()
The code is also at https://github.com/timp21337/java-generics-example package a; import java.util.List; public interface Container<T> {
Consider the following example @Remote public interface RegistrationService { public String register(); public void
I have a class that implements an interface. In another area of the code
IDsmCore.java (interface class) public interface IDsmCore { public void Initialize( String path, String fileName
Consider an example (which compiles in java) public abstract interface Interface { public void
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>

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.