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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:57:57+00:00 2026-05-21T23:57:57+00:00

I am trying to inject a service dependency in the constructor for another service.(see

  • 0

I am trying to inject a service dependency in the constructor for another service.(see example code below). However, a NoSuchBeanDefinitionException is throws when I try to assign the api variable, even if the bean variable is set correctly.

It could also be worth to mention that this occur when I extend this service and initializing the child service.

My question is then, how could I proper inject the dependency service into my class dependent on the setting in my Config.groovy?

class MyService {
def api

public MyService() {
def beanString = ConfigurationHolder.config.api
def bean =  ApplicationHolder.application.getMainContext().getBean(GrailsNameUtils.getPropertyName(beanString))
this.api = bean
}
}

EDIT: To clarify my idea. I have two API:s, one XML and one JSON that serves the same data, but with different representations. However, I want to be able to set the api to use in the configuration file, eg a string with the api-name.

My idea is to have a common interface for the available apis (ApiInterface) and then when my service that uses the api is initialized, set an instance variable to the configured api.

In addition to this, I want to be able to have service specific settings i.e. a setting for each service that injects the api in the configuration file that sets which api to use.
e.g.


Config.groovy

firstService { api = "xml" } secondService { api = "json" }

  • 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-21T23:57:58+00:00Added an answer on May 21, 2026 at 11:57 pm

    I doubt you can get this to work without a lot of trouble since the instance is created by Spring when building the beans, so the other bean probably isn’t available yet. You could look it up lazily on first request though:

    class MyService {
    
       def grailsApplication
    
       private apiBean // use different name so 'api' resolves as property to getApi()
    
       def someMethod() {
          def foo = api.someMethodCall()
          ...
       }
    
       def someOtherMethod() {
          def bar = api.someOtherMethodCall()
          ...
       }
    
       private getApi() {
          if (apiBean == null) {
             String beanString = grailsApplication.configuration.api
             apiBean =  grailsApplication.mainContext.getBean(
                   GrailsNameUtils.getPropertyName(beanString))
          }
          apiBean
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using FluentValidation and Ninject. I am trying to inject a service into
I am trying to inject dependency to my session dictionary class into my controller's
I'm trying to inject a dependency to my MVC controllers like this private static
I have a service that is hosted in WAS. I am trying to inject
I am trying to use Spring to inject @PersistenceContext entityManager into my service. The
I'm trying to implement dependency injection for a WCF service hosted in an Azure
I am trying to inject a DAO into an CXF Service. For this purpose
Hi i am trying to inject a dictionary of interfaces but am getting an
I am trying to inject inside repeaters items, the problem is that the row
I'm trying to inject a view using Ajax but it's not quite working. I

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.