Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using @ComponentScan and multiple packages #13861

Open
dularion opened this issue Nov 19, 2024 · 4 comments
Open

Using @ComponentScan and multiple packages #13861

dularion opened this issue Nov 19, 2024 · 4 comments

Comments

@dularion
Copy link

dularion commented Nov 19, 2024

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:

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

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

@jeffscottbrown
Copy link
Member

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.

Is @ComponentScan relevant to discovering domain classes in Grails 6.2.0?

@dularion
Copy link
Author

it is, if they are placed in /src/main/groovy instead of /grails-app/domains

@jeffscottbrown
Copy link
Member

it is, if they are placed in /src/main/groovy instead of /grails-app/domains

Thank you!

@jdaugherty
Copy link
Contributor

jdaugherty commented Nov 26, 2024

Does @AutoConfigurationPackage work instead of @ComponentScan?

https://stackoverflow.com/questions/45343594/why-autoconfigurationpackages-not-consider-componentscan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants