java - spring 注入為null問(wèn)題
問(wèn)題描述
如圖:
@Componentpublic class UserArgumentResolver implements HandlerMethodArgumentResolver{ @Autowired private RedisTemplate<String,User> redisTemplate; @Override public boolean supportsParameter(MethodParameter parameter) {if(parameter.getParameterAnnotation(CurrentUser.class)!=null&& parameter.getParameterType()==User.class){ return true;}return false; } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest webRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {HttpServletRequest request= (HttpServletRequest) webRequest.getNativeRequest(); // todoreturn null; }}
創(chuàng)建的類實(shí)現(xiàn)HandlerMethodArgumentResolver后,發(fā)現(xiàn)在里面注入bean實(shí)例都失效了,向上面的redisTemplate實(shí)例在運(yùn)行的時(shí)候就為null,同一個(gè)包下其他的class都是正常。請(qǐng)問(wèn)是什么原因?qū)е耣ean的注入失敗。
問(wèn)題解答
回答1:你使用的UserArgumentResolver對(duì)象沒有被IoC容器管理, 因?yàn)樵贎Autowired注解沒有配置required=false的情況下spring發(fā)現(xiàn)沒有該對(duì)象會(huì)直接拋出Exception, 不會(huì)出現(xiàn)注入null的情況.
回答2:HandlerMethodArgumentResolver接口應(yīng)該是被spring實(shí)例化的,不是IOC容器實(shí)例化管理的
相關(guān)文章:
1. python - django orm 過(guò)濾日期為當(dāng)天日期的數(shù)據(jù)2. nginx - pip install python庫(kù)報(bào)錯(cuò)3. mysql里的大表用mycat做水平拆分,是不是要先手動(dòng)分好,再配置mycat4. 老師您的微信號(hào)是多少?5. window下mysql中文亂碼怎么解決??6. mysql - 數(shù)據(jù)庫(kù)如何存儲(chǔ)小說(shuō)數(shù)據(jù)比較好?7. python - (初學(xué)者)代碼運(yùn)行不起來(lái),求指導(dǎo),謝謝!8. mysql - 5千萬(wàn)文章,怎么做相關(guān)文章?9. 為什么python中實(shí)例檢查推薦使用isinstance而不是type?10. mysql如何添加索引的時(shí)候指定索引方式
