Skip to content

Commit 6abe547

Browse files
authored
Merge pull request #138 from mercyblitz/dev-1.x
Update README and remove unused AutoRegistrationBeanInitializer
2 parents 2cd90aa + 92e01d8 commit 6abe547

24 files changed

Lines changed: 724 additions & 182 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ Choose the version that matches your Spring Boot generation:
110110

111111
| Branch | Spring Boot Compatibility | Latest Version |
112112
|--------|---------------------------|----------------|
113-
| `main` | 3.0.x – 3.5.x, 4.1.x | `0.2.23` |
114-
| `1.x` | 2.0.x – 2.7.x | `0.1.23` |
113+
| `main` | 3.0.x – 3.5.x, 4.1.x | `0.2.24` |
114+
| `1.x` | 2.0.x – 2.7.x | `0.1.24` |
115115

116116
### Add Module Dependencies
117117

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.boot.context.autoconfigure;
19+
20+
import io.microsphere.spring.boot.context.properties.annotation.EnableConfigurationPropertiesExtension;
21+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
26+
/**
27+
* {@link ConfigurationProperties @ConfigurationProperties} Auto-Configuration
28+
*
29+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
30+
* @see org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
31+
* @see EnableConfigurationProperties
32+
* @see EnableConfigurationPropertiesExtension
33+
* @since 1.0.0
34+
*/
35+
@ConditionalOnClass(name = {
36+
"io.microsphere.spring.context.annotation.BeanCapableImportCandidate"
37+
})
38+
@AutoConfigureBefore(name = {
39+
"org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration"
40+
})
41+
@EnableConfigurationPropertiesExtension
42+
public class ConfigurationPropertiesAutoConfiguration {
43+
}

microsphere-spring-boot-core/src/main/java/io/microsphere/spring/boot/context/config/AutoRegistrationBeanInitializer.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

