Had I not seen the Sun[我本可以忍受黑暗]

I could have borne the shade[如果我不曾见过太阳]

But Light a newer Wilderness[然而阳光已使我的荒凉]

My Wilderness has made[成为更新的荒凉]

——Emily Dickinson

之前我们引用spring里的bean都是通过@Autowired或者@Resource注解获取

这里可以使用第三种方式

首先写个工具类

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ruben.utils;
/**
* @ClassName: SpringContextHolder
* @Date: 2020/11/12 0012 20:40
* @Description:
*/

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
* @ClassName: SpringContextHolder
* @Description: 可以从applicationContext获取bean
* @Date: 2020/11/12 0012 20:40
* *
* @author: <achao1441470436@gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
@Lazy(false)
@Component("SpringContextHolder")
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

private static ApplicationContext applicationContext;
private static final String ERROR_MESSAGE = "applicationContext尚未注入";

@Override
public void destroy() {
applicationContext = null;
}

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

public static <T> T getBean(Class<T> type) {
return Optional.ofNullable(applicationContext).orElseThrow(() -> new IllegalStateException(ERROR_MESSAGE)).getBean(type);
}

@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) Optional.ofNullable(applicationContext).orElseThrow(() -> new IllegalStateException(ERROR_MESSAGE)).getBean(name);
}


}

然后就可以通过

1
private static final UserService userService = SpringContextHolder.getBean(UserService.class);

获取到注入到spring容器的bean

注意,如果是单体架构项目需要在引用时在对应的组件上加@DependsOn("SpringContextHolder")注解

或者是分布式项目的话,直接写到最顶层依赖部分就行