实用知识
@resource还是@autowired
介绍:
@resource 是jdk提供的,默认根据name注入,如果不写名字,会默认name=类名首字母小写,同时支持根据类型注入.
@autowired 是spring提供的,默认根据类型注入.如果指定名字需要搭配:@Qualifier("name")使用.
使用经验
1.在idea编辑器中,@autowired会有提示无法注入,不过可以关闭这个提示.
2. 建议使用@autowired,如下图:

如果使用@resource,当我们定义属性名字的时候,如果写成了一个其他的存在的Bean类型,比如:
LoginLogMapper loginMapperspring将会报错:
Bean named 'loginMapper' is expected to be of type 'com.user.biz.mapper.LoginLogMapper' but was actually of type 'com.sun.proxy.$Proxy129'
因为根据name,LoginLogMapper在spring中应该是loginLogMapper,LoginMapper应该是loginMapper.
从而引发报错.
而@autowired则不会有这个问题.
Last updated
Was this helpful?