我有下课.
public class SomeClass<T extends CustomView> {
public void someMethod() {
T t; // = new T(context) ...compile error!
// I want instance of SomeType that have parameter(context) of constructor.
t.go();
}
}
Run Code Online (Sandbox Code Playgroud)
我想用构造函数的参数创建泛型类型T的实例.
我想TypeToken,Class<T>,newInstance和等,但没有成功.我想要一些帮助.谢谢您的回答.
我有一些示例代码发布到github,所以我不会在这里包含长代码片段,请参阅:https://github.com/ralf-lackinger/spring-inheritance/tree/master
我有一个Parent和一个Child,都是春豆.当我想Parent通过BeanFactorybean的名称检索via时,Child返回一个实例.这会给我带来问题,因为我希望它们具有不同的名称并仅返回特定的名称,因为子类会覆盖我从超类中使用的方法:
父名称:parent,parentName1,parentName2
子名称:child,childName1,childName2
简短的代码示例,请参阅我的github存储库以获取完整的代码示例:
context.getBean("parentName1", Parent.class).print();
返回:
Print from child.
也许有一个使用Spring的限定符的解决方案,但我不确定这是否有效.我已经尝试过@Primary注释,但没有帮我解决这个问题.
我正在使用:
java版"1.8.0_66"
Spring 4.2.3.RELEASE
编辑:
我使用名为'fixed-name-conflict'的分支更新了github存储库,其中包含了我需要进行的更改,以解决我的问题.有关更多信息,请参阅已接受的答案(以及那里的公告).
谢谢@oailloud的帮助.
编辑2:
有问题的部分是这些方法:
Parent:
@Bean(name = {"parentName1", "parentName2"})
public Parent additionalBeanNames() {
return new Parent();
}
Run Code Online (Sandbox Code Playgroud)
并且Child:
@Override
@Bean(name = {"childName1", "childName2"})
public Child additionalBeanNames() {
return new Child();
}
Run Code Online (Sandbox Code Playgroud)
解决方案是重命名为Child方法,因此孩子不会获取Parent额外的bean名称.