以下内容完全符合我的要求。虽然,它没有综合工厂的实现,但它已经足够好了,因为工厂可以访问 injection context 以便在期间可以使用其他 beans(可注入的工件)建造。它使用基于 java 的 @Configuration 而不是 XML,但它也适用于 XML。
工厂界面:
public interface Robot {
}
// Implementation of this is to be injected by the IoC in the Robot instances
public interface Brain {
String think();
}
public class RobotImpl implements Robot {
private final String name_;
private final Brain brain_;
@Inject
public RobotImpl(String name, Brain brain) {
name_ = name;
brain_ = brain;
}
public String toString() {
return "RobotImpl [name_=" + name_ + "] thinks about " + brain_.think();
}
}
public class RobotBrain implements Brain {
public String think() {
return "an idea";
}
}
// The assisted factory type
public interface RobotFactory {
Robot newRobot(String name);
}
// 这是显示如何进行辅助注入的 Spring 配置
@Configuration
class RobotConfig {
@Bean @Scope(SCOPE_PROTOTYPE)
public RobotFactory robotFactory() {
return new RobotFactory() {
@Override
public Robot newRobot(String name) {
return new RobotImpl(name, r2dxBrain());
}
};
}
@Bean @Scope(SCOPE_PROTOTYPE)
public Brain r2dxBrain() {
return new RobotBrain();
}
}
测试代码:
public class RobotTest {
@Test
public void t1() throws Exception {
ApplicationContext ctx = new
AnnotationConfigApplicationContext(RobotConfig.class);
RobotFactory rf = ctx.getBean(RobotFactory.class);
assertThat(rf.newRobot("R2D2").toString(),
equalTo("RobotImpl [name_=R2D2] thins about an idea"));
}
}
这正是 Guice 所做的。棘手的区别是Scope。 Spring 的默认范围是 Singleton 而 Guice 的不是(它是原型)。