public interface TestInterface {
void name();
}
public class TestInterfaceImplA implements TestInterface {
public void name() {
System.out.println("impl:a");
}
}
public class TestInterfaceImplB implements TestInterface {
public void name() {
System.out.println("impl:b");
}
}
3.3 工厂Bean
public class TestFactoryBean implements FactoryBean<TestInterface> {
private Class<?> testInterface;
/**
* 随机返回TestInterface接口的实现类A或实现类B
* @return
* @throws Exception
*/
public TestInterface getObject() throws Exception {
return new Random().nextInt(2) == 0? new TestInterfaceImplA(): new TestInterfaceImplB();
}
/**
* 返回bean的类型
* @return
*/
public Class<?> getObjectType() {
return testInterface;
}
public void setTestInterface(Class<?> testInterface) {
this.testInterface = testInterface;
}
public void test() {
System.out.println("test1111");
}
}