Skip to content

Commit

Permalink
Singleton as the defualt scope of Controllers
Browse files Browse the repository at this point in the history
* Singleton has been the defualt scope (via setting) for quite a while, but the fallback defaultValue was prototype.
* Profiles should be updated too to remove this setting

Closes gh-467
  • Loading branch information
rainboyan committed Aug 14, 2024
1 parent 7c6d159 commit 0dec386
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2022 the original author or authors.
* Copyright 2004-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,7 +86,7 @@ public void initialize() {
public void setGrailsApplication(GrailsApplication grailsApplication) {
this.grailsApplication = grailsApplication;
if (this.scope == null) {
this.scope = grailsApplication.getConfig().getProperty(Settings.CONTROLLERS_DEFAULT_SCOPE, "prototype");
this.scope = grailsApplication.getConfig().getProperty(Settings.CONTROLLERS_DEFAULT_SCOPE, SCOPE_SINGLETON);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class DefaultGrailsControllerClassSpec extends Specification {
def controllerClass = new DefaultGrailsControllerClass(NotSpecifiedController)
controllerClass.setGrailsApplication(new DefaultGrailsApplication())

expect: "the default scope is prototype"
controllerClass.getScope() == PROTOTYPE
!controllerClass.isSingleton()
expect: "the default scope is singleton"
controllerClass.getScope() == SINGLETON
controllerClass.isSingleton()
}

void "test getScope when scope is specified on the controller, and not specified in config"() {
Expand All @@ -45,14 +45,14 @@ class DefaultGrailsControllerClassSpec extends Specification {

void "test getScope when scope is specified both in the controller and config"() {
given:
def controllerClass = new DefaultGrailsControllerClass(SingletonController)
def controllerClass = new DefaultGrailsControllerClass(PrototypeController)
def grailsApplication = new DefaultGrailsApplication()
grailsApplication.getConfig().put(Settings.CONTROLLERS_DEFAULT_SCOPE, PROTOTYPE)
controllerClass.setGrailsApplication(grailsApplication)

expect: "controller's setting to have priority"
controllerClass.getScope() == SINGLETON
controllerClass.isSingleton()
controllerClass.getScope() == PROTOTYPE
!controllerClass.isSingleton()
}

class NotSpecifiedController {
Expand All @@ -61,4 +61,8 @@ class DefaultGrailsControllerClassSpec extends Specification {
class SingletonController {
static scope = "singleton"
}

class PrototypeController {
static scope = "prototype"
}
}

0 comments on commit 0dec386

Please sign in to comment.