简体   繁体   中英

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 );

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM