Search code examples
optaplanner

Optaplanner + Spring Boot. Is it possible to inject bean in ConstraintProvider implementation class?


I have an application with Optaplanner + Spring Boot, which solves VRP problem. One of the needs for solving this problem is to know the distances between two points. I do this through a separate class that makes the http request. I tried to inject a bean of this class through the @Autowired annotation, but the bean is not injected. Therefore, the question is whether it is possible to inject bean into the ConstrainProvider, since I don’t want to drag this dependency into the DAO layer


Solution

  • The ConstraintProvider is not even instantiated as a bean in the Spring context, so you cannot inject anything into it.

    Important to note, that making any HTTP requests from the constraints in the ConstraintProvider will kill the performance.

    If possible, try incorporating the distance matrix into your domain model and initialize it before solving.