[英]Autowiring Beans with Generic typed parameters at runtime
是的,所以我一直在努力解決這個問題,並希望得到一些建議。
我有一個 class條,我需要連接一個類型化的參數。
@Component
@RequiredArgsConstructor
@Scope(value = "prototype")
public class Bar<T> extends Foo<T> {
private final Utility utility;
public Bar<T> init(String a, int b) {
//initializations
}
//some more methods
}
在我看來,我可以像這樣使用ApplicationContext
連接 class,
Foo<Detail> foo = applicationContext.getBean(Bar.class).init(a,b);
但這會引發警告,
Type safety: The expression of type Bar needs unchecked conversion to conform to Foo<Detail>.
現在我明白這個問題是因為我在使用ApplicationContext
初始化Bar
class 的bean時沒有提到 typed 參數。
Question is, what might be the right syntax to mention the typed parameter <Detail> in the above statement?
我猜是這樣的:
String [] names = context.getBeanNamesForType(ResolvableType.forClassWithGenerics(Bar.class, Detail.class));
Foo<Detail> bar = context.getBean(names[0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.