spring-statemachine icon indicating copy to clipboard operation
spring-statemachine copied to clipboard

Unable to get JpaStateMachineRepository autowired.

Open gaurav11007 opened this issue 7 years ago • 20 comments

Getting an error ### Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.statemachine.persist.StateMachineRuntimePersister<com.example.StateMachineDemos.statemachine.enums.States, com.example.StateMachineDemos.statemachine.enums.Events, java.lang.String>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Please help to resolve this.

gaurav11007 avatar Feb 21 '18 03:02 gaurav11007

You need to provide some sort of a sample to show what you've done.

jvalkeal avatar Feb 21 '18 13:02 jvalkeal

I've missed #497, is this somewhat related to that code?

jvalkeal avatar Feb 21 '18 15:02 jvalkeal

Same issue. Built a project as in sample 'datapersist', with all required dependencies and no more code than example. Complaint about JpaStateMachineRepository:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method stateMachineRuntimePersister in com.poc.statemachine.StateMachineConfig$JpaPersisterConfig required a bean of type 'org.springframework.statemachine.data.jpa.JpaStateMachineRepository' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.statemachine.data.jpa.JpaStateMachineRepository' in your configuration.

yangzii0920 avatar Mar 23 '18 20:03 yangzii0920

@jvalkeal @yangzii0920

I have created two sample demo web application with JpaStateMachineRepository & StateMachineService.

  1. Spring boot 1.5.10 & & SM 1.2.8 https://github.com/gaurav11007/SpringStateMachineJPAExample_1.5.10

  2. With Spring boot 2.0.0 & SM 2.0.0 https://github.com/gaurav11007/SpringStateMachinePersistanceExampleWithSpringBoot2.0.0

They are identical with respect to all the config & only change in it is the version of the state machine and spring boot.

1st One running on SM 1.2.8 is a success. No issues. 2nd one is throwing below error

Description:

Parameter 0 of method stateMachineService in com.statemachine.statemachinejpapersistanceexample20.config.StateMachineConfig required a bean of type 'com.statemachine.statemachinejpapersistanceexample20.config.StateMachineConfig' that could not be found.


Action:

Consider defining a bean of type 'com.statemachine.statemachinejpapersistanceexample20.config.StateMachineConfig' in your configuration.

Any Help?

gaurav11007 avatar Mar 27 '18 15:03 gaurav11007

Seems like it's an issue with spring-statemachine-data-jpa version 2.0.0.RELEASE.

I have another supportive log might be useful when I try to instantiate a state machine:

Hibernate: select jpareposit0_.machine_id as machine_1_7_0_, jpareposit0_.state as state2_7_0_, jpareposit0_.state_machine_context as state_ma3_7_0_ from state_machine jpareposit0_ where jpareposit0_.machine_id=?
Hibernate: insert into state_machine (state, state_machine_context, machine_id) values (?, ?, ?)
2018-03-27 17:03:52.573  INFO 28416 --- [nio-8080-exec-1] o.s.s.support.LifecycleObjectSupport     : started org.springframework.statemachine.support.DefaultStateMachineExecutor@60fe9fb1
2018-03-27 17:03:52.573  INFO 28416 --- [nio-8080-exec-1] o.s.s.support.LifecycleObjectSupport     : started S5 S1 S4 S2 S3 S6  / S1 / uuid=4ba0b337-7746-4711-8c7d-532dc45804d2 / id=mymachine
2018-03-27 17:03:52.603 ERROR 28416 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.springframework.statemachine.data.StateMachineRepository.findById(Ljava/lang/Object;)Ljava/util/Optional;] with root cause

java.lang.NoSuchMethodError: org.springframework.statemachine.data.StateMachineRepository.findById(Ljava/lang/Object;)Ljava/util/Optional;
	at org.springframework.statemachine.data.RepositoryStateMachinePersist.read(RepositoryStateMachinePersist.java:68) ~[spring-statemachine-data-common-2.0.0.RELEASE.jar:2.0.0.RELEASE]
	at org.springframework.statemachine.data.jpa.JpaPersistingStateMachineInterceptor.read(JpaPersistingStateMachineInterceptor.java:70) ~[spring-statemachine-data-jpa-2.0.0.RELEASE.jar:2.0.0.RELEASE]
	at org.springframework.statemachine.service.DefaultStateMachineService.acquireStateMachine(DefaultStateMachineService.java:94) ~[spring-statemachine-core-1.2.8.RELEASE.jar:1.2.8.RELEASE]
	at org.springframework.statemachine.service.DefaultStateMachineService.acquireStateMachine(DefaultStateMachineService.java:81) ~[spring-statemachine-core-1.2.8.RELEASE.jar:1.2.8.RELEASE]
	at com.poc.statemachine.rest.StateMachineController.getStateMachine(StateMachineController.java:81) ~[classes/:na]
	at com.poc.statemachine.rest.StateMachineController.newMachine(StateMachineController.java:47) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110) ~[spring-boot-actuator-1.5.10.RELEASE.jar:1.5.10.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106) ~[spring-boot-actuator-1.5.10.RELEASE.jar:1.5.10.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.27.jar:8.5.27]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

yangzii0920 avatar Mar 27 '18 21:03 yangzii0920

Facing the same issue with 2.0.3-Release. Will this be fixed?

Parameter 0 of method stateMachineRuntimePersister in StateMachineConfig required a bean of type 'org.springframework.statemachine.data.jpa.JpaStateMachineRepository' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'org.springframework.statemachine.data.jpa.JpaStateMachineRepository' in your configuration.

sivan123 avatar Feb 06 '19 12:02 sivan123

Adding a new repository interface solves this. Not sure why

