Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return spring bean with type

This feels like a dumb question and could even be a duplicate (I've looked, but can't find it).

But how in the heck (if it is even possible) do I do this with type safety?

ArrayList<String> myList = applicationContext.getBean( ArrayList<String>.class );
like image 502
javamonkey79 Avatar asked Jan 18 '23 22:01

javamonkey79


2 Answers

Java implements generics via type erasure. Which means the generic type is available at compile time but is Object at runtime. So no, there's no way to get that to work without casting.

like image 195
Kevin Avatar answered Jan 30 '23 21:01

Kevin


Sadly, you don't. That would require knowledge at runtime about the parameterized type <String>, and only the compiler knows that.

like image 41
Ryan Stewart Avatar answered Jan 30 '23 21:01

Ryan Stewart