Detailed explanation of the problem of injecting beans under Spring multi-threading

question

Multi-threaded injection userThreadService in Spring cannot be injected, showing that userThreadService is null exception

code show as below:

1

2

3

4

5

6

7

8

9

10

public class UserThreadTask implements Runnable {

  @Autowired

  private UserThreadService userThreadService;

 

  @Override

  public void run() {

    AdeUser user = userThreadService.get("0");

    System.out.println(user);

  }

}

 

Solution one

Pass the Service to be injected through the construction, the code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

public class UserThreadTask implements Runnable {

  private UserThreadService userThreadService;

 

  public UserThreadTask(UserThreadService userThreadService) {

    this.userThreadService = userThreadService;

  }

 

  @Override

  public void run() {

    AdeUser user = userThreadService.get("0");

    System.out.println(user);

  }

}

 

1

2

Thread t = new Thread(new UserThreadTask(userThreadService));

t.start();

 

Solution two

Obtain the Service that needs to be used through ApplicationContext

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

 

public class ApplicationContextHolder implements ApplicationContextAware {

  private static ApplicationContext context;

  @Override

  public void setApplicationContext(ApplicationContext context) throws BeansException {

    ApplicationContextHolder.context = context;

  }

  //根据bean name 获取实例

  public static Object getBeanByName(String beanName) {

    if (beanName == null || context == null) {

      return null;

    }

    return context.getBean(beanName);

  }

  //只适合一个class只被定义一次的bean(也就是说,根据class不能匹配出多个该class的实例)

  public static Object getBeanByType(Class clazz) {

    if (clazz == null || context == null) {

      return null;

    }

    return context.getBean(clazz);

  }

  public static String[] getBeanDefinitionNames() {

    return context.getBeanDefinitionNames();

  }

}

 

Spring loads its own defined ApplicationContextHolder class

1

<bean class = "cn.com.infcn.applicationcontext.ApplicationContextHolder"></bean>

Get instance by bean name

Copy code The code is as follows:
UserService user = (UserService) ApplicationContextHolder.getBeanByName("userService"); 

Get the instance according to the class of the bean (if there are multiple instances of the class, an error will be reported)

Copy code The code is as follows:
UserService user = (UserService) ApplicationContextHolder.getBeanByType(UserService.class); 

In this way, no matter whether it is multi-threaded or not, or a common class that does not accept spring management, you can use this method to obtain spring-managed beans.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324440962&siteId=291194637