Skip to content

Commit 49be748

Browse files
authored
Merge pull request #263 from mercyblitz/dev-1.x
Refactor code, update README, and manage Spring initializers
2 parents db61398 + b7b5bae commit 49be748

11 files changed

Lines changed: 286 additions & 21 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Choose the version that matches your Spring Framework line:
9191

9292
| Branch | Spring Framework compatibility | Latest version |
9393
|--------|--------------------------------|----------------|
94-
| `main` | 6.0.x – 7.0.x | `0.2.31` |
95-
| `1.x` | 4.3.x – 5.3.x | `0.1.31` |
94+
| `main` | 6.0.x – 7.0.x | `0.2.32` |
95+
| `1.x` | 4.3.x – 5.3.x | `0.1.32` |
9696

9797
### 2. Add individual modules
9898

microsphere-spring-context/src/main/java/io/microsphere/spring/beans/factory/support/ListenableAutowireCandidateResolverInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ protected void initialize(ConfigurableApplicationContext context, ConfigurableEn
100100
}
101101

102102
@Override
103-
protected String getEnabledPropertyName() {
103+
public String getEnabledPropertyName() {
104104
return ENABLED_PROPERTY_NAME;
105105
}
106106

107107
@Override
108-
protected boolean getDefaultEnabled() {
108+
public boolean getDefaultEnabled() {
109109
return DEFAULT_ENABLED_PROPERTY_VALUE;
110110
}
111111
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.config.env;
19+
20+
import io.microsphere.annotation.Nonnull;
21+
import io.microsphere.logging.Logger;
22+
import org.springframework.core.env.Environment;
23+
24+
import static io.microsphere.constants.PropertyConstants.ENABLED_PROPERTY_NAME;
25+
import static io.microsphere.constants.SymbolConstants.DOT;
26+
import static io.microsphere.logging.LoggerFactory.getLogger;
27+
import static io.microsphere.spring.constants.PropertyConstants.MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX;
28+
import static io.microsphere.util.ClassUtils.getTypeName;
29+
30+
31+
/**
32+
* The template class for component that is enabled or disabled based on {@link Environment}.
33+
*
34+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
35+
* @see Environment
36+
* @since 1.0.0
37+
*/
38+
public interface EnvironmentEnabled {
39+
40+
/**
41+
* Checks if this component is enabled based on the given {@link Environment}.
42+
*
43+
* <p>This method determines the enabled status by looking up the property defined by
44+
* {@link #getEnabledPropertyName()} in the provided environment. If the property is not found,
45+
* it falls back to the default value specified by {@link #getDefaultEnabled()}.
46+
*
47+
* <b>Example Usage</b>
48+
* <pre>{@code
49+
* // Assuming this interface is implemented by a class named MyService.
50+
* // The default property name would be "microsphere.spring.MyService.enabled".
51+
*
52+
* Environment environment = ...; // Obtain Environment
53+
* MyService myService = new MyService();
54+
*
55+
* // Check if enabled (defaults to true if property is absent)
56+
* boolean enabled = myService.isEnabled(environment);
57+
*
58+
* // To disable, set the property "microsphere.spring.MyService.enabled=false"
59+
* // in your environment configuration.
60+
* }</pre>
61+
*
62+
* @param environment the Spring {@link Environment} to check against, must not be {@code null}
63+
* @return {@code true} if the component is enabled, {@code false} otherwise
64+
* @see #getEnabledPropertyName()
65+
* @see #getDefaultEnabled()
66+
*/
67+
default boolean isEnabled(@Nonnull Environment environment) {
68+
String enabledPropertyName = getEnabledPropertyName();
69+
boolean enabled = environment.getProperty(enabledPropertyName, boolean.class, getDefaultEnabled());
70+
Class<?> currentClass = getClass();
71+
Logger logger = getLogger(currentClass);
72+
if (enabled) {
73+
if (logger.isTraceEnabled()) {
74+
logger.trace("The {} is enabled, if it needs to be disabled[defalt : '{}'], please set the property '{}' to 'false' .",
75+
currentClass, getDefaultEnabled(), getEnabledPropertyName());
76+
}
77+
} else {
78+
if (logger.isInfoEnabled()) {
79+
logger.info("The {} is disabled, if it needs to be enabled[defalt : '{}'], please set the property '{}' to 'true' .",
80+
currentClass, getDefaultEnabled(), getEnabledPropertyName());
81+
}
82+
}
83+
return enabled;
84+
}
85+
86+
/**
87+
* Gets the property name used to determine if this component is enabled.
88+
*
89+
* <p>The default implementation constructs the property name by combining
90+
* the module prefix, the implementing class name, and the enabled suffix.
91+
*
92+
* <b>Example Usage</b>
93+
* <pre>{@code
94+
* // Assuming the implementing class is named {@code DataProvider}
95+
* // The resolved property name will be: "microsphere.spring.DataProvider.enabled"
96+
*
97+
* DataProvider provider = ...;
98+
* String key = provider.getEnabledPropertyName();
99+
* boolean active = environment.getProperty(key, boolean.class);
100+
* }</pre>
101+
*
102+
* @return the property name key for checking the enabled status
103+
*/
104+
default String getEnabledPropertyName() {
105+
String className = getTypeName(this);
106+
return MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX + className + DOT + ENABLED_PROPERTY_NAME;
107+
}
108+
109+
/**
110+
* Gets the default enabled status for this component.
111+
*
112+
* <p>This value is used as the fallback when the property defined by
113+
* {@link #getEnabledPropertyName()} is not present in the {@link Environment}.
114+
*
115+
* <b>Example Usage</b>
116+
* <pre>{@code
117+
* // Assuming the implementing class is named {@code MyService}
118+
* // And the property "microsphere.spring.MyService.enabled" is NOT set in the Environment.
119+
*
120+
* MyService service = ...;
121+
* boolean enabled = service.isEnabled(environment);
122+
* // enabled will be true (since getDefaultEnabled returns true).
123+
* }</pre>
124+
*
125+
* @return {@code true} if the component is enabled by default, {@code false} otherwise
126+
* @see #isEnabled(Environment)
127+
*/
128+
default boolean getDefaultEnabled() {
129+
return true;
130+
}
131+
}

microsphere-spring-context/src/main/java/io/microsphere/spring/context/ConfigurableApplicationContextInitializer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.microsphere.logging.Logger;
2121
import io.microsphere.spring.beans.BeanUtils;
22+
import io.microsphere.spring.config.env.EnvironmentEnabled;
2223
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2324
import org.springframework.context.ApplicationContextInitializer;
2425
import org.springframework.context.ConfigurableApplicationContext;
@@ -58,7 +59,8 @@
5859
* @see ConfigurableEnvironment
5960
* @since 1.0.0
6061
*/
61-
public abstract class ConfigurableApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
62+
public abstract class ConfigurableApplicationContextInitializer implements
63+
ApplicationContextInitializer<ConfigurableApplicationContext>, EnvironmentEnabled {
6264

6365
protected final Logger logger = getLogger(getClass());
6466

@@ -68,8 +70,6 @@ public abstract class ConfigurableApplicationContextInitializer implements Appli
6870
public final void initialize(ConfigurableApplicationContext context) {
6971
ConfigurableEnvironment environment = context.getEnvironment();
7072
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());
7373
return;
7474
}
7575
if (isRegistered(context)) {
@@ -99,8 +99,7 @@ public final void initialize(ConfigurableApplicationContext context) {
9999
* @see #getDefaultEnabled()
100100
*/
101101
protected boolean isEnabled(ConfigurableApplicationContext context, ConfigurableEnvironment environment) {
102-
String propertyName = getEnabledPropertyName();
103-
return environment.getProperty(propertyName, Boolean.class, getDefaultEnabled());
102+
return isEnabled(environment);
104103
}
105104

106105
/**
@@ -109,7 +108,7 @@ protected boolean isEnabled(ConfigurableApplicationContext context, Configurable
109108
* @return the property name of the enabled
110109
* @see #getBeanName()
111110
*/
112-
protected String getEnabledPropertyName() {
111+
public String getEnabledPropertyName() {
113112
return APPLICATION_CONTEXT_INITIALIZER_PROPERTY_NAME_PREFIX + getBeanName() + DOT + ENABLED_PROPERTY_NAME;
114113
}
115114

@@ -119,7 +118,7 @@ protected String getEnabledPropertyName() {
119118
* @return the default value of enabled
120119
* @see #getEnabledPropertyName()
121120
*/
122-
protected boolean getDefaultEnabled() {
121+
public boolean getDefaultEnabled() {
123122
return true;
124123
}
125124

microsphere-spring-context/src/main/java/io/microsphere/spring/context/annotation/AnnotatedBeanCapableImportBeanDefinitionRegistrar.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
import org.springframework.core.type.AnnotationMetadata;
2626

2727
import java.lang.annotation.Annotation;
28-
import java.util.Set;
29-
30-
import static io.microsphere.collection.SetUtils.newLinkedHashSet;
3128

3229
/**
3330
* An abstract base class for {@link ImportBeanDefinitionRegistrar} implementations that are driven by a specific
@@ -73,7 +70,6 @@ public abstract class AnnotatedBeanCapableImportBeanDefinitionRegistrar<A extend
7370
public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry,
7471
BeanNameGenerator importBeanNameGenerator) {
7572
if (isEnabled(metadata)) {
76-
Set<String> imports = newLinkedHashSet();
7773
ResolvablePlaceholderAnnotationAttributes<A> annotationAttributes = getAnnotationAttributes(metadata);
7874
registerBeanDefinitions(metadata, registry, importBeanNameGenerator, annotationAttributes);
7975
}

microsphere-spring-context/src/main/java/io/microsphere/spring/context/annotation/AutoRegistrationBeanInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ static class Config {
4747
}
4848

4949
@Override
50-
protected String getEnabledPropertyName() {
50+
public String getEnabledPropertyName() {
5151
return BEANS_AUTO_REGISTERED_PROEPRTY_NAME;
5252
}
5353

5454
@Override
55-
protected boolean getDefaultEnabled() {
55+
public boolean getDefaultEnabled() {
5656
return DEFAULT_AUTO_REGISTERED_VALUE;
5757
}
5858
}

microsphere-spring-context/src/main/java/io/microsphere/spring/context/event/EventPublishingBeanInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ public int getOrder() {
115115
}
116116

117117
@Override
118-
protected String getEnabledPropertyName() {
118+
public String getEnabledPropertyName() {
119119
return ENABLED_PROPERTY_NAME;
120120
}
121121

122122
@Override
123-
protected boolean getDefaultEnabled() {
123+
public boolean getDefaultEnabled() {
124124
return DEFAULT_ENABLED_PROPERTY_VALUE;
125125
}
126126
}

microsphere-spring-context/src/main/java/io/microsphere/spring/core/env/ListenableConfigurableEnvironmentInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ protected void initialize(ConfigurableApplicationContext context, ConfigurableEn
9595
}
9696

9797
@Override
98-
protected String getEnabledPropertyName() {
98+
public String getEnabledPropertyName() {
9999
return ENABLED_PROPERTY_NAME;
100100
}
101101

102102
@Override
103-
protected boolean getDefaultEnabled() {
103+
public boolean getDefaultEnabled() {
104104
return DEFAULT_ENABLED_PROPERTY_VALUE;
105105
}
106106
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.config.env;
19+
20+
import org.springframework.core.env.Environment;
21+
22+
/**
23+
* {@link EnvironmentEnabled} Implementation
24+
*
25+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
26+
* @see EnvironmentEnabled
27+
* @since 1.0.0
28+
*/
29+
public class EnvironmentEnabledImpl implements EnvironmentEnabled {
30+
31+
@Override
32+
public boolean isEnabled(Environment environment) {
33+
return EnvironmentEnabled.super.isEnabled(environment);
34+
}
35+
36+
@Override
37+
public String getEnabledPropertyName() {
38+
return EnvironmentEnabled.super.getEnabledPropertyName();
39+
}
40+
41+
@Override
42+
public boolean getDefaultEnabled() {
43+
return EnvironmentEnabled.super.getDefaultEnabled();
44+
}
45+
}

0 commit comments

Comments
 (0)