@Autowired配合@Qualifier注解不生效,找不到bean

在学习SpringMVC错误处理时,遇到@Qualifier("studentValidator")注解无法生效的问题,导致找不到bean。解决方案是为StudentValidator类添加@Component("studentValidator")注解。@Autowired通常用于按类型自动注入,当有多个相同类型的bean时,@Qualifier用于指定具体bean。没有@Component注解,Spring容器可能无法找到StudentValidator。@ModelAttribute注解的作用不明确,需要进一步学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有没有倒霉的小朋友和我一起学SpringMVC然后根据教程,在学SpringMVC错误处理的时候,明明代码跟教程一样,可是这句@Qualifier("studentValidator")死活是红色,报错不生效,找不到bean 

解决方法是在StudentValidator那个类上加注解@Component("studentValidator")

如下所示

至于为什么会这样

我觉得主要是我几个注解的意思没弄明白

@Autowired:

大概是如果一个Boss 类包含了 Car类和Officel类,正常来讲,bean文件里需要写

 <bean id="boss" class="com.baobaotao.Boss">    

        <property name="car" ref="car"/>    

        <property name="office" ref="office" />    

    </bean>    

但是如果car类和Office类加了@Autowired注解

那么

public class Boss {   

    @Autowired   

    private Car car;    

   @Autowired   

    private Office office;   

…    

}     

 <bean id="boss" class="com.baobaotao.Boss"/>    

 

@Qualifier

@Auttowired是根据类型注入的 by-type,如果很多type相同的话,需要指明具体的类型

比如说这句,你如果不加@Qualifier,你知道你的Validator是普通的Validator还是实现了接口的StudentValidator

所以要加

@Component

把普通的pojo对象实例化Spring容器中

大致相当于在配置文件里不用写

<bean id =    class =    >

所以如果没有这个注解。。。。spring可能找不到这个类,我这次错误的原因就是根本找不到StudentValidator这个bean嘛

@ModelAtrribute

没看懂其他csdn讲的,如果有哪位大佬有看懂了的,可以在评论分享下链接给我

我唯一知道的就是这个注释将数据添加到模型对象中,在jsp页面可以负责展示

 

引用 引用 引用根据这些引用内容,警告信息"warning: in the working copy of 'androidPrivacy.json', LF will be replaced by CRLF the next time Git touches it"是在提醒用户在Git操作中,回车和换行的转换将会发生。这是因为Git会在提交时对换行符进行转换以保持统一性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [warning: in the working copy of ‘App.vue‘, LF will be replaced by CRLF the next time Git touches ...](https://blog.youkuaiyun.com/weixin_44953057/article/details/129644219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Git: ‘LF will be replaced by CRLF the next time Git touches it‘ 问题解决与思考](https://blog.youkuaiyun.com/Babylonxun/article/details/126598477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [warning: in the working copy of ‘...‘, LF will be replaced by CRLF the next time Git touche](https://blog.youkuaiyun.com/weixin_55252589/article/details/129017650)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值