@Repository public interface JpaStateMachineRepository extends org.springframework.statemachine.data.jpa.JpaStateMachineRepository { }

sivan123 avatar Feb 07 '19 10:02 sivan123

It still occurent on 2.1.3.RELEASE

dreamkidd avatar Nov 14 '19 05:11 dreamkidd

same issue

chanhengseang3 avatar Dec 04 '19 03:12 chanhengseang3

Solved the problem through: @EnableJpaRepositories("org.springframework.statemachine.data.jpa") @EntityScan("org.springframework.statemachine.data.jpa")

okornilov avatar Mar 17 '20 13:03 okornilov

Solved the problem through: @EnableJpaRepositories("org.springframework.statemachine.data.jpa") @EntityScan("org.springframework.statemachine.data.jpa")

Where put these annotations?

Above the class with the main method. Next to SpringBootApplication annotation

okornilov avatar Mar 17 '20 15:03 okornilov

Solved the problem through adding dependency: image

smallollams avatar Sep 02 '20 06:09 smallollams

Solved the problem through adding dependency: image

you are right, use jpa not ref jpa

snoopylovefiona avatar Feb 07 '22 12:02 snoopylovefiona

Problem occurs again when state-machine dependency is used with JDK 17 and its usage in Spring 3.0. Is there any plan to upgrade libraries in spring-state-machine? Problem is present on jpa and redis repositories (not tested on mongo).

Bartoszmleczko avatar Dec 15 '22 22:12 Bartoszmleczko

Problem occurs again when state-machine dependency is used with JDK 17 and its usage in Spring 3.0. Is there any plan to upgrade libraries in spring-state-machine? Problem is present on jpa and redis repositories (not tested on mongo).

I am facing the same issue

@jvalkeal Any recommendations for solving this issue with spring boot 3.0?

mina-sa-beshara avatar Mar 12 '23 17:03 mina-sa-beshara

None of the above mentioned solutions work with JDK 17 and spring 3.0 . Any work around?

anudeepsharma82 avatar Jul 17 '23 07:07 anudeepsharma82

@Bartoszmleczko @mina-sa-beshara @anudeepsharma82

Like many of you, I tried to use the org.springframework.statemachine:spring-statemachine-data-jpa:3.2.1 library in my application using Spring Boot 3.x, but I was unable to autowire the repository objects needed to implement the functionality.

I searched through the official documentation, but couldn't find a solution.

In the end, it wasn't until I checked the source code of the org.springframework.statemachine:spring-statemachine-data-jpa:3.2.1 library myself that I realized what was causing the problem, and it was much simpler and more ridiculous than I thought.

As you can see in the attached screenshot, the library was still using Java EE based annotations, which had already been removed in Spring Boot 3.x, and that's what was causing the problem.

스크린샷 2023-10-19 오후 12 55 14

In my opinion, this issue could be resolved if the developers of this library released a version that removed the Java EE based annotations and replaced them with Jakarta EE based annotations.

I just checked the latest changes in the source code repository, and it looks like this issue will be fixed in the upcoming version, 4.0.0, but it's going to be very hard for us to wait for this version to be released. I would like to see a hotfix released as soon as possible that fixes this issue.

iceflower avatar Oct 19 '23 04:10 iceflower

I tried the default datajpamultipersist sample with springboot 3.1.4 and springstatemachine version 3.2.1 and I get the following error. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stateMachineCnfig.Config': Unsatisfied dependency expressed through field 'stateRepository': No qualifying bean of type 'org.springframework.statemachine.data.StateRepository<org.springframework.statemachine.data.RepositoryState>' available: expected at least 1 bean which qualifies as autowire candidate.

This works fine with spring boot 2.7.12.

@jvalkeal can you please suggest workaround to make it work with spring boot 3.x. Evaluating the statemachine for a project so its critical. Given that 2.x version is eol soon. thanks for your help.

rxrram2 avatar Oct 30 '23 19:10 rxrram2

Hello! I have found a temporary workaround for this problem.

Basically you have to create a wrapper repository class that uses an entity with the same structure of the original one and you have to manually inject the repository when creating the interceptor. (I implemented only some of the methods just to prove that it's working ... it should be completed with all the methods )

You can find the code here: https://github.com/gsantandrea/springstatemachine-jpa-workaround/tree/master

Does anyone see any contraindications to this approach?

gsantandrea avatar Nov 06 '23 11:11 gsantandrea

FYI, I just encountered this bug. I don't really understand what happened, as my project was working fine (3.2.1), and then, "all of the sudden", I got this error.

1 - The initial situation (working fine) was:

pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-statemachine-samples-datapersist</artifactId>
            <version>3.2.1</version>
        </dependency>

Note: I guess I should not use 'spring-statemachine-samples-datapersist' ... anyway, it was working fine like so.

2 - Then, for some unknown reason, app stop working and I got:

Consider defining a bean of type 'org.springframework.statemachine.data.jpa.JpaStateMachineRepository' in your configuration.

3 - My fix:

After some investigations, I managed to make things work back again simply by adding EnableJpaRepositories and EntityScan annotations on my Spring Application main class as follow:

@EnableJpaRepositories("org.springframework.statemachine.data.jpa")
@EntityScan("org.springframework.statemachine.data.jpa")
@SpringBootApplication
public class MySpringApplication {

I also updated my pom.xml like so:

        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-core</artifactId>
            <version>${version.spring-statemachine}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-data-jpa</artifactId>
            <version>${version.spring-statemachine}</version>
        </dependency>

4 - All is working fine now

No more "Consider defining a bean of type 'org.springframework.statemachine.data.jpa.JpaStateMachineRepository' in your configuration" My app is working fine now :-)

pdalfarr avatar Feb 14 '24 13:02 pdalfarr