实例化类和实例化对象@Autowired和new⼀个对象的区别
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)
{
(id);
}
}
如果,controller中使⽤的是new ⼀个对象,那在service层中的ShoesModelDao就不能直接被调⽤,
因为他是被依赖注⼊的,如果使⽤@Autowired,这时就在controller中可以直接调⽤ShoesModelDao了