Skip to content

Commit

Permalink
Auto-configure GrailsApplication and GrailsPluginManager
Browse files Browse the repository at this point in the history
Closes gh-857
  • Loading branch information
rainboyan committed Dec 30, 2024
1 parent 2951b59 commit 441a133
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,8 +28,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
Expand Down Expand Up @@ -57,7 +55,6 @@
import grails.core.DefaultGrailsApplication;
import grails.core.GrailsApplication;
import grails.core.GrailsApplicationLifeCycle;
import grails.plugins.DefaultGrailsPluginManager;
import grails.plugins.GrailsPlugin;
import grails.plugins.GrailsPluginManager;
import grails.spring.BeanBuilder;
Expand Down Expand Up @@ -95,9 +92,9 @@ public class GrailsApplicationPostProcessor

protected final Logger log = LoggerFactory.getLogger(getClass());

protected final GrailsApplication grailsApplication;
protected GrailsApplication grailsApplication;

protected final GrailsPluginManager pluginManager;
protected GrailsPluginManager pluginManager;

protected final GrailsApplicationEventListener grailsApplicationEventListener;

Expand All @@ -115,9 +112,7 @@ public class GrailsApplicationPostProcessor
private ApplicationStartup applicationStartup = ApplicationStartup.DEFAULT;

public GrailsApplicationPostProcessor() {
this.grailsApplication = new DefaultGrailsApplication();
this.grailsApplicationEventListener = new GrailsApplicationEventListener();
this.pluginManager = new DefaultGrailsPluginManager(this.grailsApplication);
}

public void setGrailsApplicationLifeCycle(GrailsApplicationLifeCycle lifeCycle) {
Expand All @@ -132,6 +127,8 @@ public void setLoadExternalBeans(boolean loadExternalBeans) {
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (this.applicationContext != applicationContext) {
this.applicationContext = applicationContext;
this.grailsApplication = applicationContext.getBean(GrailsApplication.class);
this.pluginManager = applicationContext.getBean(GrailsPluginManager.class);
initializeGrailsApplication(applicationContext);
if (applicationContext instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext configurable = (ConfigurableApplicationContext) applicationContext;
Expand Down Expand Up @@ -253,7 +250,9 @@ public Object convert(NavigableMap.NullSafeNavigator source) {
if (conversionService != null) {
config.setConversionService(conversionService);
}
((DefaultGrailsApplication) this.grailsApplication).setConfig(config);
if (this.grailsApplication instanceof DefaultGrailsApplication) {
((DefaultGrailsApplication) this.grailsApplication).setConfig(config);
}
}
configStep.end();
}
Expand Down Expand Up @@ -334,17 +333,6 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) t

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
if (parentBeanFactory instanceof ConfigurableBeanFactory) {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) parentBeanFactory;
configurableBeanFactory.registerSingleton(GrailsApplication.APPLICATION_ID, this.grailsApplication);
configurableBeanFactory.registerSingleton(GrailsPluginManager.BEAN_NAME, this.pluginManager);
}
else {
beanFactory.registerSingleton(GrailsApplication.APPLICATION_ID, this.grailsApplication);
beanFactory.registerSingleton(GrailsPluginManager.BEAN_NAME, this.pluginManager);
}

beanFactory.addBeanPostProcessor(new GrailsApplicationAwareBeanPostProcessor(this.grailsApplication));
beanFactory.addBeanPostProcessor(new PluginManagerAwareBeanPostProcessor(this.pluginManager));
}
Expand All @@ -353,7 +341,6 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
public void setApplicationStartup(ApplicationStartup applicationStartup) {
Assert.notNull(applicationStartup, "applicationStartup should not be null");
this.applicationStartup = applicationStartup;
this.pluginManager.setApplicationStartup(applicationStartup);
this.grailsApplicationEventListener.setApplicationStartup(applicationStartup);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 the original author or authors.
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,12 +19,18 @@
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable;

import grails.core.DefaultGrailsApplication;
import grails.core.GrailsApplication;
import grails.plugins.DefaultGrailsPluginManager;
import grails.plugins.GrailsPluginManager;

/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
* Auto-configuration} for Grails.
Expand All @@ -35,10 +41,21 @@
* @see GrailsApplicationPostProcessor
*/
@AutoConfiguration
@AutoConfigureOrder(10000)
@Import(GrailsAutoConfiguration.GrailsRegistrar.class)
public class GrailsAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public GrailsApplication grailsApplication() {
return new DefaultGrailsApplication();
}

@Bean
@ConditionalOnMissingBean
public GrailsPluginManager pluginManager(GrailsApplication grailsApplication) {
return new DefaultGrailsPluginManager(grailsApplication);
}

static class GrailsRegistrar implements ImportBeanDefinitionRegistrar {

@Override
Expand Down

0 comments on commit 441a133

Please sign in to comment.