Reputation: 81
I am experiencing a problem with a service class implementing the InitializingBean interface, so implementing the afterPropertiesSet method. The service is then injected in a controller
In my case the afterPropertiesSet method references a domain class calling .list method
In 2.0.4 everything goes well: the afterPropertiesSet is called at the first controller action invocation and is correctly executed
In 2.4.2 (also in 2.3) the afterPropertiesSet is invoked at application startup, and fails with an exception
java.lang.IllegalStateException: Method on class [XXXXXX] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
Upvotes: 0
Views: 117
Reputation: 81
As clarified by Graeme Rocher in this JIRA:
this is caused by the fact that in 2.3.x and above the default controller scope is singleton whilst in 2.0.x the default scope was prototype. The difference here is that in 2.3.x the controller is created on startup whilst in earlier versions it is created only on first request.
Since dynamic methods are only registered after the Spring context is initialised you are in a kind of half way state when your afterPropertySet method is called.
You can however go back to the previous behaviour by making your controller prototype again using:
static scope = "prototype"
In the controller class.
More info about controller scope (and the change to default scope since grails 2.3) can be found in the grails documentation
Upvotes: 2