|
23 | 23 | import org.springframework.beans.factory.BeanRegistrar;
|
24 | 24 | import org.springframework.beans.factory.BeanRegistry;
|
25 | 25 | import org.springframework.beans.factory.config.BeanDefinition;
|
| 26 | +import org.springframework.core.ParameterizedTypeReference; |
| 27 | +import org.springframework.core.ResolvableType; |
26 | 28 | import org.springframework.core.env.Environment;
|
27 | 29 | import org.springframework.core.env.StandardEnvironment;
|
28 | 30 |
|
@@ -203,6 +205,22 @@ void customSupplier() {
|
203 | 205 | assertThat(supplier.get()).isNotNull().isInstanceOf(Foo.class);
|
204 | 206 | }
|
205 | 207 |
|
| 208 | + @Test |
| 209 | + void customTargetTypeFromResolvableType() { |
| 210 | + BeanRegistryAdapter adapter = new BeanRegistryAdapter(this.beanFactory, this.beanFactory, TargetTypeBeanRegistrar.class); |
| 211 | + new TargetTypeBeanRegistrar().register(adapter, env); |
| 212 | + RootBeanDefinition beanDefinition = (RootBeanDefinition)this.beanFactory.getBeanDefinition("fooSupplierFromResolvableType"); |
| 213 | + assertThat(beanDefinition.getResolvableType().resolveGeneric(0)).isEqualTo(Foo.class); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + void customTargetTypeFromTypeReference() { |
| 218 | + BeanRegistryAdapter adapter = new BeanRegistryAdapter(this.beanFactory, this.beanFactory, TargetTypeBeanRegistrar.class); |
| 219 | + new TargetTypeBeanRegistrar().register(adapter, env); |
| 220 | + RootBeanDefinition beanDefinition = (RootBeanDefinition)this.beanFactory.getBeanDefinition("fooSupplierFromTypeReference"); |
| 221 | + assertThat(beanDefinition.getResolvableType().resolveGeneric(0)).isEqualTo(Foo.class); |
| 222 | + } |
| 223 | + |
206 | 224 |
|
207 | 225 | private static class DefaultBeanRegistrar implements BeanRegistrar {
|
208 | 226 |
|
@@ -292,6 +310,18 @@ public void register(BeanRegistry registry, Environment env) {
|
292 | 310 | }
|
293 | 311 | }
|
294 | 312 |
|
| 313 | + private static class TargetTypeBeanRegistrar implements BeanRegistrar { |
| 314 | + |
| 315 | + @Override |
| 316 | + public void register(BeanRegistry registry, Environment env) { |
| 317 | + registry.registerBean("fooSupplierFromResolvableType", Foo.class, |
| 318 | + spec -> spec.targetType(ResolvableType.forClassWithGenerics(Supplier.class, Foo.class))); |
| 319 | + ParameterizedTypeReference<Supplier<Foo>> type = new ParameterizedTypeReference<>() {}; |
| 320 | + registry.registerBean("fooSupplierFromTypeReference", Supplier.class, |
| 321 | + spec -> spec.targetType(type)); |
| 322 | + } |
| 323 | + } |
| 324 | + |
295 | 325 | private static class Foo {}
|
296 | 326 |
|
297 | 327 | }
|
0 commit comments