How to specify which version of a concrete implementation for an abstract class to autowire in Spring?

Brad :

Having the following class structure:

public abstract class A {
    String someProperty = "property"

    public abstract void doSomething();
}

@Service
public class Aa extends A {

    @Override
    public abstract void doSomething() {
        System.out.println("I did");
    }
}

@Service
public class Ab extends A {

    @Override
    public abstract void doSomething() {
        System.out.println("I did something else");
    }
}

I need a way to tell Spring which A concrete class to Autowire in my Foo service, based on a property in a properties file.

@Service
public class Foo {

    @Autowire
    private A assignMeAConcreteClass;
}

And in my properties file I have this:

should-Aa-be-used: {true, false}
JB Nizet :

Remove the @Service annotation, instead write a @Bean-annotated method in a configuration class that reads the properties, and returns the appropriate A instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Abstract Class and Concrete Subclasses

Spring - how to inject concrete interface implementation?

Abstract class extends concrete class

How define constructor implementation for an Abstract Class in Python?

Abstract class with all concrete methods

How is abstract class different from concrete class?

How to autowire Spring service with Class name?

How to autowire reference of Interface with its concrete implementation?

How to define which bean to autowire in Spring

UML Which arrow to connect concrete class and its abstract class?

How to autowire a class which is in another project

Refactoring a concrete method in abstract class which contains an abstract method

Configure Implementation Class of Abstract Factory with Spring

Return concrete type in abstract class

Python Abstract class with concrete methods

How to create an instance of concrete class from static method of abstract class

Checking of **kwargs in concrete implementation of abstract class method. Interface issue?

How does the JLS specify the terms "abstract method", "concrete method" and "default method"?

How to create an entity relationship between an abstract class and a concrete class in a java spring-boot app?

Autowiring Concrete Class into Abstract Decorator

Does Spring @transaction work on a concrete method of abstract class

View model binding: Is there a way to access properties of a concrete implementation, when the class being bound to is abstract?

scala Int class abstract def without concrete implementation

How to specify the return type of the method of an abstract class

Doctrine Abstract - Concrete class ORM

Why can't we autowire an abstract class inside a concrete class in Spring?

How to resolve concrete non abstract class instance

Python: Testing abstract class with concrete implementation details

Java Spring: Autowire an implementation of an interface based on the value of a property in the same class