zsmeijin
6/16/2019 - 1:40 PM

通过ApplicationContext获取Bean

[StaticContextAccessor getBean] #Spring

@Component
public class StaticContextAccessor {
    private static StaticContextAccessor instance;
    
    @Autowired
    private ApplicationContext applicationContext;
    
    @PostConstruct
    public void registerInstance() {
        instance = this;
    }

    public static <T> T getBean(Class<T> clazz) {
        return instance.applicationContext.getBean(clazz);
    }
}