@Autowired和@Resource 区别

@Autowired 是按type装配bean (优先选择)
@Resource 是按name装配bean

使用@Resource 时,被装配的类,头部需要指明”被装配的名称”。
例如:
Controller类里引用UserService接口

UserController.java
@Resource
private UserService userService;

UserServiceImpl.java(UserService的实现类)

@Service(“userService”)
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;

@Override
public User getUserService(Integer id) {
	return userMapper.selectByPrimaryKey(id);
}

}

猜你喜欢

转载自blog.csdn.net/sinat_26832967/article/details/89176815