26
26
import org .springframework .beans .factory .config .ConstructorArgumentValues ;
27
27
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
28
28
import org .springframework .beans .factory .support .BeanNameGenerator ;
29
- import org .springframework .beans .factory .support .GenericBeanDefinition ;
29
+ import org .springframework .beans .factory .support .RootBeanDefinition ;
30
30
import org .springframework .context .EnvironmentAware ;
31
31
import org .springframework .context .ResourceLoaderAware ;
32
32
import org .springframework .context .annotation .ClassPathScanningCandidateComponentProvider ;
38
38
import org .springframework .core .type .classreading .MetadataReader ;
39
39
import org .springframework .core .type .filter .AnnotationTypeFilter ;
40
40
import org .springframework .util .Assert ;
41
- import org .springframework .util .StringUtils ;
42
41
import org .springframework .web .service .annotation .HttpExchange ;
43
42
44
43
/**
69
68
* @author Rossen Stoyanchev
70
69
* @author Phillip Webb
71
70
* @author Olga Maciaszek-Sharma
71
+ * @author Stephane Nicoll
72
72
* @since 7.0
73
73
* @see ImportHttpServices
74
74
* @see HttpServiceProxyRegistryFactoryBean
75
75
*/
76
76
public abstract class AbstractHttpServiceRegistrar implements
77
77
ImportBeanDefinitionRegistrar , EnvironmentAware , ResourceLoaderAware , BeanFactoryAware {
78
78
79
+ /**
80
+ * The bean name of the {@link HttpServiceProxyRegistry}.
81
+ */
82
+ public static final String HTTP_SERVICE_PROXY_REGISTRY_BEAN_NAME = "httpServiceProxyRegistry" ;
83
+
84
+ static final String HTTP_SERVICE_GROUP_NAME_ATTRIBUTE = "httpServiceGroupName" ;
85
+
79
86
private HttpServiceGroup .ClientType defaultClientType = HttpServiceGroup .ClientType .UNSPECIFIED ;
80
87
81
88
private @ Nullable Environment environment ;
@@ -127,33 +134,36 @@ public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefin
127
134
128
135
registerHttpServices (new DefaultGroupRegistry (), metadata );
129
136
130
- String proxyRegistryBeanName = StringUtils .uncapitalize (HttpServiceProxyRegistry .class .getSimpleName ());
131
- GenericBeanDefinition proxyRegistryBeanDef ;
132
-
133
- if (!beanRegistry .containsBeanDefinition (proxyRegistryBeanName )) {
134
- proxyRegistryBeanDef = new GenericBeanDefinition ();
135
- proxyRegistryBeanDef .setBeanClass (HttpServiceProxyRegistryFactoryBean .class );
136
- ConstructorArgumentValues args = proxyRegistryBeanDef .getConstructorArgumentValues ();
137
- args .addIndexedArgumentValue (0 , new GroupsMetadata ());
138
- beanRegistry .registerBeanDefinition (proxyRegistryBeanName , proxyRegistryBeanDef );
139
- }
140
- else {
141
- proxyRegistryBeanDef = (GenericBeanDefinition ) beanRegistry .getBeanDefinition (proxyRegistryBeanName );
142
- }
137
+ RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry (beanRegistry );
143
138
144
139
mergeGroups (proxyRegistryBeanDef );
145
140
146
141
this .groupsMetadata .forEachRegistration ((groupName , types ) -> types .forEach (type -> {
147
- GenericBeanDefinition proxyBeanDef = new GenericBeanDefinition ();
142
+ RootBeanDefinition proxyBeanDef = new RootBeanDefinition ();
148
143
proxyBeanDef .setBeanClassName (type );
144
+ proxyBeanDef .setAttribute (HTTP_SERVICE_GROUP_NAME_ATTRIBUTE , groupName );
145
+ proxyBeanDef .setInstanceSupplier (() -> getProxyInstance (groupName , type ));
149
146
String beanName = (groupName + "#" + type );
150
- proxyBeanDef .setInstanceSupplier (() -> getProxyInstance (proxyRegistryBeanName , groupName , type ));
151
147
if (!beanRegistry .containsBeanDefinition (beanName )) {
152
148
beanRegistry .registerBeanDefinition (beanName , proxyBeanDef );
153
149
}
154
150
}));
155
151
}
156
152
153
+ private RootBeanDefinition createOrGetRegistry (BeanDefinitionRegistry beanRegistry ) {
154
+ if (!beanRegistry .containsBeanDefinition (HTTP_SERVICE_PROXY_REGISTRY_BEAN_NAME )) {
155
+ RootBeanDefinition proxyRegistryBeanDef = new RootBeanDefinition ();
156
+ proxyRegistryBeanDef .setBeanClass (HttpServiceProxyRegistryFactoryBean .class );
157
+ ConstructorArgumentValues args = proxyRegistryBeanDef .getConstructorArgumentValues ();
158
+ args .addIndexedArgumentValue (0 , new GroupsMetadata ());
159
+ beanRegistry .registerBeanDefinition (HTTP_SERVICE_PROXY_REGISTRY_BEAN_NAME , proxyRegistryBeanDef );
160
+ return proxyRegistryBeanDef ;
161
+ }
162
+ else {
163
+ return (RootBeanDefinition ) beanRegistry .getBeanDefinition (HTTP_SERVICE_PROXY_REGISTRY_BEAN_NAME );
164
+ }
165
+ }
166
+
157
167
/**
158
168
* This method is called before any bean definition registrations are made.
159
169
* Subclasses must implement it to register the HTTP Services for which bean
@@ -175,7 +185,7 @@ private ClassPathScanningCandidateComponentProvider getScanner() {
175
185
return this .scanner ;
176
186
}
177
187
178
- private void mergeGroups (GenericBeanDefinition proxyRegistryBeanDef ) {
188
+ private void mergeGroups (RootBeanDefinition proxyRegistryBeanDef ) {
179
189
ConstructorArgumentValues args = proxyRegistryBeanDef .getConstructorArgumentValues ();
180
190
ConstructorArgumentValues .ValueHolder valueHolder = args .getArgumentValue (0 , GroupsMetadata .class );
181
191
Assert .state (valueHolder != null , "Expected GroupsMetadata constructor argument at index 0" );
@@ -184,12 +194,10 @@ private void mergeGroups(GenericBeanDefinition proxyRegistryBeanDef) {
184
194
target .mergeWith (this .groupsMetadata );
185
195
}
186
196
187
- private Object getProxyInstance (String registryBeanName , String groupName , String httpServiceType ) {
197
+ private Object getProxyInstance (String groupName , String httpServiceType ) {
188
198
Assert .state (this .beanFactory != null , "BeanFactory has not been set" );
189
- HttpServiceProxyRegistry registry = this .beanFactory .getBean (registryBeanName , HttpServiceProxyRegistry .class );
190
- Object proxy = registry .getClient (groupName , GroupsMetadata .loadClass (httpServiceType ));
191
- Assert .notNull (proxy , "No proxy for HTTP Service [" + httpServiceType + "]" );
192
- return proxy ;
199
+ HttpServiceProxyRegistry registry = this .beanFactory .getBean (HTTP_SERVICE_PROXY_REGISTRY_BEAN_NAME , HttpServiceProxyRegistry .class );
200
+ return registry .getClient (groupName , GroupsMetadata .loadClass (httpServiceType ));
193
201
}
194
202
195
203
0 commit comments