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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:07:20+00:00 2026-06-04T09:07:20+00:00

I have several services implementing a common interface and I want to be able

  • 0

I have several services implementing a common interface and I want to be able to choose one of them to inject into other services when my application starts up.

I have tried referencing the service implementation from resources.groovy as shown below but then Spring makes a new instance of the selected service and doesn’t autowire its dependencies.

How can I get this solution to work? Or is there another way?

class MyService {

    Repository repository

    interface Repository {
        void save(...)
    }
}

class MySqlRepositoryService implements MyService.Repository { ... }

class FileRepositoryService implements MyService.Repository { ... }

resources.groovy:

beans = {
    ...
    repository(FileRepositoryService) { }
}
  • 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-04T09:07:22+00:00Added an answer on June 4, 2026 at 9:07 am

    It’s of course possible to retrieve the reference to service from hand-built factory, but in my opinion, the approach you’ve taken is the best one. I use it myself, because it gathers all the information on configuration phase of the application in one place, so it’s easier to track down which implementation is used.

    The pitfall with autowiring that you’ve encountered can be explained very easily. All the classes put in grails-app/services are automatically configured by Grails as Spring singleton beans with autowiring by name. So the bean definition you’ve placed in grails-app/conf/resources.groovy creates another bean, but without the defaults imposed by Grails conventions.

    The most straightforward solution is to put the implementation in src/groovy to avoid duplication of beans and use the following syntax to turn on the autowiring:

    beans = {
      repository(FileRepositoryService) { bean ->
        bean.autowire = 'byName'
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a web application that integrates with several other APIs and services
I have several WCF services in an ASP.NET application. I want to prevent applications
I have several WCF services and I want to execute methods in them at
I have a spring application that uses several services such as MySQL, PostgreSQL, and
Scenario: I have several services that I want to be discovered by different clients.
I have several web services I need to create for them some testing mechanism
I have several WCF services. When I check them on the server (example: ServiceTest.svc?wsdl),
I have several WCF services that I have imported into a .Net4 Class Library.
I have written several services in Delphi now, but I want to add the
I have constructed several web services that successfully serialize my .NET types into JSON.

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.