@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;
}
}
applicationContext.getBean(beanName)返回结果为null, 是什么原因呢
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
Json-Huang 2019-11-01 20:18关注没有getStudentInfoById的配置吧,比如类似如下的,class需要改成实际的类全路经
<bean id="getStudentInfoById" class=“xxx.xxx.xxx”> <property name="name" value=“xxx”></property> </bean>评论 打赏 举报解决 1无用
