简体   繁体   English

返回类型的spring bean

[英]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. Java通过类型擦除实现泛型。 Which means the generic type is available at compile time but is Object at runtime. 这意味着泛型类型在编译时可用,但在运行时是Object。 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. 这需要在运行时了解有关参数化类型<String> ,并且只有编译器知道这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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