applicationContext.getBean(beanName)返回结果为null, 是什么原因呢

@Component
@Slf4j
public class StudentInfoFactory implements ApplicationContextAware{

    private static Map<String, StudentInfoStrategy> strategyMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
        Map<String,StudentInfoStrategy> map = applicationContext.getBeansOfType(StudentInfoStrategy.class);
        Object studentInfoStrategy = applicationContext.getBean("getStudentInfoById");


        map.forEach((key, value) -> strategyMap.put(value.getQueryAction(), value));
    }

    public  <T extends StudentInfoStrategy> T getStrategy(String queryAction) {
        StudentInfoStrategy strategy = strategyMap.get(queryAction);
        if(strategy == null){
            LogUtils.info(log,"error");
            throw new BaseCheckException("error");
        }
        return (T)strategy;
    }

}

程序运行时debug,applicationContext是有内容的,beanName也拿到了,但是拿不到bean
图片说明

代码地址:https://github.com/shejieyin/templates

没有getStudentInfoById的配置吧,比如类似如下的,class需要改成实际的类全路经

<bean id="getStudentInfoById" class=“xxx.xxx.xxx”>
    <property name="name" value=“xxx”></property>
 </bean>

根本原因:你的StudentInfoFactory类没有交给Spring管理,你可能会疑惑,为什么加了@Component,还没有给Spring管理呢?
鉴于你加的是@Component注解,那你肯定是想用<context:component-scan base-package=""/>扫描,或是@ComponentScan注解去扫描,

所以那就是你扫描的路径没扫描到这个StudentInfoFactory类,检查一下StudentInfoFactory类是否在你扫描的范围内即可,保证在扫描范围内就OK