栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@Autowired和@Resource的区别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@Autowired和@Resource的区别

@Autowired 依照类型进行装配
@Resource 依照名称进行装配

@Resource
private StringRedisTemplate redisTemplate;

启动时会报错,因为是按名称进行装配;

@Resource(name = "stringRedisTemplate")
private StringRedisTemplate redisTemplate;

启动正常,按name为stringRedisTemplate进行装配


在Controller中需要注入service那么我的这个server有两个实现类,可以用@Resource(name="xxxxxx")或者@Autowired @Qualifier("xxxxxx")来区分

public interface ProductService

@Service("aProductService")
public class AProductService implements ProductService

@Service("bProductService")
public class BProductService implements ProductService


在Controller中
第一种方法
@Resource(name="bProductService")
ProductService productService;

第二种方法

@Autowired
@Qualifier("bProductService")
ProductService productService;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号