Dependency Injection, need factory like functionality

isah

I'm developing a Web application using Spring.

I have a common abstract class and many implementions of it.

Now, in a controller, depending on some parameter, I need different implementations of it.

This can be easily implemented with Factory Pattern.

For example:

abstract class Animal{
    public abstract void run(){
        }
}

class Dog extends Animal{   
...
}
Class Cat extends Animal{
...
}

Now with factory pattern, I can create a factory class with a factory method which creates Animals based on some parameter. But I don't want to create instances on my own.

I need the same functionality, but I want Spring to manage everything. Because different implementations have their dependencies and I want them to be injected by Spring.

What is the best way of handling this situation?

M. Deinum

Configure the beans you want to be created as prototype beans. Next create a factory which basically knows which bean to retrieve from the application context based on the input (so instead of creating them you basically let spring do the heavy lifting).

Defining the components can be done with either @Component combinded with @Scope("prototype") or by using XML configuration.

abstract class Animal{
    public abstract void run(){}
}

@Component
@Scope("prototype")
class Dog extends Animal{}


@Component
@Scope("prototype")
Class Cat extends Animal{}

And an AnimalFactory to complete the answer.

@Component
public class AnimalFactory implements BeanFactoryAware {

    private BeanFactory factory;

    public Animal create(String input) {
        if ("cat".equals(input) ) {
            return factory.getBean(Cat.class);

        } else if ("dog".equals(input) ) {
            return factory.getBean(Dog.class);
        }
    }

    public void setBeanFactory(BeanFactory beanFactory) {
        this.factory=beanFactory;
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dependency Injection lifetimes for a Factory

Which dependency injection "factory" to use?

Generic Interface dependency injection into factory

Angularjs dependency injection inside factory

PHP Dependency Injection Container With Factory

Dependency injection inside model factory

Spring Dependency injection with factory(dynamic value)

dependency injection in factory method causes NullPointerException

Angularjs dependency injection, array, $inject, factory

Using a Strategy and Factory Pattern with Dependency Injection

Handle dependency injection with factory method DP

Should I choose Dependency Injection or Factory Pattern

Custom consumer implementation factory with Microsoft Dependency Injection

Why Dependency injection of a factory inside another factory failing?

Angular Dependency Injection -Can't Instantiate Factory/ Undefined Factory

Do we need interfaces for dependency injection?

Need help understanding Interfaces and Dependency Injection

Do we need dependency injection for Model

Dagger injection ViewModel Factory compile time error (dependency cycle in dagger)

Can Dependency Injection be considered to be a replacement for factory method pattern?

Factory method and Dependency injection, Some services are not able to be constructed

ASP.Net Core 3.0 Dependency Injection ignoring Factory Methods?

Using Factory Pattern with ASP.NET Core Dependency Injection

Passing Services using Dependency Injection and Factory Pattern in ASP.NET

object lifetime using factory method for dependency injection in .net 5

.Using a Factory Pattern with NET Core 3.1 Dependency Injection

Factory pattern with dependency injection, serviceProvider always return null

How Can a Custom Controller Factory Relate to Dependency Injection?

How to Implement ViewController custom init using dependency injection and factory patterns?

TOP Ranking

HotTag

Archive