I wanted one of my bean to created at some condition which is decided at runtime. So, I choose to use spring @Conditional annotation for that. Whats happening is that the class which implements the Condition interface is getting instantiated multiple times and its match method also gets called multiple times. Its behavior should not be like that.
I am using spring boot
Here is the code I used.
package com.zopper.bsi.controllers;
import org.springframework.context.annotation.Conditional;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zopper.bsi.config.conditional.HelloConditionalBean;
@Conditional(HelloConditionalBean.class)
@Controller
public class MainController {
@RequestMapping(value = "/ping", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public @ResponseBody String ping() throws Exception {
return "pong";
}
}
package com.zopper.bsi.config.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class HelloConditionalBean implements Condition {
static int staticCount = 0;
int instCount = 0;
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
System.out.println("HelloConditionalBean " + ++staticCount + ", instCount= " + ++instCount + ", " + this);
return true;
}
}
Here is the output:
HelloConditionalBean staticCount= 1, instCount= 1, com.zopper.bsi.config.conditional.HelloConditionalBean@c1a4620
HelloConditionalBean staticCount= 2, instCount= 1, com.zopper.bsi.config.conditional.HelloConditionalBean@3c0fbd3a
HelloConditionalBean staticCount= 3, instCount= 1, com.zopper.bsi.config.conditional.HelloConditionalBean@3daf03d8
Please find the pom.xml and related files in the attached bug.zip file. bug.zip
Comment From: wilkinsona
While Spring Boot provides several conditions, it's Spring Framework that's responsible for evaluating them. Unfortunately that means that you've opened this issue in the wrong place. It needs to be in Spring Framework's JIRA: http://jira.spring.io/browse/SPR.
Note that if you do open a JIRA ticket, you should explain why you believe that "its behavior should not be like that". AFAIK, there's no guarantee that a condition will only be evaluated once.
Comment From: philwebb
Indeed, multiple calls to the conditions are expected and necessary.
Comment From: Katharsas
Problem is, it gets called with different context environments, which makes it impossible to determine if an environment variable is really null or if its just the call with the wrong environment. I guess i should put that still in Spring JIRA?
Comment From: razilein
Did anyone create an issue in Spring JIRA? Please link it, I am facing the same issue in spring 5.3.21
Comment From: snicoll
@razilein this comment was from 6 years ago. In the meantime, Spring Framework moved to GitHub as well. Before you create an issue, please note this from Andy's comment above:
Note that if you do open a JIRA ticket, you should explain why you believe that "its behavior should not be like that". AFAIK, there's no guarantee that a condition will only be evaluated once.
Comment From: AmanMulani
Did anyone create an issue in Spring JIRA? Please link it, I am facing the same issue in spring 5.3.21
Any progress here? Facing the same issue! My instance is getting invoked 6 times.
Comment From: wilkinsona
@AmanMulani I'm not sure why you're commenting here asking for a progress update. Please read the previous comments on this issue. They explain that it is to be expected that a condition is evaluated more than once and that this behavior is part of Spring Framework which has a separate issue tracker.