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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:23:39+00:00 2026-05-20T11:23:39+00:00

I have a number of Grails Services that are singleton Spring beans, not prototypes.

  • 0

I have a number of Grails Services that are singleton Spring beans, not prototypes.

Is there any way to retrieve a prototype instance (fully injected with any dependencies) of what is normally a singleton bean?

I’d like this for testing purposes so that I can mess with the prototype instance (potentially changing it’s metaClass to mock things out) without the risk of forgetting to clean up after any changes I make to the instance and having them leak into other tests.

I’ve been playing around with this a little bit and haven’t had any luck. I’ve tried doing something like this:

def ctx = grailsApplication.mainContext

ctx.registerPrototype("foo", MyService)

I’m then able to ask Spring for an instance of MyService

def myServicePrototype = ctx.getBean("foo")

and it does give a new instance every time, but none of the instances have had their properties autowired up so they’re all null. I’m guessing theres some way to create a BeanDefinition and feed it to the BeanFactory with some of the autowire stuff that I must be missing turned on.

I’m hoping to come up with some generic solution that doesn’t force me to explicitly annotate the target services in any way.

Is there any way to directly ask the Spring applicationContext for a prototype version of what’s normally a singleton?

  • 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-20T11:23:40+00:00Added an answer on May 20, 2026 at 11:23 am

    Figured out how to do it using the Grails BeanBuilder to create a temporary ApplicationContext that has the main app context as a parent. This won’t work if any unusual wiring/spring configuration has been done to the service, but that’s not normally the case with Grails services, so seems like a good default pattern:

    // this works in the grails console where grailsApplication is in the binding, 
    // need to inject in other situations
    import org.springframework.context.ApplicationContext
    import grails.spring.BeanBuilder    
    import com.example.service.MyService
    
    def getPrototypeInstanceOf(Class clazz) {
        BeanBuilder beanBuilder = new BeanBuilder(grailsApplication.mainContext)
        String beanName = "prototype${clazz.name}"
    
        beanBuilder.beans {
            "$beanName"(clazz) { bean ->
                bean.autowire = 'byName'
            }
        }
    
        ApplicationContext applicationContext = beanBuilder.createApplicationContext()
    
        return applicationContext.getBean(beanName)
    }
    
    
    def prototypeService = getPrototypeInstanceOf(MyService)
    def singletonService = grailsApplication.mainContext.getBean("myService")
    
    assert singletonService != prototypeService
    assert singletonService.injectedService == prototypeService.injectedService
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of code value tables that contain a code and a
I have a number of tables that use the trigger/sequence column to simulate auto_increment
We have a number of projects that use the same and/or similar package names.
How do I create a grails war file so that it doesn't have the
I have the following simplified model in Grails: A DataBlock consists of a number
My Grails application has a large number of enums that look like this: public
I have number of tables in my database. There will be number of methods
I have a number of algorithms for community detection on graphs that I want
I have a number laptops that run our application while connected to each other
Say I have a number, double, or NSNumber, 3.333333 I want that to turn

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.