Spring 中@Autowired自动装配对象和new对象的区别
@Autowired相当于setter,在对象注入之前已经实例化了,是在这个接口注解的时候实例化的,而new只是实例化一个对象,而且new的对象不能调用注入的其他类
example
controller
@controller
public class BusinessShopShoesController extends BaseController {
@c
private ShoesService shoesService;//相当于setter,已经实例化
}
service
@service
public class ShoesService extends CrudService<ShoesDao, Shoes> {
@Autowired
ShoesModelDao shoesModelDao;
@Transactional(readOnly = false)
public Shoes get(int id)
{
return shoesModelDao.get(id);
}
}
如果,controller中使用的是new 一个对象,那在service层中的ShoesModelDao就不能直接被调用,因为他是被依赖注入的,如果使用@Autowired,这时就在controller中可以直接调用ShoesModelDao了