2016-11-09 51 views
0
class Proxy{ 
    private Class<?> customType; 
    .. 
} 
interface Foo{ 
    public String foo(); 
    .. 
} 
<bean id="foo" class="com.test.Proxy"> 
    <property name="customType" value="com.test.Foo"/> 
</bean> 

除了如何將bean作爲自定義類型?

豆foo是的com.test.Foo的情況下,不com.test.Proxy

問題

我應該如何在Proxy類做的,這是似乎春天提供一個界面做的與此,但我真的不知道如何實現這一目標?

我也搜索谷歌,但沒有找到它,也許我使用的關鍵詞是錯誤的,任何人都可以幫助或給我的指導鏈接,非常感謝。

結果

class Proxy<T> implements FactoryBean<T>{ 
    private Class<?> customType; 
    public Class<?> getObjectType() { 
    return customType; 
    } 
    public T getObject() throws Exception { 
     return (T)customObj; 
    } 
    .. 
} 
+0

也許看看Java的java.lang.reflect .Proxy和InvocationHandler機制。 – GPI

回答

1

你必須提供的Foo的實現,因爲你必須實例的執行,而不是接口:在XML

class Proxy{ 
    private Foo customType; 
    .. 
} 

interface Foo{ 
    public String foo(); 
    .. 
} 

class FooImpl implements Foo{ 
    public String foo(); 
    ... 
}  

然後,設置這樣的:

<bean id="foo" class="com.test.Proxy"> 
    <property name="customType"> 
    <bean class="com.test.FooImpl" /> 
    </property> 
</bean> 
+0

謝謝,我知道現在該怎麼做,只要實現'FactoryBean'的'getObjectType'和'getObject' – Kerwin

相關問題