Add a BeanFactory#getBeanProvider Kotlin extension

Issue: SPR-17274
This commit is contained in:
Sebastien Deleuze 2018-09-13 16:38:59 +02:00
parent cde677a1f4
commit 068565172e
2 changed files with 25 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,9 @@
package org.springframework.beans.factory package org.springframework.beans.factory
import org.springframework.core.ParameterizedTypeReference
import org.springframework.core.ResolvableType
/** /**
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>()` variant. * Extension for [BeanFactory.getBean] providing a `getBean<Foo>()` variant.
* *
@ -44,3 +47,15 @@ inline fun <reified T : Any> BeanFactory.getBean(name: String): T =
*/ */
inline fun <reified T : Any> BeanFactory.getBean(vararg args:Any): T = inline fun <reified T : Any> BeanFactory.getBean(vararg args:Any): T =
getBean(T::class.java, *args) getBean(T::class.java, *args)
/**
* Extension for [BeanFactory.getBeanProvider] providing a `getBeanProvider<Foo>()` variant.
* This extension is not subject to type erasure and retains actual generic type arguments.
*
* @see BeanFactory.getBeanProvider(ResolvableType)
* @author Sebastien Deleuze
* @since 5.1
*/
inline fun <reified T : Any> BeanFactory.getBeanProvider(): ObjectProvider<T> =
getBeanProvider(ResolvableType.forType((object : ParameterizedTypeReference<T>() {}).type))

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,9 +19,11 @@ package org.springframework.beans.factory
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.Answers import org.mockito.Answers
import org.mockito.ArgumentMatchers
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.* import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import org.springframework.core.ResolvableType
/** /**
* Mock object based tests for BeanFactory Kotlin extensions. * Mock object based tests for BeanFactory Kotlin extensions.
@ -55,5 +57,11 @@ class BeanFactoryExtensionsTests {
verify(bf, times(1)).getBean(Foo::class.java, arg1, arg2) verify(bf, times(1)).getBean(Foo::class.java, arg1, arg2)
} }
@Test
fun `getBeanProvider with reified type parameters`() {
bf.getBeanProvider<Foo>()
verify(bf, times(1)).getBeanProvider<ObjectProvider<Foo>>(ArgumentMatchers.any<ResolvableType>())
}
class Foo class Foo
} }