xiaoziyin 2019-11-01 19:24 采纳率: 0%
浏览 4503

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

  • 写回答

2条回答 默认 最新

  • Json-Huang 2019-11-01 20:18
    关注

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

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

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作