spring bean自动装配

说到spring自动装配的bean大伙都比较了解,@Autowired不就完了么。哈哈,今天我在review的时候发现了这个问题,小伙伴看下代码:

@Bean
    public List<PlatformCheck> platformCheckList() {
        Map<String, PlatformCheck> result = applicationContext.getBeansOfType(PlatformCheck.class);
        List<PlatformCheck> list = new ArrayList<>(result.values());
        list.sort(Comparator.comparingInt(PlatformCheck::getOrder));
        for (int i = 0; i < list.size(); i++) {
            PlatformCheck platformCheck = list.get(i);
            log.info("PlatformCheck类:{} 已经加载, order:{}", platformCheck.getClass().getName(), platformCheck.getOrder());
        }
        return list;
    }

上面代码配置一个bean

@Component
public class AuthenticationHandler {
    private final Logger log = LoggerFactory.getLogger(AuthenticationHandler.class);
    @Autowiredpublic List<PlatformCheck> platformCheckList;
    public List<Function<ContextBridge, Boolean>> loginContextInitialization;
    @Autowired
    public LoginContextCollection loginContextCollection;

上面的代码将配置的bean自动注入进来。上面的程序会正确运行么?

突然看上去其实就有些心里疑问的,List<PlatformCheck>一定会被注册进来;但是注册究竟是不是上面配置的名为platformCheckList这个bean呢。

猜你喜欢

转载自www.cnblogs.com/zzq-include/p/13183060.html