springboot injects beans in threads to solve the problem that the injected beans are null

Solve the problem:

在我们开发过程中总会遇到比如在线程中需要代用service或者mapper等读取数据库,或者某些自动注入bean失效的情况

problem analysis:

In the thread, because the thread is safe, it is impossible to automatically inject the bean

Solution:

1. Obtain the required beans through the tool class in the construction method

Tool class code:


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author yang
 * @decription 通过工具类获取需要的的bean
 */
@Component
public class BeanContextUtils implements ApplicationContextAware {
    
    
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
    
    
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
    
    
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
    
    
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
    
    
        return getApplicationContext().getBean(name, clazz);
    }

}

Test thread code:


public class TestThread implements Runnable {
    
    

    private ISysUserService iSysUserService;


    public TestThread(){
    
    
        this.iSysUserService = BeanContextUtils.getBean(ISysUserService.class);
    }

    @Override
    public void run() {
    
    
        SysUser sysUser = iSysUserService.selectUserById(1l);
        System.out.println("测试通过工具类获取bean 获取用户名 : {} "+sysUser.getUserName());
    }
}

The calling thread executes:

TestThread testThread = new TestThread();
        testThread.run();

Test Results:
insert image description here

Note: There is no need to add @Component, @Service and other annotations on the class

2. Inject the bean
test thread code through the set method:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TestThread implements Runnable {
    
    

    private static ISysUserService iSysUserService;


    @Autowired
    public void setISysUserService(ISysUserService iSysUserService){
    
    
        TestThread.iSysUserService = iSysUserService;
    }

    @Override
    public void run() {
    
    
        SysUser sysUser = iSysUserService.selectUserById(1l);
        System.out.println("测试通过set方法获取bean 获取用户名 : {} "+sysUser.getUserName());
    }
}

Call code:

TestThread testThread = new TestThread();
        testThread.run();
测试结果:

insert image description here
Note: @Component annotation needs to be added above the class


3. Call the bean test thread code in the class by statically initializing the execution class :

import javax.annotation.PostConstruct;

@Component
public class TestThread implements Runnable {
    
    

    @Autowired
    private ISysUserService iSysUserService;

    //创建静态的线程类属性
    private static TestThread testThread;

    @PostConstruct
    public void testThreadInit(){
    
    
        System.out.println("执行成功");
        testThread = this;
    }

    @Override
    public void run() {
    
    
        SysUser sysUser = this.testThread.iSysUserService.selectUserById(1l);
        System.out.println("测试通过静态初始化执行类 获取bean 获取用户名 : {} "+sysUser.getUserName());
    }
}

Call test:

TestThread testThread = new TestThread();
        testThread.run();

Test Results:
insert image description here

4. Pass in through a parameterized construction method

Test thread code:

public class TestThread implements Runnable {
    
    

    private ISysUserService iSysUserService;


    public TestThread(ISysUserService iSysUserService){
    
    
        this.iSysUserService = iSysUserService;
    }

    @Override
    public void run() {
    
    
        SysUser sysUser = iSysUserService.selectUserById(1l);
        System.out.println("测试通过有参构造方法传入 获取bean 获取用户名 : {} "+sysUser.getUserName());
    }
}

Call code:

//在引用类中自动注入bean或者其他方式注入bean
    @Autowired
    private ISysUserService iSysUserService;


//再引用方法中引用测试线程
TestThread testThread = new TestThread(iSysUserService);
        testThread.run();

Test Results:
insert image description here

The personal test is available, and the above beans are created by yourself, so I won’t show examples here! ! !
There are also other bean injection methods here for reference.
Several other ways to inject beans

Guess you like

Origin blog.csdn.net/A_awen/article/details/127982092