Springboot integrates stateless4j: Autowire get null

32 Views Asked by At

Dirs tree is as follow:enter image description here

I coding a statemachine with stateless4j in CTU.java of ConditionTest module, this module is used to store types.And create a springboot demo called demo. In the fb package of demo, I coding CTU1 extends the CTU with @Component.

here are my code:

CTU1.java

@Component
@Async("myExecutor")
public class CTU1 extends CTU {
    public CTU1() {
        super();
    }

    @Override
    public void pubCUO() {

    }

    @Override
    public void pubRO() {

    }
}

DemoApplication.java


@SpringBootApplication
@EnableAsync
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

@Component
@Async("myExecutor")
@Slf4j
class Commandline implements CommandLineRunner{

    @Autowired
    private CTU1 ctu1;

    @Override
    public void run(String... args) throws Exception {
//        var context = ApplicationContextHolder.getContext();
        StateMachine ctu1_sm = ctu1.getMachine();
        log.info(ctu1_sm.getState().toString());
    }
}

The problem is when I run the demo, I get the autowired ctu1 as null. What's the problem? log:

java.lang.NullPointerException: null
    at com.example.demo.Commandline.run(DemoApplication.java:35) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_372]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_372]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_372]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_372]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.23.jar:5.3.23]
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) ~[spring-aop-5.3.23.jar:5.3.23]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_372]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_372]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_372]
    at java.lang.Thread.run(Thread.java:750) ~[na:1.8.0_372]

I hope the ctu1 can be autowired as expected.

0

There are 0 best solutions below