Skip to content

Commit 0685651

Browse files
committed
Add a BeanFactory#getBeanProvider Kotlin extension
Issue: SPR-17274
1 parent cde677a commit 0685651

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanFactoryExtensions.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.beans.factory
1818

19+
import org.springframework.core.ParameterizedTypeReference
20+
import org.springframework.core.ResolvableType
21+
1922
/**
2023
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>()` variant.
2124
*
@@ -44,3 +47,15 @@ inline fun <reified T : Any> BeanFactory.getBean(name: String): T =
4447
*/
4548
inline fun <reified T : Any> BeanFactory.getBean(vararg args:Any): T =
4649
getBean(T::class.java, *args)
50+
51+
/**
52+
* Extension for [BeanFactory.getBeanProvider] providing a `getBeanProvider<Foo>()` variant.
53+
* This extension is not subject to type erasure and retains actual generic type arguments.
54+
*
55+
* @see BeanFactory.getBeanProvider(ResolvableType)
56+
* @author Sebastien Deleuze
57+
* @since 5.1
58+
*/
59+
inline fun <reified T : Any> BeanFactory.getBeanProvider(): ObjectProvider<T> =
60+
getBeanProvider(ResolvableType.forType((object : ParameterizedTypeReference<T>() {}).type))
61+

spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,9 +19,11 @@ package org.springframework.beans.factory
1919
import org.junit.Test
2020
import org.junit.runner.RunWith
2121
import org.mockito.Answers
22+
import org.mockito.ArgumentMatchers
2223
import org.mockito.Mock
2324
import org.mockito.Mockito.*
2425
import org.mockito.junit.MockitoJUnitRunner
26+
import org.springframework.core.ResolvableType
2527

2628
/**
2729
* Mock object based tests for BeanFactory Kotlin extensions.
@@ -55,5 +57,11 @@ class BeanFactoryExtensionsTests {
5557
verify(bf, times(1)).getBean(Foo::class.java, arg1, arg2)
5658
}
5759

60+
@Test
61+
fun `getBeanProvider with reified type parameters`() {
62+
bf.getBeanProvider<Foo>()
63+
verify(bf, times(1)).getBeanProvider<ObjectProvider<Foo>>(ArgumentMatchers.any<ResolvableType>())
64+
}
65+
5866
class Foo
5967
}

0 commit comments

Comments
 (0)