Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Spring equivalent to FactoryModuleBuilder, @AssistedInject, and @Assisted in Guice?

Tags:

java

spring

guice

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?

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.

like image 587
Derek Mahar Avatar asked Apr 10 '15 07:04

Derek Mahar


People also ask

Does Guice work with spring?

Guice uses binding as the equivalent to wiring in Spring. Simply put, bindings allow us to define how dependencies are going to be injected into a class. Guice bindings are declared in our module's configure() method. Instead of @Autowired, Guice uses the @Inject annotation to inject the dependencies.

Which is better Guice or Spring?

Spring allows you to omit the @Autowired annotation when there's only one constructor. Guice allows binding to a Provider, as well as injecting a Provider of your class, even when your class has no Provider binding.

What is AssistedInject?

AssistedInject allows you to dynamically configure Factory for class instead of coding it by yourself. This is often useful when you have an object that has a dependencies that should be injected and some parameters that must be specified during creation of object.

What is Guice AssistedInject?

an easier way to get Guice to build auto-wired factories. Factories are a well established pattern for creating value objects, model/domain objects (entities), or objects that combine parameterization and dependencies. Factories can be brittle and contain a lot of boilerplate.


1 Answers

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.

Example:

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);
            }
        };
    }
}
like image 61
Derek Mahar Avatar answered Oct 04 '22 07:10

Derek Mahar