microsphere-spring-boot-core/src/main/java/io/microsphere/spring/boot/context/properties/ListenableConfigurationPropertiesBindHandlerAdvisor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
* @see Bindable
7171
* @since 1.0.0
7272
*/
73-
public class ListenableConfigurationPropertiesBindHandlerAdvisor implements ConfigurationPropertiesBindHandlerAdvisor, BeanFactoryAware {
73+
public class ListenableConfigurationPropertiesBindHandlerAdvisor implements ConfigurationPropertiesBindHandlerAdvisor,
74+
BeanFactoryAware {
7475

7576
private BeanFactory beanFactory;
7677

@@ -112,4 +113,4 @@ public BindHandler apply(BindHandler bindHandler) {
112113
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
113114
this.beanFactory = beanFactory;
114115
}
115-
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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.boot.context.properties.annotation;
19+
20+
import io.microsphere.spring.beans.BeanSource;
21+
import io.microsphere.spring.boot.context.properties.ListenableConfigurationPropertiesBindHandlerAdvisor;
22+
import io.microsphere.spring.boot.context.properties.bind.BindListener;
23+
import io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanPropertyChangedEvent;
24+
import io.microsphere.spring.boot.context.properties.bind.EventPublishingConfigurationPropertiesBeanPropertyChangedListener;
25+
import io.microsphere.spring.context.annotation.OverrideAnnotationAttributes;
26+
import org.springframework.boot.context.properties.ConfigurationPropertiesBindHandlerAdvisor;
27+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
28+
import org.springframework.boot.context.properties.bind.BindHandler;
29+
import org.springframework.boot.context.properties.bind.Binder;
30+
import org.springframework.context.annotation.Import;
31+
32+
import java.lang.annotation.Documented;
33+
import java.lang.annotation.Retention;
34+
import java.lang.annotation.Target;
35+
36+
import static io.microsphere.spring.beans.BeanSource.BEAN_FACTORY;
37+
import static io.microsphere.spring.beans.BeanSource.JAVA_SERVICE_PROVIDER;
38+
import static io.microsphere.spring.beans.BeanSource.SPRING_FACTORIES;
39+
import static java.lang.annotation.ElementType.TYPE;
40+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
41+
42+
43+
/**
44+
* An extension annotation of {@link EnableConfigurationProperties @EnableConfigurationProperties} that provides
45+
* advanced configuration properties binding features.
46+
* <p>
47+
* This annotation enables:
48+
* <ul>
49+
* <li>Automatic advising of {@link BindListener} implementations during {@link Binder#bind(String, Class) binding}</li>
50+
* <li>Event publishing for configuration properties bean property changes (e.g., {@link ConfigurationPropertiesBeanPropertyChangedEvent})</li>
51+
* <li>Flexible bean source configuration for discovering extension components</li>
52+
* </ul>
53+
*
54+
* <h3>Example Usage</h3>
55+
* <pre>{@code
56+
* @Configuration
57+
* @EnableConfigurationPropertiesExtension(
58+
* adviseBindListener = true,
59+
* publishEvents = true,
60+
* sources = {BeanSource.BEAN_FACTORY, BeanSource.SPRING_FACTORIES}
61+
* )
62+
* public class MyConfiguration {
63+
*
64+
* @Bean
65+
* public BindListener myBindListener() {
66+
* return event -> {
67+
* // Handle bind events
68+
* };
69+
* }
70+
* }
71+
* }</pre>
72+
*
73+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
74+
* @see EnableConfigurationProperties
75+
* @see OverrideAnnotationAttributes
76+
* @see ListenableConfigurationPropertiesBindHandlerAdvisor
77+
* @see BindListener
78+
* @see ConfigurationPropertiesBindHandlerAdvisor
79+
* @see BindHandler
80+
* @since 1.0.0
81+
*/
82+
@Target(TYPE)
83+
@Retention(RUNTIME)
84+
@Documented
85+
@OverrideAnnotationAttributes
86+
@Import(EnableConfigurationPropertiesExtensionRegistrar.class)
87+
public @interface EnableConfigurationPropertiesExtension {
88+
89+
/**
90+
* Indicate whether advise {@link BindListener} adapting on the lifecycle of {@link BindHandler} when
91+
* {@link EnableConfigurationProperties @EnableConfigurationProperties} {@link Binder#bind(String, Class) binds}.
92+
*
93+
* @return {@code true} as default
94+
* @see BindListener
95+
* @see ListenableConfigurationPropertiesBindHandlerAdvisor
96+
* @see ConfigurationPropertiesBindHandlerAdvisor
97+
* @see BindHandler
98+
*/
99+
boolean adviseBindListener() default true;
100+
101+
/**
102+
* Indicate whether publish events when {@link EnableConfigurationProperties @EnableConfigurationProperties}
103+
* {@link Binder#bind(String, Class) binds}, such as:
104+
* <ul>
105+
* <li>{@link ConfigurationPropertiesBeanPropertyChangedEvent}</li>
106+
* </ul>
107+
* <p>
108+
* If {@link #adviseBindListener()} is {@code false}, the events will not be published.
109+
*
110+
* @return {@code true} as default
111+
* @see #adviseBindListener()
112+
* @see ConfigurationPropertiesBeanPropertyChangedEvent
113+
* @see EventPublishingConfigurationPropertiesBeanPropertyChangedListener
114+
* @see ListenableConfigurationPropertiesBindHandlerAdvisor
115+
*/
116+
boolean publishEvents() default true;
117+
118+
/**
119+
* Indicate the sources of beans from which the {@link EnableConfigurationProperties @EnableConfigurationProperties}
120+
* extension components are collected, such as:
121+
* <ul>
122+
* <li>{@link ConfigurationPropertiesBindHandlerAdvisor}</li>
123+
* <li>{@link BindListener}</li>
124+
* </ul>
125+
*
126+
* @return the default value is the array of
127+
* {@link BeanSource#BEAN_FACTORY}, {@link BeanSource#SPRING_FACTORIES} and {@link BeanSource#JAVA_SERVICE_PROVIDER}
128+
* @see BeanSource#BEAN_FACTORY
129+
* @see BeanSource#SPRING_FACTORIES
130+
* @see BeanSource#JAVA_SERVICE_PROVIDER
131+
*/
132+
BeanSource[] sources() default {BEAN_FACTORY, SPRING_FACTORIES, JAVA_SERVICE_PROVIDER};
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.boot.context.properties.annotation;
19+
20+
import io.microsphere.spring.beans.BeanSource;
21+
import io.microsphere.spring.boot.context.properties.ListenableConfigurationPropertiesBindHandlerAdvisor;
22+
import io.microsphere.spring.boot.context.properties.bind.BindListener;
23+
import io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanPropertyChangedEvent;
24+
import io.microsphere.spring.boot.context.properties.bind.EventPublishingConfigurationPropertiesBeanPropertyChangedListener;
25+
import io.microsphere.spring.context.annotation.AnnotatedBeanCapableImportBeanDefinitionRegistrar;
26+
import io.microsphere.spring.core.annotation.ResolvablePlaceholderAnnotationAttributes;
27+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
28+
import org.springframework.beans.factory.support.BeanNameGenerator;
29+
import org.springframework.boot.context.properties.ConfigurationPropertiesBindHandlerAdvisor;
30+
import org.springframework.boot.context.properties.bind.BindHandler;
31+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
32+
import org.springframework.core.type.AnnotationMetadata;
33+
34+
import static io.microsphere.spring.beans.BeanSource.registerBeans;
35+
import static io.microsphere.spring.beans.factory.support.BeanRegistrar.registerGenericBean;
36+
37+
/**
38+
* The {@link ImportBeanDefinitionRegistrar} for {@link EnableConfigurationPropertiesExtension @EnableConfigurationPropertiesExtension}
39+
*
40+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
41+
* @see EnableConfigurationPropertiesExtension
42+
* @see AnnotatedBeanCapableImportBeanDefinitionRegistrar
43+
* @see ListenableConfigurationPropertiesBindHandlerAdvisor
44+
* @see BindListener
45+
* @see EventPublishingConfigurationPropertiesBeanPropertyChangedListener
46+
* @see ConfigurationPropertiesBeanPropertyChangedEvent
47+
* @see ConfigurationPropertiesBindHandlerAdvisor
48+
* @see BindHandler
49+
* @see ImportBeanDefinitionRegistrar
50+
* @since 1.0.0
51+
*/
52+
class EnableConfigurationPropertiesExtensionRegistrar extends AnnotatedBeanCapableImportBeanDefinitionRegistrar<EnableConfigurationPropertiesExtension> {
53+
54+
@Override
55+
protected void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry,
56+
BeanNameGenerator importBeanNameGenerator,
57+
ResolvablePlaceholderAnnotationAttributes<EnableConfigurationPropertiesExtension> annotationAttributes) {
58+
59+
boolean adviseBindListener = annotationAttributes.getBoolean("adviseBindListener");
60+
BeanSource[] sources = (BeanSource[]) annotationAttributes.get("sources");
61+
62+
if (adviseBindListener) {
63+
// register ListenableConfigurationPropertiesBindHandlerAdvisor
64+
registerGenericBean(registry, ListenableConfigurationPropertiesBindHandlerAdvisor.class);
65+
boolean publishEvents = annotationAttributes.getBoolean("publishEvents");
66+
if (publishEvents) {
67+
// register EventPublishingConfigurationPropertiesBeanPropertyChangedListener
68+
registerGenericBean(registry, EventPublishingConfigurationPropertiesBeanPropertyChangedListener.class);
69+
}
70+
// register BindListener Beans
71+
registerBindListenerBeans(registry, sources);
72+
}
73+
// register ConfigurationPropertiesBindHandlerAdvisor Beans
74+
registerConfigurationPropertiesBindHandlerAdvisorBeans(registry, sources);
75+
}
76+
77+
private void registerBindListenerBeans(BeanDefinitionRegistry registry, BeanSource[] sources) {
78+
registerBeans(getBeanFactory(), registry, sources, BindListener.class);
79+
}
80+
81+
private void registerConfigurationPropertiesBindHandlerAdvisorBeans(BeanDefinitionRegistry registry, BeanSource[] sources) {
82+
registerBeans(getBeanFactory(), registry, sources, ConfigurationPropertiesBindHandlerAdvisor.class);
83+
}
84+
}

0 commit comments

Comments
 (0)