Skip to content

Commit 3341a57

Browse files
committed
Add auto-service-registration condition
Introduce a new @ConditionalOnAutoServiceRegistrationAvailable meta-annotation and refactor web service registry auto-configurations to use Microsphere web availability conditions. WebFlux/WebMvc auto-config classes now use ConditionalOnWebFluxAvailable/ConditionalOnWebMvcAvailable, include AutoConfigureAfter for Microsphere web auto-config and ServiceRegistryAutoConfiguration, and apply the new auto-registration availability condition. WebServiceRegistryAutoConfiguration had class-level {@code @configuration} and several conditional annotations removed so concrete web-specific subclasses control activation. Update pom.xml to adjust Microsphere module dependencies (add microsphere-spring-boot-webmvc, microsphere-spring-boot-webflux, microsphere-spring-boot-actuator and relocate aspectjweaver). Tests updated to remove deprecated EnableWebMvc/EnableWebFlux extension annotations.
1 parent 4867531 commit 3341a57

7 files changed

Lines changed: 85 additions & 49 deletions

File tree

microsphere-spring-cloud-commons/pom.xml

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@
2020

2121
<dependencies>
2222

23+
<!-- Microsphere Dependencies -->
2324
<dependency>
24-
<groupId>org.aspectj</groupId>
25-
<artifactId>aspectjweaver</artifactId>
25+
<groupId>io.github.microsphere-projects</groupId>
26+
<artifactId>microsphere-spring-boot-webmvc</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>io.github.microsphere-projects</groupId>
31+
<artifactId>microsphere-spring-boot-webflux</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>io.github.microsphere-projects</groupId>
36+
<artifactId>microsphere-spring-boot-actuator</artifactId>
37+
<optional>true</optional>
2638
</dependency>
2739

2840
<!-- Spring Boot Dependencies -->
@@ -109,28 +121,9 @@
109121
<optional>true</optional>
110122
</dependency>
111123

112-
<!-- Microsphere Dependencies -->
113-
<dependency>
114-
<groupId>io.github.microsphere-projects</groupId>
115-
<artifactId>microsphere-spring-boot-core</artifactId>
116-
</dependency>
117-
118-
<dependency>
119-
<groupId>io.github.microsphere-projects</groupId>
120-
<artifactId>microsphere-spring-boot-actuator</artifactId>
121-
<optional>true</optional>
122-
</dependency>
123-
124124
<dependency>
125-
<groupId>io.github.microsphere-projects</groupId>
126-
<artifactId>microsphere-spring-webmvc</artifactId>
127-
<optional>true</optional>
128-
</dependency>
129-
130-
<dependency>
131-
<groupId>io.github.microsphere-projects</groupId>
132-
<artifactId>microsphere-spring-webflux</artifactId>
133-
<optional>true</optional>
125+
<groupId>org.aspectj</groupId>
126+
<artifactId>aspectjweaver</artifactId>
134127
</dependency>
135128

136129
<!-- Testing -->

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebFluxServiceRegistryAutoConfiguration.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@
1616
*/
1717
package io.microsphere.spring.cloud.client.service.registry.autoconfigure;
1818

19+
import io.microsphere.spring.boot.webflux.autoconfigure.WebFluxAutoConfiguration;
20+
import io.microsphere.spring.boot.webflux.autoconfigure.condition.ConditionalOnWebFluxAvailable;
21+
import io.microsphere.spring.cloud.client.service.registry.condition.ConditionalOnAutoServiceRegistrationAvailable;
1922
import io.microsphere.spring.web.metadata.WebEndpointMapping;
20-
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
23+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2124
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
2225

