ApplicationListener<ContextRefreshedEvent>使用

【ApplicationListener<ContextRefreshedEvent>使用】示例

import com.google.common.collect.Maps; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import java.util.Map; /** * Description: * * @author Lenovo * @date 2020/8/12 **/ @Component public class MyApplicationListener implements ApplicationListener { private static Map,UserService> userServiceMap = Maps.newHashMap(); @Override public void onApplicationEvent(ContextRefreshedEvent event) { ApplicationContext applicationContext = event.getApplicationContext(); Map, UserService> beansOfType = applicationContext.getBeansOfType(UserService.class); for (UserService userService : beansOfType.values()) { System.out.println(userService); userServiceMap.put(userService.getId(),userService); } System.out.println(userServiceMap); }}

    推荐阅读