方法一:在初始化時保存ApplicationContext對象
方法二:通過Spring提供的utils類獲取ApplicationContext對象
方法三:繼承自抽象類ApplicationObjectSupport
方法四:繼承自抽象類WebApplicationObjectSupport
方法五:實現接口ApplicationContextAware
方法六:通過Spring提供的ContextLoader
方法一:在初始化時保存ApplicationContext對象
@Service public class ApplicationContextLoader { /** * 用於保存接口實現類名及對應的類 */ private Map<String, IService> map; @Autowired ApplicationContext applicationContext; @PostConstruct public void init(){ //根據接口類型返回相應的所有bean Map<String, IService> map = applicationContext.getBeansOfType(IService.class); System.out.println(map); } public Map<String, IService> getMap() { return map; } }
參考范例博客地址:https://www.cnblogs.com/yjbjingcha/p/6752265.html