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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:14:43+00:00 2026-06-07T15:14:43+00:00

My application has a interface as below. public interface MainInterface { void someMethod(); }

  • 0

My application has a interface as below.

public interface MainInterface
{
     void someMethod();
}

Then, i have number of implementations of this interface.

@Service    
public class ImplClass1 implements MainInterface
{
   @Override
   public void someMehtod()
   {
      //Execution of code
   }
}

@Service    
public class ImplClass2 implements MainInterface
{
   @Override
   public void someMehtod()
   {
      //Execution of code
   }
}

@Service
public class ImplClass3 implements MainInterface
{
   @Override
   public void someMehtod()
   {
      //Execution of code
   }
}

Below is a controller.

@Controller
public class MainController
{
     MainInterface implObj;

     @RequestMapping("service1")
     public void Service1Handler()
     {
         //Replace below with @Autowire
         implObj = new ImplClass1();
     }

     @RequestMapping("service2")
     public void Service1Handler()
     {
         //Replace below with @Autowire
         implObj = new ImplClass2();
     }

     @RequestMapping("service3")
     public void Service1Handler()
     {
         //Replace below with @Autowire
         implObj = new ImplClass3();
     }
}

As mentioned in comment of each method i want to initialize it using spring.
This is just an example. In my real time application i have 12 implementations of a interface and 6 methods in a controller.

Can you please guide how can i use autowire feature at method level or suggest any other best way around.

Thanks

  • 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-07T15:14:45+00:00Added an answer on June 7, 2026 at 3:14 pm

    Can think of these two ways –

    @Controller
    public class MainController
    {
         @Autowired @Qualifier("impl1") MainInterface impl1;
         @Autowired @Qualifier("impl2") MainInterface impl2;
         @Autowired @Qualifier("impl3") MainInterface impl3;
    
         @RequestMapping("service1")
         public void service1Handler()
         {
              impl1.doSomething()
         }
    
         @RequestMapping("service2")
         public void Service1Handler()
         {
             //Replace below with @Autowire
              impl2.doSomething()
         }
    
         @RequestMapping("service3")
         public void Service1Handler()
         {
             //Replace below with @Autowire
               impl3.doSomething()
         }
    }
    

    OR hide it behind a factory:

    class MaintenanceInterfaceFactory{
         @Autowired @Qualifier("impl1") MainInterface impl1;
         @Autowired @Qualifier("impl2") MainInterface impl2;
         @Autowired @Qualifier("impl3") MainInterface impl3;
         getImplForService(String name){
            //return one of the impls above based on say service name..
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which has a similar interface to Visual Studio, in that
Our Java application has a number of modules which implement a common interface. By
My application has multiple SQLite tables(around 20 with different fields). This I have created
I have a C#/WPF application with a tabbed interface that has been behaving strangely.
My Application has 3 activities (MainActivity,SampleActivity,TempActivity) and Application have to start from MainActivity because
The application has been put in iCloud since the beginning, so I have the
Our application has a service layer and a DAO layer, written as Spring beans.
I have a cocoa application that has a finder like feel it and is
This specific class's member private fields which are annotated @autowired are within a service
I have an application that has many views (Using tab bar controller). I want

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.