@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;