codeconsole opened a new pull request, #15979:
URL: https://github.com/apache/grails-core/pull/15979

   ### Problem
   
   #15934 retimed plugin `doWithSpring` to run before Spring Boot 
auto-configuration — and therefore before any bean is created. An application 
class using the long-documented externalized-configuration pattern:
   
   ```groovy
   class Application extends GrailsAutoConfiguration implements 
EnvironmentAware {
       void setEnvironment(Environment environment) {
           // fetch secrets / discover endpoints, addFirst a property source
       }
   }
   ```
   
   previously had `setEnvironment` invoked (when the application configuration 
bean was instantiated) *before* plugin bean registration ran. After the 
retiming, plugins that resolve settings inside `doWithSpring` read 
configuration the application has not customized yet. The GORM plugins resolve 
their connection URLs there — e.g. `MongodbGrailsPlugin.doWithSpring()` builds 
`MongoDbDataStoreSpringInitializer` from `config` at definition time — so an 
app that injects its production MongoDB URL from a secret manager in 
`setEnvironment` silently boots against the static yaml default (`localhost`) 
instead. Beans that read the `Environment` at creation time (Redis, 
Elasticsearch, `@Value`) are unaffected, which makes the failure look like a 
Mongo-specific mystery. This is a real-world regression observed in production 
on the re-staged 8.0.0-M3.
   
   ### Fix
   
   Replay the contract at the start of 
`GrailsEarlyPluginRegistrationPostProcessor`: for each stashed application 
source class that is a `GrailsAutoConfiguration`/`GrailsApplicationClass` and 
implements `EnvironmentAware`, invoke `setEnvironment` on a detached instance — 
the same approach the phase already uses for artefact scanning 
(`scanApplicationSource`) — before plugin loading, the `PropertySourcesConfig` 
snapshot, and the `doWithSpring` drain.
   
   The real application bean still receives the standard `EnvironmentAware` 
callback when Spring creates it later, so implementations must be idempotent: 
contribute a *named* property source (re-adding a source with the same name 
replaces the earlier one) rather than accumulate state. This is documented on 
the new method and asserted in the spec (`INVOCATIONS == 2`, no duplicated 
source).
   
   ### Testing
   
   New test in `EarlyPluginRegistrationOrderingSpec`: a plugin that reads 
config in `doWithSpring` (as the GORM plugins do) observes the value 
contributed by the application's `setEnvironment`, not the default; the 
double-callback semantics are pinned. Full `:grails-core:test` and checkstyle 
pass.
   
   Happy to add an upgrade-notes entry in grails-doc ("`setEnvironment` on 
`EnvironmentAware` application classes now runs twice; keep it idempotent") if 
wanted.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to