I would like to create a Condition which uses a special Bean. Is it possible?
i.e. something like this:
@Component
public class MyCondition implements Condition{
@Autowired
MyBean myBean;
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return myBean.isTrue();
}
}
I.e. in some cases I have to get a database session bean and check something in a database before I can say if the condition is true or false.
I need that condition to be used with @Conditional
annotation then.
I.e. :
@Conditional(MyCondition.class)
@Configuration
public class Config{
//...
}
No you can't. As it says in the documentation this is because it might
cause premature bean instantiation, violating the container and causing unintended side-effects
so in your case, if your condition references a DB bean, the DB might get instantiated before the container is able to fulfil its preconditions.