Skip to content

Commit

Permalink
Merge pull request #950 from gsartori/7.0.x
Browse files Browse the repository at this point in the history
BootStrap init servletContext cleanup
  • Loading branch information
jdaugherty authored Feb 19, 2025
2 parents 56a2a56 + ea81d9d commit 144ab3a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/en/guide/REST/domainResources.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You can try it out by adding some test data to `BootStrap.groovy`:

[source,groovy]
----
def init = { servletContext ->
def init = {
new Book(title:"The Stand").save()
new Book(title:"The Shining").save()
}
Expand Down
10 changes: 6 additions & 4 deletions src/en/guide/conf/environments.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,18 @@ It's often desirable to run code when your application starts up on a per-enviro

[source,groovy]
----
def init = { ServletContext ctx ->
ServletContext servletContext
def init = {
environments {
production {
ctx.setAttribute("env", "prod")
servletContext.setAttribute("env", "prod")
}
development {
ctx.setAttribute("env", "dev")
servletContext.setAttribute("env", "dev")
}
}
ctx.setAttribute("foo", "bar")
servletContext.setAttribute("foo", "bar")
}
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AuthorService {
----
class AuthorController {
def authorService
AuthorService authorService
def updateAge() {
try {
Expand Down Expand Up @@ -110,7 +110,7 @@ In this case a new request will deal with retrieving the `Author` again. And, fi
----
class AuthorController {
def authorService
AuthorService authorService
def updateAge() {
try {
Expand Down Expand Up @@ -154,7 +154,7 @@ import grails.validation.ValidationException
class AuthorController {
def authorService
AuthorService authorService
def updateAge() {
try {
Expand Down
31 changes: 12 additions & 19 deletions src/en/guide/services/dependencyInjectionServices.adoc
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

==== Dependency Injection Basics


A key aspect of Grails services is the ability to use http://www.springframework.org/[Spring Framework]'s dependency injection features. Grails supports "dependency injection by convention". In other words, you can use the property name representation of the class name of a service to automatically inject them into controllers, tag libraries, and so on.

As an example, given a service called `BookService`, if you define a property called `bookService` in a controller as follows:
As an example, given a service called `BookService`, you can define a property called `bookService` in a controller as follows:

[source,groovy]
----
class BookController {
def bookService
BookService bookService
...
}
----

In this case, the Spring container will automatically inject an instance of that service based on its configured scope. All dependency injection is done by name. You can also specify the type as follows:
In this case, the Spring container will automatically inject an instance of that service based on its configured scope. Since all dependency injection is done by name you can also specify the type as follows:

[source,groovy]
----
Expand All @@ -33,56 +32,51 @@ NOTE: Only the top level object is subjected to injection as traversing all nest

Be careful when injecting the non-default datasources. For example, using this config:

[source,groovy]
[source,yaml]
----
dataSources:
dataSource:
pooled: true
jmxExport: true
.....
...
secondary:
pooled: true
jmxExport: true
.....
...
----

You can inject the primary `dataSource` like you would expect:

[source,java]
[source,groovy]
----
class BookSqlService {
def dataSource
DataSource dataSource
}
----

But to inject the `secondary` datasource, you have to use Spring's `Autowired` injection or `resources.groovy`.

[source,java]
[source,groovy]
----
class BookSqlSecondaryService {
@Autowired
@Qualifier('dataSource_secondary')
def dataSource2
DataSource dataSource2
}
----



==== Dependency Injection and Services


You can inject services in other services with the same technique. If you had an `AuthorService` that needed to use the `BookService`, declaring the `AuthorService` as follows would allow that:

[source,groovy]
----
class AuthorService {
def bookService
BookService bookService
}
----


==== Dependency Injection and Domain Classes / Tag Libraries

You can even inject services into domain classes and tag libraries, which can aid in the development of rich domain models and views:
Expand All @@ -91,7 +85,7 @@ You can even inject services into domain classes and tag libraries, which can ai
----
class Book {
...
def bookService
BookService bookService
def buyBook() {
bookService.buyBook(this)
Expand All @@ -103,7 +97,6 @@ WARNING: Since Grails 3.2.8 this is not enabled by default. If you want to enabl

==== Service Bean Names


The default bean name which is associated with a service can be problematic if there are multiple services with the same name defined in different packages. For example consider the situation where an application defines a service class named `com.demo.ReportingService` and the application uses a plugin named `ReportingUtilities` and that plugin provides a service class named `com.reporting.util.ReportingService`.

The default bean name for each of those would be `reportingService` so they would conflict with each other. Grails manages this by changing the default bean name for services provided by plugins by prefixing the bean name with the plugin name.
Expand Down
2 changes: 1 addition & 1 deletion src/en/guide/spring/springdslAdditional.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ package my.company
class MyBeanImpl {
Integer someProperty
String otherProperty
BookService bookService // or just "def bookService"
BookService bookService
}
----

Expand Down
9 changes: 4 additions & 5 deletions src/en/ref/Database Mapping/column.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ Given the next code:
----
package demo
import groovy.transform.CompileStatic
@CompileStatic
class BootStrap {
def init = { servletContext ->
def init = {
Book.saveAll(
new Book(name: 'Grails 3 - Step by Step',
status: Status.NOT_SET),
Expand All @@ -94,7 +91,9 @@ class BootStrap {
status: Status.SOLD),
)
}
def destroy = { }
def destroy = {
}
}
----

Expand Down

0 comments on commit 144ab3a

Please sign in to comment.