在我的项目中,我有一些外部的Java接口(因此我无法更改它们)。
例如:
public interface Abc
{
void do123();
}
在一个Bean中,我喜欢通过注入来使用这个接口。
例如:
public class TestAbc
{
@Inject
private Abc abc;
}
但我没有这个接口的实现或生产方法。相反,我正在寻找一种方法来注入此接口的通用代理。
我猜我需要像CDI扩展这样的东西来做这样的事情。不幸的是,我找不到任何好的如何做的方法。 在一个理想的世界里,我希望可以实现这样的一个方法。
public Object produce( Class< ? > type )
{
if(isMyType(type))
{
// I can produce this type
return createProxy(type);
}
return null; // this method can't produce this type
}
有没有人知道如何做到这一点?
祝好, 约翰内斯