Kerwin
Kerwin

Reputation: 1212

How to make a bean as a custom type?

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>

Except

Bean foo is the instances of com.test.Foo,not com.test.Proxy

Question

How should I do in the Proxy class,it's seems spring provide a interface to do with this, but i really don't know how to achieve this?

I also search by google,but don't find it,maybe the keyword I used was wrong,can anyone help or give a guide link to me, thanks very much.

Result

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

Upvotes: 0

Views: 57

Answers (1)

jlumietu
jlumietu

Reputation: 6444

You must provide an implementation of Foo, as you must instantiate the implementation and not the interface:

class Proxy{
  private Foo customType;
  ..
}

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

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

Then in the xml, set it like this:

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

Upvotes: 1

Related Questions