0

I have a service. I want to autowire a policy bean in my service.

@Service
class MyService {
    @Autowired
    Policy myPolicy;
}

The policy is an interface. It has 3 subclass.

interface Policy {
    void method();
}

class APolicy implements Policy...
class BPolicy implements Policy...
class CPolicy implements Policy...

There is a condition to initialize the policy class.

if (config.isA()) {
    myPolicy = new APolicy();
} else if (config.isB()) {
    myPolicy = new BPolicy();
} else {
    myPolicy = new CPolicy();
}

I want to autowired myPolicy. so I write the condition code.

class ACondition extends SpringBootConditoin {
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (config.isA()) {
            return new ConditionOutcome(true, "ok");
        } else {
            return new ConditionOutcome(false, "error");
        }
    }
}

class BCondition extends SpringBootConditoin {
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (config.isB()) {
            return new ConditionOutcome(true, "ok");
        } else {
            return new ConditionOutcome(false, "error");
        }
    }
}

class CCondition extends SpringBootConditoin {
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (config.isC()) {
            return new ConditionOutcome(true, "ok");
        } else {
            return new ConditionOutcome(false, "error");
        }
    }
}

@Service
@Conditional(ACondition.class)
class APolicy implements Policy...

@Service
@Conditional(BCondition.class)
class BPolicy implements Policy...

@Service
@Conditional(CCondition.class)
class CPolicy implements Policy...

Just one policy implementation will be initialize. Others will be ignored. but MyService can't autowired the matched policy. It shows there are 3 bean instance but don't know witch will be autowired.

so how can I autowire the matched bean without the word "new" and "if...else"?

S.Lee
  • 206
  • 1
  • 4
  • 15
  • Does this answer your question? [How to do conditional auto-wiring in Spring?](https://stackoverflow.com/questions/19225115/how-to-do-conditional-auto-wiring-in-spring) – Dusayanta Prasad Apr 25 '22 at 07:47
  • no. I want to autowire the bean just use the annotation. If I should implement the factory with if...else code, when I want to add a policy, I should change some code in factory – S.Lee Apr 25 '22 at 08:29
  • Does your `config.isA()` come from applicatiom.properties / .yml ? such as `your.bean.active=A` – Kai-Sheng Yang Apr 25 '22 at 08:51
  • no. the config file is a single file. and it's path may be changed sometimes. I'm not sure where it is. It may be read from more than 1 files in a worse case. – S.Lee Apr 25 '22 at 11:55

1 Answers1

0

I think you can use @ConditionalOnExpression if you want to put conditions based on Expression https://www.baeldung.com/spring-conditional-annotations

Aakash Thomas
  • 34
  • 1
  • 2