1818package io .microsphere .spring .context ;
1919
2020import io .microsphere .logging .Logger ;
21+ import io .microsphere .spring .beans .BeanUtils ;
22+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
2123import org .springframework .context .ApplicationContextInitializer ;
2224import org .springframework .context .ConfigurableApplicationContext ;
2325import org .springframework .core .env .ConfigurableEnvironment ;
2426
2527import static io .microsphere .constants .PropertyConstants .ENABLED_PROPERTY_NAME ;
2628import static io .microsphere .constants .SymbolConstants .DOT ;
2729import static io .microsphere .logging .LoggerFactory .getLogger ;
30+ import static io .microsphere .spring .beans .BeanUtils .generateBeanName ;
31+ import static io .microsphere .spring .beans .factory .support .BeanRegistrar .registerBean ;
2832import static io .microsphere .spring .constants .PropertyConstants .APPLICATION_CONTEXT_INITIALIZER_PROPERTY_NAME_PREFIX ;
2933
3034/**
31- * Abstract class of {@link ApplicationContextInitializer} for {@link ConfigurableApplicationContext}, which can be
32- * enabled or disabled by configuration properties.
35+ * An abstract base class for {@link ApplicationContextInitializer} implementations targeting {@link ConfigurableApplicationContext}.
36+ * It provides built-in support for enabling or disabling the initializer via configuration properties,
37+ * as well as automatic self-registration into the Spring context.
38+ *
39+ * <h3>Example Usage</h3>
40+ * <pre>{@code
41+ * public class MyCustomInitializer extends ConfigurableApplicationContextInitializer {
42+ *
43+ * @Override
44+ * protected void initialize(ConfigurableApplicationContext context, ConfigurableEnvironment environment) {
45+ * // Perform custom initialization logic here
46+ * }
47+ * }
48+ * }</pre>
49+ *
50+ * <p>By default, this initializer is enabled. To disable it, set the corresponding property to {@code false}:
51+ * <pre>
52+ * microsphere.spring.context-initializer.myCustomInitializer.enabled=false
53+ * </pre>
3354 *
3455 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
3556 * @see ApplicationContextInitializer
@@ -41,15 +62,23 @@ public abstract class ConfigurableApplicationContextInitializer implements Appli
4162
4263 protected final Logger logger = getLogger (getClass ());
4364
65+ private String beanName ;
66+
4467 @ Override
4568 public final void initialize (ConfigurableApplicationContext context ) {
4669 ConfigurableEnvironment environment = context .getEnvironment ();
47- if (isEnabled (context , environment )) {
48- initialize (context , environment );
70+ if (!isEnabled (context , environment )) {
71+ logger .info ("The {} was disabled, if it needs to be enabled, please set the property '{}' to 'true' ." ,
72+ getClass (), getEnabledPropertyName ());
73+ return ;
74+ }
75+ if (isRegistered (context )) {
76+ logger .info ("Current {} Bean[name : '{}'] is already registered to the Spring Context[id : '{}'] , so it will not be initialized." ,
77+ getClass (), getBeanName (), context .getId ());
4978 return ;
5079 }
51- logger . info ( "The {} was disabled, if it needs to be enabled, please set the property '{}' to 'true' ." ,
52- getClass (), getEnabledPropertyName () );
80+ initialize ( context , environment );
81+ registerSelf ( context );
5382 }
5483
5584 /**
@@ -66,6 +95,8 @@ public final void initialize(ConfigurableApplicationContext context) {
6695 * @param context the {@link ConfigurableApplicationContext}
6796 * @param environment the {@link ConfigurableEnvironment}
6897 * @return if enabled, return <code>true</code>, or <code>false</code>
98+ * @see #getEnabledPropertyName()
99+ * @see #getDefaultEnabled()
69100 */
70101 protected boolean isEnabled (ConfigurableApplicationContext context , ConfigurableEnvironment environment ) {
71102 String propertyName = getEnabledPropertyName ();
@@ -76,10 +107,10 @@ protected boolean isEnabled(ConfigurableApplicationContext context, Configurable
76107 * Get the property name of the enabled
77108 *
78109 * @return the property name of the enabled
110+ * @see #getBeanName()
79111 */
80112 protected String getEnabledPropertyName () {
81- String simpleClassName = getClass ().getSimpleName ();
82- return APPLICATION_CONTEXT_INITIALIZER_PROPERTY_NAME_PREFIX + simpleClassName + DOT + ENABLED_PROPERTY_NAME ;
113+ return APPLICATION_CONTEXT_INITIALIZER_PROPERTY_NAME_PREFIX + getBeanName () + DOT + ENABLED_PROPERTY_NAME ;
83114 }
84115
85116 /**
@@ -91,4 +122,39 @@ protected String getEnabledPropertyName() {
91122 protected boolean getDefaultEnabled () {
92123 return true ;
93124 }
125+
126+ /**
127+ * Get the bean name of this {@link ConfigurableApplicationContextInitializer}
128+ *
129+ * @return the bean name of this {@link ConfigurableApplicationContextInitializer}
130+ * @see BeanUtils#generateBeanName(Class)
131+ */
132+ protected String getBeanName () {
133+ String beanName = this .beanName ;
134+ if (beanName == null ) {
135+ beanName = generateBeanName (getClass ());
136+ this .beanName = beanName ;
137+ }
138+ return beanName ;
139+ }
140+
141+ /**
142+ * Is registered or not
143+ *
144+ * @param context the {@link ConfigurableApplicationContext}
145+ * @return if registered, return <code>true</code>, or <code>false</code>
146+ */
147+ protected boolean isRegistered (ConfigurableApplicationContext context ) {
148+ return context .containsBean (getBeanName ());
149+ }
150+
151+ /**
152+ * Register self to the {@link ConfigurableApplicationContext}
153+ *
154+ * @param context the {@link ConfigurableApplicationContext}
155+ */
156+ protected void registerSelf (ConfigurableApplicationContext context ) {
157+ ConfigurableListableBeanFactory beanFactory = context .getBeanFactory ();
158+ registerBean (beanFactory , getBeanName (), this );
159+ }
94160}
0 commit comments