Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a bean into a Spring Condition class?

I am defining conditions that I will check to dynamically load one of the two implementations of my service interface later.

@Component
public class IsPolicyEnabled implements Condition {

    @Autowired
    private MyProperties props;

    @Override
    public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
        return props.isPolicyEnabled();
    }

}

And

@Component
public class MyProperties {...}

And

@Service
@Conditional(IsPolicyEnabled.class)
public class ServiceA implements Service {...}

However, I am running into a runtime error as.

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.NullPointerException
at com.xyz.utils.IsPolicyEnabled.matches(IsPolicyEnabled.java:9)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:88)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:71)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:515)

Basically, it failed to initialize props object that has been auto-wired inside the condition implementation. Is that not allowed?

How can I auto wire another dependency inside the condition implementation since my condition evaluation depends on a value provided by that dependency?

like image 210
WillMcavoy Avatar asked Aug 29 '18 07:08

WillMcavoy


1 Answers

Conditions are checked immediately before the bean-definition is due to be registered [...]

Condition, Spring Framework 5.0.8.RELEASE API documentation

You can't inject a bean into a Condition instance because there are no bean-definitions in the context yet1.

Moreover, you are not supposed to work with beans within Condition classes:

Conditions must follow the same restrictions as BeanFactoryPostProcessor and take care to never interact with bean instances.

Condition, Spring Framework 5.0.8.RELEASE API documentation

You should rethink the design because

[...] my condition evaluation depends on a value provided by that dependency.

indicates that it's not quite right.

1 Precisely speaking, there are a few beans already registered by Spring for its own needs.

like image 194
Andrew Tobilko Avatar answered Sep 28 '22 07:09

Andrew Tobilko