23-
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.REACTIVE;
24-
2526
/**
2627
* Auto-Configuration class for {@link ServiceRegistry ServiceRegistry} on the Spring WebFlux Application
2728
*
2829
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
2930
* @since 1.0.0
3031
*/
31-
@ConditionalOnWebApplication(type = REACTIVE)
32+
@ConditionalOnWebFluxAvailable
33+
@ConditionalOnAutoServiceRegistrationAvailable
34+
@AutoConfigureAfter(value = {
35+
WebFluxAutoConfiguration.class,
36+
ServiceRegistryAutoConfiguration.class
37+
})
3238
public class WebFluxServiceRegistryAutoConfiguration extends WebServiceRegistryAutoConfiguration {
3339

3440
/**

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfiguration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
*/
1717
package io.microsphere.spring.cloud.client.service.registry.autoconfigure;
1818

19+
import io.microsphere.spring.boot.webmvc.autoconfigure.WebMvcAutoConfiguration;
20+
import io.microsphere.spring.boot.webmvc.autoconfigure.condition.ConditionalOnWebMvcAvailable;
21+
import io.microsphere.spring.cloud.client.service.registry.condition.ConditionalOnAutoServiceRegistrationAvailable;
1922
import io.microsphere.spring.web.metadata.WebEndpointMapping;
2023
import io.microsphere.util.ValueHolder;
2124
import jakarta.servlet.Filter;
2225
import org.springframework.beans.factory.ObjectProvider;
2326
import org.springframework.beans.factory.annotation.Autowired;
2427
import org.springframework.beans.factory.annotation.Value;
25-
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
28+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2629
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
2730
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2831
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
@@ -33,15 +36,19 @@
3336
import static io.microsphere.util.ArrayUtils.EMPTY_STRING_ARRAY;
3437
import static io.microsphere.util.ArrayUtils.arrayEquals;
3538
import static java.lang.Boolean.FALSE;
36-
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;
3739

3840
/**
3941
* Auto-Configuration class for {@link ServiceRegistry ServiceRegistry} on the Spring WebMVC Application
4042
*
4143
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
4244
* @since 1.0.0
4345
*/
44-
@ConditionalOnWebApplication(type = SERVLET)
46+
@ConditionalOnWebMvcAvailable
47+
@ConditionalOnAutoServiceRegistrationAvailable
48+
@AutoConfigureAfter(value = {
49+
WebMvcAutoConfiguration.class,
50+
ServiceRegistryAutoConfiguration.class
51+
})
4552
public class WebMvcServiceRegistryAutoConfiguration extends WebServiceRegistryAutoConfiguration {
4653

4754
private static final String[] DEFAULT_URL_MAPPINGS = {"/*"};

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebServiceRegistryAutoConfiguration.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717
package io.microsphere.spring.cloud.client.service.registry.autoconfigure;
1818

1919
import io.microsphere.logging.Logger;
20-
import io.microsphere.spring.cloud.client.service.registry.condition.ConditionalOnAutoServiceRegistrationEnabled;
2120
import io.microsphere.spring.web.event.WebEndpointMappingsReadyEvent;
2221
import io.microsphere.spring.web.metadata.WebEndpointMapping;
2322
import org.springframework.beans.factory.ObjectProvider;
2423
import org.springframework.beans.factory.annotation.Value;
25-
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
26-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
27-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2824
import org.springframework.cloud.client.serviceregistry.Registration;
2925
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
3026
import org.springframework.context.ApplicationContext;
3127
import org.springframework.context.ApplicationListener;
32-
import org.springframework.context.annotation.Configuration;
3328

3429
import java.util.Collection;
3530
import java.util.Iterator;
@@ -45,16 +40,6 @@
4540
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
4641
* @since 1.0.0
4742
*/
48-
@Configuration(proxyBeanMethods = false)
49-
@ConditionalOnClass(name = {
50-
"io.microsphere.spring.web.metadata.WebEndpointMapping",
51-
"io.microsphere.spring.web.event.WebEndpointMappingsReadyEvent"
52-
})
53-
@ConditionalOnBean(Registration.class)
54-
@ConditionalOnAutoServiceRegistrationEnabled
55-
@AutoConfigureAfter(value = {
56-
ServiceRegistryAutoConfiguration.class
57-
})
5843
public abstract class WebServiceRegistryAutoConfiguration implements ApplicationListener<WebEndpointMappingsReadyEvent> {
5944

6045
protected final Logger logger = getLogger(getClass());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package io.microsphere.spring.cloud.client.service.registry.condition;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
21+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
22+
import org.springframework.cloud.client.serviceregistry.Registration;
23+
import org.springframework.core.annotation.AliasFor;
24+
25+
import java.lang.annotation.Documented;
26+
import java.lang.annotation.Retention;
27+
import java.lang.annotation.Target;
28+
29+
import static io.microsphere.spring.cloud.commons.constants.CommonsPropertyConstants.SERVICE_REGISTRY_AUTO_REGISTRATION_ENABLED_PROPERTY_NAME;
30+
import static java.lang.annotation.ElementType.METHOD;
31+
import static java.lang.annotation.ElementType.TYPE;
32+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
33+
34+
/**
35+
* The conditional annotation meta-annotates {@link ConditionalOnProperty @ConditionalOnProperty} for
36+
* {@link AutoServiceRegistration Service Registry Auto-Registration} enabled.
37+
*
38+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
39+
* @see AutoServiceRegistration
40+
* @see ConditionalOnProperty
41+
* @since 1.0.0
42+
*/
43+
@Retention(RUNTIME)
44+
@Target({TYPE, METHOD})
45+
@Documented
46+
@ConditionalOnBean(Registration.class)
47+
@ConditionalOnAutoServiceRegistrationEnabled
48+
public @interface ConditionalOnAutoServiceRegistrationAvailable {
49+
}

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebFluxServiceRegistryAutoConfigurationTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package io.microsphere.spring.cloud.client.service.registry.autoconfigure;
1919

20-
import io.microsphere.spring.webflux.annotation.EnableWebFluxExtension;
2120
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2221
import org.springframework.test.context.TestPropertySource;
2322

@@ -33,7 +32,6 @@
3332
"spring.main.web-application-type=reactive"
3433
}
3534
)
36-
@EnableWebFluxExtension
3735
@EnableAutoConfiguration
3836
class WebFluxServiceRegistryAutoConfigurationTest extends WebServiceRegistryAutoConfigurationTest {
3937
}

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package io.microsphere.spring.cloud.client.service.registry.autoconfigure;
1818

19-
import io.microsphere.spring.webmvc.annotation.EnableWebMvcExtension;
2019
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2120

2221
/**
@@ -25,7 +24,6 @@
2524
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
2625
* @since 1.0.0
2726
*/
28-
@EnableWebMvcExtension
2927
@EnableAutoConfiguration
3028
class WebMvcServiceRegistryAutoConfigurationTest extends WebServiceRegistryAutoConfigurationTest {
3129
}

0 commit comments

Comments
 (0)