Spring 相当于 Guice 中的 FactoryModuleBuilder、@AssistedInject 和 @Assisted 是什么?
What is the Spring equivalent to FactoryModuleBuilder, @AssistedInject, and @Assisted in Guice?
什么是Spring Framework equivalent to FactoryModuleBuilder
, @AssistedInject
, and @Assisted
in Google Guice?换句话说,使用 Spring 创建其方法接受应用程序(而非容器)必须提供的参数的工厂对象的推荐方法是什么?
Spring静态工厂方法与FactoryModuleBuilder
不同。 FactoryModuleBuilder
构建一个生成 "factories" 的 Guice 模块,实现 Factory Method Pattern。与 Spring 静态工厂方法不同,这些工厂对象的方法是实例方法,而不是静态方法。静态工厂方法的问题在于它是静态的并且没有实现接口,因此不能用替代工厂实现来替换它。但是,不同的 FactoryModuleBuilder
实例可以构建实现相同接口的不同工厂。
我不完全确定这个问题是一个骗局(只有 90% 确定),但是这个答案:
似乎有您需要的信息。具体来说,您应该这样做:
I got it working by fetching an instance of the bean used in the constructor-arg out of the context and then populating it with the values that you are working with at run-time. This bean will then be used as the parameter when you get your factory-generated bean.
public class X {
public void callFactoryAndGetNewInstance() {
User user = context.getBean("user");
user.setSomethingUsefull(...);
FileValidator validator = (FileValidator)context.getBean("fileValidator");
...
}
}
我建议阅读整个答案。
Spring 没有等同于 Guice FactoryModuleBuilder
。最接近的等价物是 Spring @Configuration
class,它提供了一个实现工厂接口的工厂 bean,其方法接受来自应用程序的任意参数。 Spring 容器可以将依赖项注入到 @Configuration
对象中,而该对象又可以提供给工厂构造函数。与 FactoryModuleBuilder
不同,Spring 方法会生成大量工厂实现的典型样板代码。
示例:
public class Vehicle {
}
public class Car extends Vehicle {
private final int numberOfPassengers;
public Car(int numberOfPassengers) {
this.numberOfPassengers = numberOfPassengers;
}
}
public interface VehicleFactory {
Vehicle createPassengerVehicle(int numberOfPassengers);
}
@Configuration
public class CarFactoryConfiguration {
@Bean
VehicleFactory carFactory() {
return new VehicleFactory() {
@Override
Vehicle createPassengerVehicle(int numberOfPassengers) {
return new Car(numberOfPassengers);
}
};
}
}
什么是Spring Framework equivalent to FactoryModuleBuilder
, @AssistedInject
, and @Assisted
in Google Guice?换句话说,使用 Spring 创建其方法接受应用程序(而非容器)必须提供的参数的工厂对象的推荐方法是什么?
Spring静态工厂方法与FactoryModuleBuilder
不同。 FactoryModuleBuilder
构建一个生成 "factories" 的 Guice 模块,实现 Factory Method Pattern。与 Spring 静态工厂方法不同,这些工厂对象的方法是实例方法,而不是静态方法。静态工厂方法的问题在于它是静态的并且没有实现接口,因此不能用替代工厂实现来替换它。但是,不同的 FactoryModuleBuilder
实例可以构建实现相同接口的不同工厂。
我不完全确定这个问题是一个骗局(只有 90% 确定),但是这个答案:
似乎有您需要的信息。具体来说,您应该这样做:
I got it working by fetching an instance of the bean used in the constructor-arg out of the context and then populating it with the values that you are working with at run-time. This bean will then be used as the parameter when you get your factory-generated bean.
public class X {
public void callFactoryAndGetNewInstance() {
User user = context.getBean("user");
user.setSomethingUsefull(...);
FileValidator validator = (FileValidator)context.getBean("fileValidator");
...
}
}
我建议阅读整个答案。
Spring 没有等同于 Guice FactoryModuleBuilder
。最接近的等价物是 Spring @Configuration
class,它提供了一个实现工厂接口的工厂 bean,其方法接受来自应用程序的任意参数。 Spring 容器可以将依赖项注入到 @Configuration
对象中,而该对象又可以提供给工厂构造函数。与 FactoryModuleBuilder
不同,Spring 方法会生成大量工厂实现的典型样板代码。
示例:
public class Vehicle {
}
public class Car extends Vehicle {
private final int numberOfPassengers;
public Car(int numberOfPassengers) {
this.numberOfPassengers = numberOfPassengers;
}
}
public interface VehicleFactory {
Vehicle createPassengerVehicle(int numberOfPassengers);
}
@Configuration
public class CarFactoryConfiguration {
@Bean
VehicleFactory carFactory() {
return new VehicleFactory() {
@Override
Vehicle createPassengerVehicle(int numberOfPassengers) {
return new Car(numberOfPassengers);
}
};
}
}