我已經閱讀了許多奧林的文章,並無法找到答案,並且我試圖使其正確無法做到。如何在春季使用泛型?
任何人都可以幫助,如何初始化bean文件中的泛型並使其工作?
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="collectionDemo" class="com.prashant.Collections6.CollectionDemo">
<constructor-arg index="0" type="String" value="google"/>
<constructor-arg index="1" type="String" value="gooogle"/>
</bean>
</beans>
CollectionDemo.java
public class CollectionDemo<T> {
private T id,phoneNumber;
public CollectionDemo(T id, T phoneNumber) {
super();
this.id = id;
this.phoneNumber = phoneNumber;
}
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
public T getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(T phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "CollectionDemo [id=" + id + ", phoneNumber=" + phoneNumber
+ "]";
}
}
ColleactionApp.java
package com.prashanth.Collections6;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ColleactionApp {
public static void main(String[] args) {
ApplicationContext app=new FileSystemXmlApplicationContext
("/src/main/java/com/prashanth/Collections6/beans.xml");
CollectionDemo<String> coll=(CollectionDemo<String>) app.getBean("collectionDemo");
System.out.println(coll);
}
}
你能解釋一下有什麼問題嗎? – 2014-11-02 15:58:39
我只是想使用字符串類型實例化類,就像我們在覈心Java中做的那樣,我如何使用bean配置文件實現它。 – Terminator 2014-11-02 16:06:10
你目前的解決方案有什麼問題? – 2014-11-02 16:07:58