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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:13:28+00:00 2026-06-15T23:13:28+00:00

How can I initialize a static variable with a value defined in config.groovy ?

  • 0

How can I initialize a static variable with a value defined in config.groovy?

Currently I have something like this:

class ApiService {
    JSON get(String path) {
        def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
        ...
    }
    JSON get(String path, String token) {
        def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
        ...
    }
    ...
    JSON post(String path, String token) {
        def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
        ...
    }
}

I don’t want to define the http variable inside each method (several GET, POST, PUT and DELETE).

I want to have the http variable as a static variable inside the service.

I tried this without success:

class ApiService {

    static grailsApplication
    static http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")

    JSON get(String path) {
        http.get(...)
        ...
    }
}

I get Cannot get property 'config' on null object. The same with:

class ApiService {

    def grailsApplication
    static http

    ApiService() {
        super()
        http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
    }

    JSON get(String path) {
        http.get(...)
        ...
    }
}

Also I tried without a static definition, but the same error Cannot get property 'config' on null object:

class ApiService {

    def grailsApplication
    def http

    ApiService() {
        super()
        http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
    }
}

Any clue?

  • 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-15T23:13:30+00:00Added an answer on June 15, 2026 at 11:13 pm

    Rather than a static, use an instance property (as service beans are singleton scoped). You can’t do the initialization in the constructor, as dependencies haven’t yet been injected, but you can use a method annotated @PostConstruct, which will be called by the framework after dependency injection.

    import javax.annotation.PostConstruct
    
    class ApiService {
      def grailsApplication
      HTTPBuilder http
    
      @PostConstruct
      void init() {
        http = new HTTPBuilder(grailsApplication.config.grails.api.server.url)
      }
    
      // other methods as before
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We can initialize a container deque by using standard input like this: deque<int> c((istream_iterator<int>(cin)),(istream_iterator<int>()));
Usually, when I try to initialize a static variable class Test2 { public: static
In Java can a method have anything close to a static variable in C?Although
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
I like c#, but why can I do : public static bool Initialized {
In c# I can initialize a List at creation time like var list =
My class contains LOT_SIZE constant which can not be changed. But I can initialize
Can I initialize an IOLoop.instance and than fork new processes, which uses IOLoop.instance()? Something
I have a confusion in the concepts of static integer.When i initialize a static
I've got an activity, where I initialize some static variable (Menu of the app).

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.