apache / apache/grails-core

Using @ComponentScan and multiple packages

Open
#13,861 4 comments 0 reactions 0 assignees View on GitHub
status: awaiting feedback
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Expected Behavior

We have some Domains in the src/main/groovy directory, in order to bundle Module code together in a prettier way. For this, we use the annotations `@Entity` and `@Artefact("domain")` which works great, as long as we put them in the default application package. However, if we another package, and use `@ComponentScan(basePackages = ["package1", "package2"])` the domains are not recognized in the second package, even with the same annotations.

### Actual Behaviour

Instead of a functional Entity, when calling the list() command on an Entity from package2, i get this error:

`Caused by: java.lang.IllegalStateException: Either class [pms.Foo] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity. `

2024-11-19_09-39-44

### Steps To Reproduce

I've added a sample project to this issue. It includes the packages "dila" and "pms". There are two Entities, Foo and Test, of which Test loads fine, but Foo doesnt. To test this, I've added a list command for each in the TestController which can be accessed under http://localhost:8080/test/listFoo and http://localhost:8080/test/listTest

grails-app/init/dila/Application.groovy:
```@ComponentScan(basePackages = ["dila", "pms"])
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
```
src/main/groovy/dila/Test.groovy

```
package dila

import grails.artefact.Artefact
import grails.gorm.annotation.Entity
import grails.validation.Validateable

@Entity
@Artefact("Domain")
class Test implements Validateable {
String name
}
```

src/main/groovy/pms/Foo.groovy

```
package pms

import grails.artefact.Artefact
import grails.gorm.annotation.Entity
import grails.validation.Validateable

@Entity
@Artefact("Domain")
class Foo implements Validateable {
String name
}
```

TestController.groovy
```
package dila

import grails.converters.JSON
import pms.Foo

class TestController {

def listFoo() {
render Foo.list() as JSON
}

def listTest() {
render Test.list() as JSON
}
}
```

[sample_project_multipackage.zip](https://github.com/user-attachments/files/17812123/sample_project_multipackage.zip)

### Environment Information

- Operating System: Mac 14.5 (23F79)
- Chip: Apple M2 Pro
- Java 11
- grailsVersion=6.2.0
- grailsGradlePluginVersion=6.1.2

### Example Application

_No response_

### Version

6.2.0

Contributor guide

Open the contributing guide

Research direction

Start by running the sample project and comparing /test/listFoo with /test/listTest. Read grails-app/init/dila/Application.groovy, src/main/groovy/dila/Test.groovy, src/main/groovy/pms/Foo.groovy, and TestController.groovy to trace component scanning and domain discovery. Done means the Foo entity in package pms is recognized and /test/listFoo works like /test/listTest.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.