jeudi 11 avril 2019

Can we have a factory class as spring bean and have a factory method returning multiple spring beans based on the condition?

I want to return multiple spring beans based on the condition in the factory class.

Is this a good practice?

Any better ways to write the following piece of code?. Any other design patterns suitable here?

Below is the code snippet:

package com.test;

import org.springframework.stereotype.Component; import javax.annotation.Resource;

@Component public class InstanceFactory {

@Resource(name = "instance1")
private Instance instance1;

@Resource(name = "instance2")
private Instance instance2;

public Instance getService(Condition condition) {
    if (condition.one() && condition.two()) {
        return instance2;
    } else {
        return instance1;
    }
}

}

Aucun commentaire:

Enregistrer un commentaire