Skip to content

Commit 6850ae9

Browse files
committed
Improve searching for qualified beans. Clean up RestClient-based implementation. Make autoconfiguration conditional on property.
1 parent bdb61d7 commit 6850ae9

File tree

8 files changed

+25
-169
lines changed

8 files changed

+25
-169
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/AbstractInterfaceClientsFactoryBean.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.springframework.beans.BeansException;
2020
import org.springframework.beans.factory.FactoryBean;
21-
import org.springframework.beans.factory.InitializingBean;
2221
import org.springframework.context.ApplicationContext;
2322
import org.springframework.context.ApplicationContextAware;
2423
import org.springframework.context.ConfigurableApplicationContext;
@@ -27,8 +26,7 @@
2726
/**
2827
* @author Olga Maciaszek-Sharma
2928
*/
30-
public abstract class AbstractInterfaceClientsFactoryBean
31-
implements FactoryBean<Object>, ApplicationContextAware, InitializingBean {
29+
public abstract class AbstractInterfaceClientsFactoryBean implements FactoryBean<Object>, ApplicationContextAware {
3230

3331
protected Class<?> type;
3432

@@ -38,20 +36,6 @@ public abstract class AbstractInterfaceClientsFactoryBean
3836

3937
protected ConfigurableApplicationContext applicationContext;
4038

41-
public AbstractInterfaceClientsFactoryBean() {
42-
}
43-
44-
// TODO
45-
@Override
46-
public Object getObject() throws Exception {
47-
throw new UnsupportedOperationException("Please, implement me.");
48-
}
49-
50-
@Override
51-
public void afterPropertiesSet() throws Exception {
52-
Assert.notNull(this.type, "Type must not be null");
53-
}
54-
5539
@Override
5640
public Class<?> getObjectType() {
5741
return this.type;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/AbstractInterfaceClientsImportRegistrar.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.List;
2323
import java.util.Set;
2424

25-
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
2725
import org.apache.commons.text.CaseUtils;
2826

2927
import org.springframework.beans.factory.ListableBeanFactory;
@@ -49,6 +47,7 @@
4947
import org.springframework.util.ObjectUtils;
5048

5149
/**
50+
* @author Josh Long
5251
* @author Olga Maciaszek-Sharma
5352
*/
5453
// TODO: Handle AOT
@@ -91,21 +90,9 @@ private void registerInterfaceClient(BeanDefinitionRegistry registry, AnnotatedB
9190
getAnnotation().getSimpleName() + "can only be placed on an interface.");
9291
MergedAnnotation<? extends Annotation> annotation = annotatedBeanMetadata.getAnnotations().get(getAnnotation());
9392
String beanClassName = annotatedBeanMetadata.getClassName();
94-
// Class<?> beanClass;
95-
// try {
96-
// beanClass = Class.forName(beanClassName);
97-
// }
98-
// catch (ClassNotFoundException e) {
99-
// if (logger.isDebugEnabled()) {
100-
// logger.debug("Class not found for interface client " + beanClassName + ": " +
101-
// e.getMessage());
102-
// }
103-
// throw new RuntimeException(e);
104-
// }
105-
// TODO: consider naming conventions: value of the annotation is the
106-
// qualifier to look for related beans
107-
// TODO: while the actual beanName corresponds to the simple class name
108-
// suffixed with InterfaceClient
93+
// The value of the annotation is the qualifier to look for related beans
94+
// while the default beanName corresponds to the simple class name suffixed with
95+
// `InterfaceClient`
10996
String clientId = annotation.getString(MergedAnnotation.VALUE);
11097
String beanName = !ObjectUtils.isEmpty(annotation.getString(BEAN_NAME_ATTRIBUTE_NAME))
11198
? annotation.getString(BEAN_NAME_ATTRIBUTE_NAME) : buildBeanName(clientId);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/HttpInterfaceClientsAutoConfiguration.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@
3737
@AutoConfiguration(after = { RestTemplateAutoConfiguration.class, RestClientAutoConfiguration.class,
3838
WebClientAutoConfiguration.class })
3939
@EnableConfigurationProperties(HttpInterfaceClientsProperties.class)
40+
@ConditionalOnProperty(value = "spring.interfaceclients.enabled", havingValue = "true")
4041
public class HttpInterfaceClientsAutoConfiguration {
4142

42-
// @Bean
43-
// HttpInterfaceClientsAdapter httpInterfaceClientAdapter(HttpExchangeAdapterProvider
44-
// adapterProvider) {
45-
// return new HttpInterfaceClientsAdapter(adapterProvider);
46-
// }
47-
4843
@Configuration(proxyBeanMethods = false)
4944
@ConditionalOnClass({ RestClient.class, RestClientAdapter.class, HttpServiceProxyFactory.class })
5045
@Conditional(NotReactiveWebApplicationCondition.class)

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/HttpInterfaceClientsImportRegistrar.java

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

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/QualifiedBeanProvider.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@
2424

2525
import org.springframework.beans.factory.BeanFactoryUtils;
2626
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
27-
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
2827
import org.springframework.beans.factory.annotation.Qualifier;
29-
import org.springframework.beans.factory.config.BeanDefinition;
3028
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
31-
import org.springframework.core.annotation.AnnotationAttributes;
32-
import org.springframework.core.annotation.MergedAnnotation;
33-
import org.springframework.core.type.MethodMetadata;
3429

3530
/**
3631
* @author Olga Maciaszek-Sharma
@@ -67,20 +62,9 @@ private static <T> Map<String, T> getQualifiedBeansOfType(ConfigurableListableBe
6762
Map<String, T> beansOfType = BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type);
6863
Map<String, T> matchingClientBeans = new HashMap<>();
6964
for (String beanName : beansOfType.keySet()) {
70-
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
71-
if (beanDefinition instanceof AnnotatedBeanDefinition definition) {
72-
// TODO: try refactoring this
73-
// TODO: beans defined in a different way
74-
// AnnotationAttributes annotationAttributes = AnnotationAttributes
75-
// .fromMap(definition.getMetadata().getAnnotationAttributes(Qualifier.class.getName()));
76-
MethodMetadata methodMetadata = definition.getFactoryMethodMetadata();
77-
AnnotationAttributes annotationAttributes = methodMetadata != null ? AnnotationAttributes
78-
.fromMap(methodMetadata.getAnnotationAttributes(Qualifier.class.getName())) : null;
79-
if (annotationAttributes != null
80-
&& clientId.equals(annotationAttributes.getString(MergedAnnotation.VALUE))) {
81-
matchingClientBeans.put(beanName, beansOfType.get(beanName));
82-
}
83-
65+
Qualifier qualifier = (beanFactory.findAnnotationOnBean(beanName, Qualifier.class));
66+
if (qualifier != null && clientId.equals(qualifier.value())) {
67+
matchingClientBeans.put(beanName, beanFactory.getBean(beanName, type));
8468
}
8569
}
8670
return matchingClientBeans;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/RestClientAdapterProvider.java

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

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/RestClientInterfaceClientsImportRegistrar.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616

1717
package org.springframework.boot.autoconfigure.interfaceclients.http;
1818

19+
import java.lang.annotation.Annotation;
20+
21+
import org.springframework.boot.autoconfigure.interfaceclients.AbstractInterfaceClientsImportRegistrar;
22+
1923
/**
2024
* @author Olga Maciaszek-Sharma
2125
*/
22-
public class RestClientInterfaceClientsImportRegistrar extends HttpInterfaceClientsImportRegistrar {
26+
public class RestClientInterfaceClientsImportRegistrar extends AbstractInterfaceClientsImportRegistrar {
27+
28+
@Override
29+
protected Class<? extends Annotation> getAnnotation() {
30+
return HttpClient.class;
31+
}
2332

2433
@Override
2534
protected Class<?> getFactoryBeanClass() {

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,12 @@
16111611
"name": "spring.info.git.location",
16121612
"defaultValue": "classpath:git.properties"
16131613
},
1614+
{
1615+
"name": "spring.interfaceclients.enabled",
1616+
"type": "java.lang.Boolean",
1617+
"description": "Whether to automatically instantiate client beans from annotated interfaces.",
1618+
"defaultValue": true
1619+
},
16141620
{
16151621
"name": "spring.interfaceclients.resttemplate.enabled",
16161622
"type": "java.lang.Boolean",

0 commit comments

Comments
 (0)