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

Grails 5.0.0.M2: property inside plugin has no effect on environment configuration (extension to #11519) #11767

Closed
2 of 4 tasks
davidkron opened this issue Mar 19, 2021 · 1 comment · Fixed by #11828 or #13388
Closed
2 of 4 tasks

Comments

@davidkron
Copy link
Contributor

davidkron commented Mar 19, 2021

This is an extension to the already closed issue #11519

The example which I described in my initial issue where a call to environment.getProperty('grails.databinding.dateFormats') inside BootStrap doesn't return the values from plugin.yml does work now.

But I discovered another use-case where it still doesn't work:
My example of configuring some default framework parameters like grails.databinding.dateFormats inside a "core"-plugin still doesn't work. The concrete problem in the databinding case seems to be, that the configuration is now implemented as a Micronaut @ConfigurationProperties bean:

@ConfigurationProperties("grails.databinding")
public class DataBindingConfigurationProperties {

And to me it seems that the configuration from the plugins gets added to the Micronaut context very late:

ConfigurableApplicationContext applicationContext = super.run(args)
Environment environment = Environment.getCurrent()
log.info("Application starting in environment: {}", environment.getName())
log.debug("Application directory discovered as: {}", IOUtils.findApplicationDirectory())
log.debug("Current base directory is [{}]. Reloading base directory is [{}]", new File("."), BuildSettings.BASE_DIR)
loadPluginConfigurationsToMicronautContext(applicationContext)

At this point the application context is already created.

Edit:
What also doesn't seem to work: when I add a custom Micronaut PropertySourceLoader the properties loaded with it are not available in the Grails application by grailsApplication.config.

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. create a grails plugin
  2. override a default grails property (e.g. grails.databinding.dateFormats) inside the plugin's grails-app/config/plugin.yml file with a non-default value of something like "dd.MM.yyyy"
  3. create a grails application
  4. add plugin dependency to app
  5. try databinding of a "LocalDate" property with a value of "19.03.2021"

Expected Behaviour

Configuration properties from plugins should be loaded already in the Micronaut context.

Actual Behaviour

Configuration is loaded when Micronaut context is already setup.

Environment Information

  • Operating System: Windows 10
  • Grails Version: 4.0.9
  • JDK Version: 11
@davidkron
Copy link
Contributor Author

So as I am currently trying to implement a workaround to make our app work, I experimented with the following extension to MicronautEnvironment, so the Spring context also receives the property sources from Micronaut:

@Replaces(MicronautEnvironment.class)
@Singleton
@Primary
@CompileStatic
class ExtendedMicronautEnvironment extends MicronautEnvironment {

    private static final Set<String> EXCLUDED_SOURCES_NAMES = Set.of(
        SystemPropertiesPropertySource.NAME,
        EnvironmentPropertySource.NAME,
        'grails-config'
    )

    ExtendedMicronautEnvironment(Environment environment) {
        super(environment)
    }

    @Override
    MutablePropertySources getPropertySources() {
        MutablePropertySources propertySources = super.getPropertySources()
        environment.propertySources
            .findAll { !EXCLUDED_SOURCES_NAMES.contains(it.name) }
            .each {
                propertySources.addLast(new MicronautPropertySource(it.name, it))
            }
        return propertySources
    }
}

During this I had to debug a lot through Spring and Micronaut and I discovered the following, which I found kinda odd and also prevents my workaround above from working:

I placed a breakpoint inside the constructor of the class org.springframework.core.env.AbstractEnvironment and found out that the Spring environment is actually created 2 times.

The first time on the line:
https://github.com/spring-projects/spring-boot/blob/fdf3b18697b171f4e184dbd8ced4461d0114cf22/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L339
The second time on this line:
https://github.com/spring-projects/spring-framework/blob/965eee7bbfc454a6e5b304de254822743248c47c/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java#L71

So the merge of the micronaut property sources (from my workaround) with the Spring context should happen at this line:

applicationContext.setParent(
parentContext
)

But at the time of execution, the active environment is actually the second instance that was created. Then after the parent context is set, the following is executed:
https://github.com/spring-projects/spring-boot/blob/fdf3b18697b171f4e184dbd8ced4461d0114cf22/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L363-L365

These lines finally set the environment to the first instance that was created in SpringApplication.

From a theoretical standpoint I would expect there would be created exactly one Spring environment and one Micronaut environment, so this behavior seems rather strange to me.

Is this expected to work like that?

@davidkron davidkron changed the title Grails 4: property inside plugin has no effect on environment configuration (extension to #11519) Grails 5.0.0.M2: property inside plugin has no effect on environment configuration (extension to #11519) Jul 22, 2021
puneetbehl added a commit that referenced this issue Sep 24, 2023
Fixing configuration loading with Micronaut (issue #11767)
matrei added a commit to matrei/grails-functional-tests that referenced this issue Feb 19, 2024
matrei added a commit to matrei/grails-core that referenced this issue Feb 19, 2024
This commit changes the behavior of the GrailsApplicationPostProcessor
so that it no longer creates a new plugin manager if a plugin manager
already exists in the application context.

Resolves grails#11767
Related grails/grails-functional-tests#208
puneetbehl pushed a commit that referenced this issue Feb 20, 2024
This commit changes the behavior of the GrailsApplicationPostProcessor
so that it no longer creates a new plugin manager if a plugin manager
already exists in the application context.

Resolves #11767
Related grails/grails-functional-tests#208
puneetbehl pushed a commit to grails/grails-functional-tests that referenced this issue Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant