在Guice中,相当于FactoryModuleBuilder,@ AssistedInject和@Assisted的Spring是什么? [英] What is the Spring equivalent to FactoryModuleBuilder, @AssistedInject, and @Assisted in Guice?

查看:90
本文介绍了在Guice中,相当于FactoryModuleBuilder,@ AssistedInject和@Assisted的Spring是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 Spring框架等同于@Assisted href ="https://github.com/google/guice" rel ="noreferrer"> Google Guice ?换句话说,使用Spring创建工厂对象的推荐方法是什么?该对象的方法接受应用程序(而不是容器)必须提供的参数?

What is the Spring Framework equivalent to FactoryModuleBuilder, @AssistedInject, and @Assisted in Google Guice? In other words, what is the recommended approach using Spring to create factory objects whose methods accept arguments that the application (not the container) must provide?

Spring静态工厂方法与FactoryModuleBuilder不同. FactoryModuleBuilder构建一个Guice模块,该模块生成用于实现工厂方法模式的工厂".与Spring静态工厂方法不同,这些工厂对象的方法是实例方法,而不是静态方法.静态工厂方法的问题在于它是静态的,并且没有实现接口,因此无法用替代工厂实现来代替.但是,不同的FactoryModuleBuilder实例可以构建实现相同接口的不同工厂.

The Spring static factory method is not the same as FactoryModuleBuilder. FactoryModuleBuilder builds a Guice module that generates "factories" that implement the Factory Method Pattern. Unlike a Spring static factory method, the methods of these factory objects are instance methods, not static methods. The problem with a static factory method is that it is static and doesn't implement an interface so it cannot be replaced with an alternative factory implementation. Different FactoryModuleBuilder instances, however, can build different factories that implement the same interface.

推荐答案

Spring与Guice FactoryModuleBuilder等效.最接近的等效项是Spring @Configuration类,该类提供实现工厂接口的工厂bean,该工厂接口的方法接受应用程序中的任意参数. Spring容器可以将依赖项注入到@Configuration对象中,该对象又可以提供给工厂构造函数.与FactoryModuleBuilder不同,Spring方法会生成许多工厂实现中典型的样板代码.

Spring has no equivalent to the Guice FactoryModuleBuilder. The closest equivalent would be a Spring @Configuration class that provides a factory bean that implements a factory interface whose methods accept arbitrary arguments from the application. The Spring container could inject dependencies into the @Configuration object that it, in turn, could supply to the factory constructor. Unlike with FactoryModuleBuilder, the Spring approach produces a lot of boilerplate code typical of factory implementations.

示例:

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);
            }
        };
    }
}

这篇关于在Guice中,相当于FactoryModuleBuilder,@ AssistedInject和@Assisted的Spring是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