相关疑难解决方法(0)

Spring没有将bean注入线程

1.如何将弹簧豆注入螺纹中

2.如何在spring bean中启动一个线程.

这是我的代码.

MyThread.java

@Component
public class MyThread implements Runnable {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    SessionFactory sessionFactory;

    public void run() {

        while (true) {
            System.out.println("Inside run()");
            try {
                System.out.println("SessionFactory : " + sessionFactory);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(10000);

                System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames()));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

我正在run从下面的类调用方法(请建议我是否遵循错误的appraoch调用spring bean中的线程)

@Component
public class MyServiceCreationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        if (event.getApplicationContext().getParent() == null) {
            System.out.println("\nThread Started");
            Thread …
Run Code Online (Sandbox Code Playgroud)

spring multithreading dependency-injection

4
推荐指数
1
解决办法
6080
查看次数

什么是Java中的Thread的LifeCycle?

在java中我们创建一个线程对象

Thread t1 = new Thread(Runnable object);
t1.start();
Run Code Online (Sandbox Code Playgroud)

线程生命周期的不同阶段t1和执行后run()的状态是t1什么?

java lifecycle multithreading

0
推荐指数
2
解决办法
2万
查看次数